Merge remote-tracking branch 'upstream/master'
Conflicts: hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutTest.java
This commit is contained in:
commit
13f006e510
54
cdi/pom.xml
54
cdi/pom.xml
@ -1,52 +1,52 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>cdi</artifactId>
|
<artifactId>cdi</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
<properties>
|
|
||||||
<spring.version>4.3.1.RELEASE</spring.version>
|
|
||||||
</properties>
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<!-- https://mvnrepository.com/artifact/junit/junit -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>junit</groupId>
|
|
||||||
<artifactId>junit</artifactId>
|
|
||||||
<version>4.12</version>
|
|
||||||
</dependency>
|
|
||||||
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-core</artifactId>
|
<artifactId>spring-core</artifactId>
|
||||||
<version>${spring.version}</version>
|
<version>${spring.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-context</artifactId>
|
<artifactId>spring-context</artifactId>
|
||||||
<version>${spring.version}</version>
|
<version>${spring.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.aspectj</groupId>
|
||||||
|
<artifactId>aspectjweaver</artifactId>
|
||||||
|
<version>1.8.9</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jboss.weld.se</groupId>
|
||||||
|
<artifactId>weld-se-core</artifactId>
|
||||||
|
<version>2.3.5.Final</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-test</artifactId>
|
<artifactId>spring-test</artifactId>
|
||||||
<version>${spring.version}</version>
|
<version>${spring.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.aspectj</groupId>
|
|
||||||
<artifactId>aspectjweaver</artifactId>
|
|
||||||
<version>1.8.9</version>
|
|
||||||
</dependency>
|
|
||||||
<!-- https://mvnrepository.com/artifact/org.jboss.weld.se/weld-se-core -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.jboss.weld.se</groupId>
|
|
||||||
<artifactId>weld-se-core</artifactId>
|
|
||||||
<version>2.3.5.Final</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<spring.version>4.3.1.RELEASE</spring.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -1,12 +1,14 @@
|
|||||||
package com.baeldung.interceptor;
|
package com.baeldung.interceptor;
|
||||||
|
|
||||||
import javax.interceptor.InterceptorBinding;
|
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import javax.interceptor.InterceptorBinding;
|
||||||
|
|
||||||
@InterceptorBinding
|
@InterceptorBinding
|
||||||
@Target( {ElementType.METHOD, ElementType.TYPE } )
|
@Target({ ElementType.METHOD, ElementType.TYPE })
|
||||||
@Retention(RetentionPolicy.RUNTIME )
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface Audited {}
|
public @interface Audited {
|
||||||
|
}
|
||||||
|
@ -3,12 +3,13 @@ package com.baeldung.interceptor;
|
|||||||
import javax.interceptor.AroundInvoke;
|
import javax.interceptor.AroundInvoke;
|
||||||
import javax.interceptor.Interceptor;
|
import javax.interceptor.Interceptor;
|
||||||
import javax.interceptor.InvocationContext;
|
import javax.interceptor.InvocationContext;
|
||||||
import java.lang.reflect.Method;
|
|
||||||
|
|
||||||
@Audited @Interceptor
|
@Audited
|
||||||
|
@Interceptor
|
||||||
public class AuditedInterceptor {
|
public class AuditedInterceptor {
|
||||||
public static boolean calledBefore = false;
|
public static boolean calledBefore = false;
|
||||||
public static boolean calledAfter = false;
|
public static boolean calledAfter = false;
|
||||||
|
|
||||||
@AroundInvoke
|
@AroundInvoke
|
||||||
public Object auditMethod(InvocationContext ctx) throws Exception {
|
public Object auditMethod(InvocationContext ctx) throws Exception {
|
||||||
calledBefore = true;
|
calledBefore = true;
|
||||||
|
@ -5,6 +5,6 @@ import com.baeldung.interceptor.Audited;
|
|||||||
public class SuperService {
|
public class SuperService {
|
||||||
@Audited
|
@Audited
|
||||||
public String deliverService(String uid) {
|
public String deliverService(String uid) {
|
||||||
return uid;
|
return uid;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,26 +1,23 @@
|
|||||||
package com.baeldung.spring.aspect;
|
package com.baeldung.spring.aspect;
|
||||||
|
|
||||||
import org.aspectj.lang.JoinPoint;
|
import java.util.List;
|
||||||
|
|
||||||
import org.aspectj.lang.ProceedingJoinPoint;
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
import org.aspectj.lang.annotation.Around;
|
import org.aspectj.lang.annotation.Around;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
import org.aspectj.lang.annotation.Before;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Aspect
|
@Aspect
|
||||||
public class SpringTestAspect {
|
public class SpringTestAspect {
|
||||||
@Autowired
|
@Autowired
|
||||||
private List<String> accumulator;
|
private List<String> accumulator;
|
||||||
|
|
||||||
@Around("execution(* com.baeldung.spring.service.SpringSuperService.*(..))")
|
@Around("execution(* com.baeldung.spring.service.SpringSuperService.*(..))")
|
||||||
public Object advice(ProceedingJoinPoint jp) throws Throwable {
|
public Object auditMethod(ProceedingJoinPoint jp) throws Throwable {
|
||||||
String methodName = jp.getSignature().getName();
|
String methodName = jp.getSignature().getName();
|
||||||
accumulator.add("Call to "+methodName);
|
accumulator.add("Call to " + methodName);
|
||||||
Object obj = jp.proceed();
|
Object obj = jp.proceed();
|
||||||
accumulator.add("Method called successfully: "+methodName);
|
accumulator.add("Method called successfully: " + methodName);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
package com.baeldung.spring.configuration;
|
package com.baeldung.spring.configuration;
|
||||||
|
|
||||||
import com.baeldung.spring.aspect.SpringTestAspect;
|
import java.util.ArrayList;
|
||||||
import com.baeldung.spring.service.SpringSuperService;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import com.baeldung.spring.aspect.SpringTestAspect;
|
||||||
import java.util.List;
|
import com.baeldung.spring.service.SpringSuperService;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableAspectJAutoProxy
|
@EnableAspectJAutoProxy
|
||||||
@ -18,12 +19,12 @@ public class AppConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SpringTestAspect springTestAspect(){
|
public SpringTestAspect springTestAspect() {
|
||||||
return new SpringTestAspect();
|
return new SpringTestAspect();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public List<String> getAccumulator(){
|
public List<String> getAccumulator() {
|
||||||
return new ArrayList<String>();
|
return new ArrayList<String>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package com.baeldung.spring.service;
|
package com.baeldung.spring.service;
|
||||||
|
|
||||||
public class SpringSuperService {
|
public class SpringSuperService {
|
||||||
public String getInfoFromService(String code){
|
public String getInfoFromService(String code) {
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
package com.baeldung.test;
|
package com.baeldung.test;
|
||||||
|
|
||||||
import com.baeldung.interceptor.Audited;
|
|
||||||
import com.baeldung.interceptor.AuditedInterceptor;
|
|
||||||
import com.baeldung.service.SuperService;
|
|
||||||
import org.jboss.weld.environment.se.Weld;
|
import org.jboss.weld.environment.se.Weld;
|
||||||
import org.jboss.weld.environment.se.WeldContainer;
|
import org.jboss.weld.environment.se.WeldContainer;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
@ -10,24 +7,21 @@ import org.junit.Assert;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import javax.enterprise.inject.spi.BeanManager;
|
import com.baeldung.interceptor.AuditedInterceptor;
|
||||||
import javax.enterprise.inject.spi.InterceptionType;
|
import com.baeldung.service.SuperService;
|
||||||
import javax.enterprise.inject.spi.Interceptor;
|
|
||||||
import javax.enterprise.util.AnnotationLiteral;
|
|
||||||
|
|
||||||
import static javafx.beans.binding.Bindings.select;
|
|
||||||
|
|
||||||
public class TestInterceptor {
|
public class TestInterceptor {
|
||||||
Weld weld;
|
Weld weld;
|
||||||
WeldContainer container;
|
WeldContainer container;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init(){
|
public void init() {
|
||||||
weld = new Weld();
|
weld = new Weld();
|
||||||
container = weld.initialize();
|
container = weld.initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void shutdown(){
|
public void shutdown() {
|
||||||
weld.shutdown();
|
weld.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,7 +30,9 @@ public class TestInterceptor {
|
|||||||
SuperService superService = container.select(SuperService.class).get();
|
SuperService superService = container.select(SuperService.class).get();
|
||||||
String code = "123456";
|
String code = "123456";
|
||||||
superService.deliverService(code);
|
superService.deliverService(code);
|
||||||
|
|
||||||
Assert.assertTrue(AuditedInterceptor.calledBefore);
|
Assert.assertTrue(AuditedInterceptor.calledBefore);
|
||||||
Assert.assertTrue(AuditedInterceptor.calledAfter);
|
Assert.assertTrue(AuditedInterceptor.calledAfter);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,21 @@
|
|||||||
package com.baeldung.test;
|
package com.baeldung.test;
|
||||||
|
|
||||||
import com.baeldung.spring.configuration.AppConfig;
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
import com.baeldung.spring.service.SpringSuperService;
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.TestExecutionListeners;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
|
||||||
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
|
|
||||||
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import com.baeldung.spring.configuration.AppConfig;
|
||||||
import java.util.List;
|
import com.baeldung.spring.service.SpringSuperService;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@ContextConfiguration(classes = {AppConfig.class})
|
@ContextConfiguration(classes = { AppConfig.class })
|
||||||
public class TestSpringInterceptor {
|
public class TestSpringInterceptor {
|
||||||
@Autowired
|
@Autowired
|
||||||
SpringSuperService springSuperService;
|
SpringSuperService springSuperService;
|
||||||
@ -28,11 +24,11 @@ public class TestSpringInterceptor {
|
|||||||
private List<String> accumulator;
|
private List<String> accumulator;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenService_whenServiceAndAspectExecuted_thenOk(){
|
public void givenService_whenServiceAndAspectExecuted_thenOk() {
|
||||||
String code = "123456";
|
String code = "123456";
|
||||||
String result = springSuperService.getInfoFromService(code);
|
String result = springSuperService.getInfoFromService(code);
|
||||||
Assert.assertThat(accumulator.size(), is(2));
|
Assert.assertThat(accumulator.size(), is(2));
|
||||||
Assert.assertThat(accumulator.get(0),is("Call to getInfoFromService"));
|
Assert.assertThat(accumulator.get(0), is("Call to getInfoFromService"));
|
||||||
Assert.assertThat(accumulator.get(1),is("Method called successfully: getInfoFromService"));
|
Assert.assertThat(accumulator.get(1), is("Method called successfully: getInfoFromService"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,11 @@
|
|||||||
package com.baeldung.completablefuture;
|
package com.baeldung.completablefuture;
|
||||||
|
|
||||||
import org.junit.Test;
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.Future;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
@ -46,6 +43,28 @@ public class CompletableFutureTest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Future<String> calculateAsyncWithCancellation() throws InterruptedException {
|
||||||
|
CompletableFuture<String> completableFuture = new CompletableFuture<>();
|
||||||
|
|
||||||
|
Executors.newCachedThreadPool().submit(() -> {
|
||||||
|
Thread.sleep(500);
|
||||||
|
completableFuture.cancel(false);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
return completableFuture;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test(expected = CancellationException.class)
|
||||||
|
public void whenCancelingTheFuture_thenThrowsCancellationException() throws ExecutionException, InterruptedException {
|
||||||
|
|
||||||
|
Future<String> future = calculateAsyncWithCancellation();
|
||||||
|
future.get();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCreatingCompletableFutureWithSupplyAsync_thenFutureReturnsValue() throws ExecutionException, InterruptedException {
|
public void whenCreatingCompletableFutureWithSupplyAsync_thenFutureReturnsValue() throws ExecutionException, InterruptedException {
|
||||||
|
|
||||||
@ -187,4 +206,4 @@ public class CompletableFutureTest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -10,8 +10,13 @@ public class HystrixController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private SpringExistingClient client;
|
private SpringExistingClient client;
|
||||||
|
|
||||||
@RequestMapping("/")
|
@RequestMapping("/withHystrix")
|
||||||
public String index() throws InterruptedException{
|
public String withHystrix() throws InterruptedException{
|
||||||
return client.invokeRemoteService();
|
return client.invokeRemoteServiceWithHystrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/withOutHystrix")
|
||||||
|
public String withOutHystrix() throws InterruptedException{
|
||||||
|
return client.invokeRemoteServiceWithOutHystrix();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,8 +10,11 @@ public class SpringExistingClient {
|
|||||||
private int remoteServiceDelay;
|
private int remoteServiceDelay;
|
||||||
|
|
||||||
@HystrixCircuitBreaker
|
@HystrixCircuitBreaker
|
||||||
public String invokeRemoteService() throws InterruptedException{
|
public String invokeRemoteServiceWithHystrix() throws InterruptedException{
|
||||||
return new RemoteServiceTestSimulator(remoteServiceDelay).execute();
|
return new RemoteServiceTestSimulator(remoteServiceDelay).execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String invokeRemoteServiceWithOutHystrix() throws InterruptedException{
|
||||||
|
return new RemoteServiceTestSimulator(remoteServiceDelay).execute();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,10 +5,9 @@ import com.netflix.hystrix.HystrixCommandGroupKey;
|
|||||||
import com.netflix.hystrix.HystrixCommandProperties;
|
import com.netflix.hystrix.HystrixCommandProperties;
|
||||||
import com.netflix.hystrix.HystrixThreadPoolProperties;
|
import com.netflix.hystrix.HystrixThreadPoolProperties;
|
||||||
import com.netflix.hystrix.exception.HystrixRuntimeException;
|
import com.netflix.hystrix.exception.HystrixRuntimeException;
|
||||||
import org.junit.Before;
|
import org.junit.*;
|
||||||
import org.junit.Rule;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.rules.ExpectedException;
|
import org.junit.rules.ExpectedException;
|
||||||
|
import org.junit.runners.MethodSorters;
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
@ -130,5 +129,4 @@ public class HystrixTimeoutTest {
|
|||||||
}
|
}
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -23,10 +23,14 @@ public class SpringAndHystrixIntegrationTest {
|
|||||||
public final ExpectedException exception = ExpectedException.none();
|
public final ExpectedException exception = ExpectedException.none();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenTimeOutOf15000_whenExistingClientCalled_thenExpectHystrixRuntimeException() throws InterruptedException {
|
public void givenTimeOutOf15000_whenClientCalledWithHystrix_thenExpectHystrixRuntimeException() throws InterruptedException {
|
||||||
exception.expect(HystrixRuntimeException.class);
|
exception.expect(HystrixRuntimeException.class);
|
||||||
assertThat(hystrixController.index(), equalTo("Success"));
|
hystrixController.withHystrix();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTimeOutOf15000_whenClientCalledWithOutHystrix_thenExpectSuccess() throws InterruptedException {
|
||||||
|
assertThat(hystrixController.withOutHystrix(), equalTo("Success"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
1
pom.xml
1
pom.xml
@ -17,6 +17,7 @@
|
|||||||
<module>apache-cxf</module>
|
<module>apache-cxf</module>
|
||||||
<module>apache-fop</module>
|
<module>apache-fop</module>
|
||||||
<module>autovalue-tutorial</module>
|
<module>autovalue-tutorial</module>
|
||||||
|
<module>cdi</module>
|
||||||
<module>core-java</module>
|
<module>core-java</module>
|
||||||
<module>core-java-8</module>
|
<module>core-java-8</module>
|
||||||
<module>couchbase-sdk-intro</module>
|
<module>couchbase-sdk-intro</module>
|
||||||
|
@ -0,0 +1,19 @@
|
|||||||
|
package org.baeldung.startup;
|
||||||
|
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.springframework.context.event.ContextRefreshedEvent;
|
||||||
|
import org.springframework.context.event.EventListener;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class EventListenerExampleBean {
|
||||||
|
private static final Logger LOG = Logger.getLogger(EventListenerExampleBean.class);
|
||||||
|
|
||||||
|
public static int counter;
|
||||||
|
|
||||||
|
@EventListener
|
||||||
|
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||||
|
LOG.info("Increment counter");
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
}
|
4
spring-cloud-config/docker/Dockerfile
Normal file
4
spring-cloud-config/docker/Dockerfile
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
FROM alpine:edge
|
||||||
|
MAINTAINER baeldung.com
|
||||||
|
RUN apk add --no-cache openjdk8
|
||||||
|
COPY files/UnlimitedJCEPolicyJDK8/* /usr/lib/jvm/java-1.8-openjdk/jre/lib/security/
|
6
spring-cloud-config/docker/Dockerfile.client
Normal file
6
spring-cloud-config/docker/Dockerfile.client
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
FROM alpine-java:base
|
||||||
|
MAINTAINER baeldung.com
|
||||||
|
RUN apk --no-cache add netcat-openbsd
|
||||||
|
COPY files/config-client.jar /opt/spring-cloud/lib/
|
||||||
|
COPY files/config-client-entrypoint.sh /opt/spring-cloud/bin/
|
||||||
|
RUN chmod 755 /opt/spring-cloud/bin/config-client-entrypoint.sh
|
9
spring-cloud-config/docker/Dockerfile.server
Normal file
9
spring-cloud-config/docker/Dockerfile.server
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
FROM alpine-java:base
|
||||||
|
MAINTAINER baeldung.com
|
||||||
|
COPY files/config-server.jar /opt/spring-cloud/lib/
|
||||||
|
ENV SPRING_APPLICATION_JSON='{"spring": {"cloud": {"config": {"server": \
|
||||||
|
{"git": {"uri": "/var/lib/spring-cloud/config-repo", "clone-on-start": true}}}}}}'
|
||||||
|
ENTRYPOINT ["/usr/bin/java"]
|
||||||
|
CMD ["-jar", "/opt/spring-cloud/lib/config-server.jar"]
|
||||||
|
VOLUME /var/lib/spring-cloud/config-repo
|
||||||
|
EXPOSE 8888
|
8
spring-cloud-config/docker/config-client-entrypoint.sh
Normal file
8
spring-cloud-config/docker/config-client-entrypoint.sh
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
while ! nc -z config-server 8888 ; do
|
||||||
|
echo "Waiting for upcoming Config Server"
|
||||||
|
sleep 2
|
||||||
|
done
|
||||||
|
|
||||||
|
java -jar /opt/spring-cloud/lib/config-client.jar
|
41
spring-cloud-config/docker/docker-compose.scale.yml
Normal file
41
spring-cloud-config/docker/docker-compose.scale.yml
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
version: '2'
|
||||||
|
services:
|
||||||
|
config-server:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile.server
|
||||||
|
image: config-server:latest
|
||||||
|
expose:
|
||||||
|
- 8888
|
||||||
|
networks:
|
||||||
|
- spring-cloud-network
|
||||||
|
volumes:
|
||||||
|
- spring-cloud-config-repo:/var/lib/spring-cloud/config-repo
|
||||||
|
logging:
|
||||||
|
driver: json-file
|
||||||
|
config-client:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile.client
|
||||||
|
image: config-client:latest
|
||||||
|
entrypoint: /opt/spring-cloud/bin/config-client-entrypoint.sh
|
||||||
|
environment:
|
||||||
|
SPRING_APPLICATION_JSON: '{"spring": {"cloud": {"config": {"uri": "http://config-server:8888"}}}}'
|
||||||
|
expose:
|
||||||
|
- 8080
|
||||||
|
ports:
|
||||||
|
- 8080
|
||||||
|
networks:
|
||||||
|
- spring-cloud-network
|
||||||
|
links:
|
||||||
|
- config-server:config-server
|
||||||
|
depends_on:
|
||||||
|
- config-server
|
||||||
|
logging:
|
||||||
|
driver: json-file
|
||||||
|
networks:
|
||||||
|
spring-cloud-network:
|
||||||
|
driver: bridge
|
||||||
|
volumes:
|
||||||
|
spring-cloud-config-repo:
|
||||||
|
external: true
|
43
spring-cloud-config/docker/docker-compose.yml
Normal file
43
spring-cloud-config/docker/docker-compose.yml
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
version: '2'
|
||||||
|
services:
|
||||||
|
config-server:
|
||||||
|
container_name: config-server
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile.server
|
||||||
|
image: config-server:latest
|
||||||
|
expose:
|
||||||
|
- 8888
|
||||||
|
networks:
|
||||||
|
- spring-cloud-network
|
||||||
|
volumes:
|
||||||
|
- spring-cloud-config-repo:/var/lib/spring-cloud/config-repo
|
||||||
|
logging:
|
||||||
|
driver: json-file
|
||||||
|
config-client:
|
||||||
|
container_name: config-client
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile.client
|
||||||
|
image: config-client:latest
|
||||||
|
entrypoint: /opt/spring-cloud/bin/config-client-entrypoint.sh
|
||||||
|
environment:
|
||||||
|
SPRING_APPLICATION_JSON: '{"spring": {"cloud": {"config": {"uri": "http://config-server:8888"}}}}'
|
||||||
|
expose:
|
||||||
|
- 8080
|
||||||
|
ports:
|
||||||
|
- 8080:8080
|
||||||
|
networks:
|
||||||
|
- spring-cloud-network
|
||||||
|
links:
|
||||||
|
- config-server:config-server
|
||||||
|
depends_on:
|
||||||
|
- config-server
|
||||||
|
logging:
|
||||||
|
driver: json-file
|
||||||
|
networks:
|
||||||
|
spring-cloud-network:
|
||||||
|
driver: bridge
|
||||||
|
volumes:
|
||||||
|
spring-cloud-config-repo:
|
||||||
|
external: true
|
@ -1,82 +1,58 @@
|
|||||||
package com.baeldung.hibernate.fetching.model;
|
package com.baeldung.hibernate.fetching.model;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.sql.Date;
|
import java.sql.Date;
|
||||||
|
|
||||||
public class OrderDetail implements Serializable {
|
@Entity
|
||||||
|
@Table (name = "USER_ORDER")
|
||||||
|
public class OrderDetail implements Serializable{
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
private Long orderId;
|
|
||||||
private Date orderDate;
|
@Id
|
||||||
private String orderDesc;
|
@GeneratedValue
|
||||||
private User user;
|
@Column(name="ORDER_ID")
|
||||||
|
private Long orderId;
|
||||||
public OrderDetail() {
|
|
||||||
|
public OrderDetail(){
|
||||||
}
|
}
|
||||||
|
|
||||||
public OrderDetail(Date orderDate, String orderDesc) {
|
public OrderDetail(Date orderDate, String orderDesc) {
|
||||||
super();
|
super();
|
||||||
this.orderDate = orderDate;
|
}
|
||||||
this.orderDesc = orderDesc;
|
|
||||||
}
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
public Date getOrderDate() {
|
final int prime = 31;
|
||||||
return orderDate;
|
int result = 1;
|
||||||
}
|
result = prime * result + ((orderId == null) ? 0 : orderId.hashCode());
|
||||||
|
return result;
|
||||||
public void setOrderDate(Date orderDate) {
|
}
|
||||||
this.orderDate = orderDate;
|
@Override
|
||||||
}
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
public String getOrderDesc() {
|
return true;
|
||||||
return orderDesc;
|
if (obj == null)
|
||||||
}
|
return false;
|
||||||
|
if (getClass() != obj.getClass())
|
||||||
public void setOrderDesc(String orderDesc) {
|
return false;
|
||||||
this.orderDesc = orderDesc;
|
OrderDetail other = (OrderDetail) obj;
|
||||||
}
|
if (orderId == null) {
|
||||||
|
if (other.orderId != null)
|
||||||
public User getUser() {
|
return false;
|
||||||
return user;
|
} else if (!orderId.equals(other.orderId))
|
||||||
}
|
return false;
|
||||||
|
|
||||||
public void setUser(User user) {
|
return true;
|
||||||
this.user = user;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
final int prime = 31;
|
|
||||||
int result = 1;
|
|
||||||
result = prime * result + ((orderId == null) ? 0 : orderId.hashCode());
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (this == obj)
|
|
||||||
return true;
|
|
||||||
if (obj == null)
|
|
||||||
return false;
|
|
||||||
if (getClass() != obj.getClass())
|
|
||||||
return false;
|
|
||||||
OrderDetail other = (OrderDetail) obj;
|
|
||||||
if (orderId == null) {
|
|
||||||
if (other.orderId != null)
|
|
||||||
return false;
|
|
||||||
} else if (!orderId.equals(other.orderId))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getOrderId() {
|
|
||||||
return orderId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setOrderId(Long orderId) {
|
|
||||||
this.orderId = orderId;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public Long getOrderId() {
|
||||||
|
return orderId;
|
||||||
|
}
|
||||||
|
public void setOrderId(Long orderId) {
|
||||||
|
this.orderId = orderId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,94 +0,0 @@
|
|||||||
package com.baeldung.hibernate.fetching.model;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class User implements Serializable {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
private Long userId;
|
|
||||||
private String userName;
|
|
||||||
private String firstName;
|
|
||||||
private String lastName;
|
|
||||||
private Set<OrderDetail> orderDetail = new HashSet<OrderDetail>();
|
|
||||||
|
|
||||||
public User() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public User(final Long userId, final String userName, final String firstName, final String lastName) {
|
|
||||||
super();
|
|
||||||
this.userId = userId;
|
|
||||||
this.userName = userName;
|
|
||||||
this.firstName = firstName;
|
|
||||||
this.lastName = lastName;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
final int prime = 31;
|
|
||||||
int result = 1;
|
|
||||||
result = prime * result + ((userId == null) ? 0 : userId.hashCode());
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(final Object obj) {
|
|
||||||
if (this == obj)
|
|
||||||
return true;
|
|
||||||
if (obj == null)
|
|
||||||
return false;
|
|
||||||
if (getClass() != obj.getClass())
|
|
||||||
return false;
|
|
||||||
final User other = (User) obj;
|
|
||||||
if (userId == null) {
|
|
||||||
if (other.userId != null)
|
|
||||||
return false;
|
|
||||||
} else if (!userId.equals(other.userId))
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getUserId() {
|
|
||||||
return userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUserId(final Long userId) {
|
|
||||||
this.userId = userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUserName() {
|
|
||||||
return userName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUserName(final String userName) {
|
|
||||||
this.userName = userName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getFirstName() {
|
|
||||||
return firstName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFirstName(final String firstName) {
|
|
||||||
this.firstName = firstName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getLastName() {
|
|
||||||
return lastName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLastName(final String lastName) {
|
|
||||||
this.lastName = lastName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<OrderDetail> getOrderDetail() {
|
|
||||||
return orderDetail;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setOrderDetail(Set<OrderDetail> orderDetail) {
|
|
||||||
this.orderDetail = orderDetail;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,71 @@
|
|||||||
|
package com.baeldung.hibernate.fetching.model;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table (name = "USER")
|
||||||
|
public class UserEager implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
@Column(name="USER_ID")
|
||||||
|
private Long userId;
|
||||||
|
|
||||||
|
@OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
|
||||||
|
private Set<OrderDetail> orderDetail = new HashSet();
|
||||||
|
|
||||||
|
public UserEager() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserEager(final Long userId) {
|
||||||
|
super();
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = prime * result + ((userId == null) ? 0 : userId.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(final Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null)
|
||||||
|
return false;
|
||||||
|
if (getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
final UserEager other = (UserEager) obj;
|
||||||
|
if (userId == null) {
|
||||||
|
if (other.userId != null)
|
||||||
|
return false;
|
||||||
|
} else if (!userId.equals(other.userId))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserId(final Long userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<OrderDetail> getOrderDetail() {
|
||||||
|
return orderDetail;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrderDetail(Set<OrderDetail> orderDetail) {
|
||||||
|
this.orderDetail = orderDetail;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,72 @@
|
|||||||
|
package com.baeldung.hibernate.fetching.model;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table (name = "USER")
|
||||||
|
public class UserLazy implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
@Column(name="USER_ID")
|
||||||
|
private Long userId;
|
||||||
|
|
||||||
|
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
|
||||||
|
private Set<OrderDetail> orderDetail = new HashSet();
|
||||||
|
|
||||||
|
public UserLazy() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserLazy(final Long userId) {
|
||||||
|
super();
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = prime * result + ((userId == null) ? 0 : userId.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(final Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null)
|
||||||
|
return false;
|
||||||
|
if (getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
final UserLazy other = (UserLazy) obj;
|
||||||
|
if (userId == null) {
|
||||||
|
if (other.userId != null)
|
||||||
|
return false;
|
||||||
|
} else if (!userId.equals(other.userId))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserId(final Long userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Set<OrderDetail> getOrderDetail() {
|
||||||
|
return orderDetail;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrderDetail(Set<OrderDetail> orderDetail) {
|
||||||
|
this.orderDetail = orderDetail;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,24 +1,26 @@
|
|||||||
package com.baeldung.hibernate.fetching.util;
|
package com.baeldung.hibernate.fetching.util;
|
||||||
|
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
|
|
||||||
public class HibernateUtil {
|
public class HibernateUtil {
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
public static Session getHibernateSession(String fetchMethod) {
|
public static Session getHibernateSession(String fetchMethod) {
|
||||||
//two config files are there
|
//two config files are there
|
||||||
//one with lazy loading enabled
|
//one with lazy loading enabled
|
||||||
//another lazy = false
|
//another lazy = false
|
||||||
|
SessionFactory sf;
|
||||||
final String configFileName = "lazy".equals(fetchMethod) ?
|
if ("lazy".equals(fetchMethod)) {
|
||||||
"fetchingLazy.cfg.xml" :
|
sf = new Configuration().configure("fetchingLazy.cfg.xml").buildSessionFactory();
|
||||||
"fetching.cfg.xml";
|
} else {
|
||||||
|
sf = new Configuration().configure("fetching.cfg.xml").buildSessionFactory();
|
||||||
return new Configuration()
|
}
|
||||||
.configure(configFileName)
|
|
||||||
.buildSessionFactory().openSession();
|
// fetching.cfg.xml is used for this example
|
||||||
}
|
return sf.openSession();
|
||||||
|
}
|
||||||
|
|
||||||
public static Session getHibernateSession() {
|
public static Session getHibernateSession() {
|
||||||
return new Configuration()
|
return new Configuration()
|
||||||
|
@ -1,20 +1,14 @@
|
|||||||
package com.baeldung.hibernate.fetching.view;
|
package com.baeldung.hibernate.fetching.view;
|
||||||
|
|
||||||
import java.sql.Date;
|
import com.baeldung.hibernate.fetching.model.OrderDetail;
|
||||||
import java.util.List;
|
import com.baeldung.hibernate.fetching.model.UserEager;
|
||||||
import java.util.Set;
|
import com.baeldung.hibernate.fetching.model.UserLazy;
|
||||||
|
import com.baeldung.hibernate.fetching.util.HibernateUtil;
|
||||||
import org.hibernate.Hibernate;
|
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.Transaction;
|
import org.hibernate.Transaction;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import com.baeldung.hibernate.fetching.util.HibernateUtil;
|
import java.util.Set;
|
||||||
|
|
||||||
import com.baeldung.hibernate.fetching.model.OrderDetail;
|
|
||||||
|
|
||||||
import com.baeldung.hibernate.fetching.model.User;
|
|
||||||
|
|
||||||
|
|
||||||
public class FetchingAppView {
|
public class FetchingAppView {
|
||||||
|
|
||||||
@ -22,52 +16,37 @@ public class FetchingAppView {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean lazyLoaded() {
|
// lazily loaded
|
||||||
|
public Set<OrderDetail> lazyLoaded() {
|
||||||
final Session sessionLazy = HibernateUtil.getHibernateSession("lazy");
|
final Session sessionLazy = HibernateUtil.getHibernateSession("lazy");
|
||||||
List<User> users = sessionLazy.createQuery("From User").list();
|
List<UserLazy> users = sessionLazy.createQuery("From UserLazy").list();
|
||||||
User userLazyLoaded = users.get(3);
|
UserLazy userLazyLoaded = users.get(3);
|
||||||
//since data is lazyloaded so data won't be initialized
|
// since data is lazyloaded so data won't be initialized
|
||||||
Set<OrderDetail> orderDetailSet = userLazyLoaded.getOrderDetail();
|
return (userLazyLoaded.getOrderDetail());
|
||||||
|
|
||||||
return (Hibernate.isInitialized(orderDetailSet));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean eagerLoaded() {
|
// eagerly loaded
|
||||||
|
public Set<OrderDetail> eagerLoaded() {
|
||||||
final Session sessionEager = HibernateUtil.getHibernateSession();
|
final Session sessionEager = HibernateUtil.getHibernateSession();
|
||||||
//data should be loaded in the following line
|
// data should be loaded in the following line
|
||||||
//also note the queries generated
|
// also note the queries generated
|
||||||
List<User> users = sessionEager.createQuery("From User").list();
|
List<UserEager> user = sessionEager.createQuery("From UserEager").list();
|
||||||
User userEagerLoaded = users.get(3);
|
UserEager userEagerLoaded = user.get(3);
|
||||||
Set<OrderDetail> orderDetailSet = userEagerLoaded.getOrderDetail();
|
return userEagerLoaded.getOrderDetail();
|
||||||
|
|
||||||
return (Hibernate.isInitialized(orderDetailSet));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// creates test data
|
||||||
//creates test data
|
// call this method to create the data in the database
|
||||||
//call this method to create the data in the database
|
|
||||||
public void createTestData() {
|
public void createTestData() {
|
||||||
|
|
||||||
final Session session = HibernateUtil.getHibernateSession();
|
final Session session = HibernateUtil.getHibernateSession("lazy");
|
||||||
Transaction tx = session.beginTransaction();
|
Transaction tx = session.beginTransaction();
|
||||||
|
final UserLazy user1 = new UserLazy();
|
||||||
|
final UserLazy user2 = new UserLazy();
|
||||||
|
final UserLazy user3 = new UserLazy();
|
||||||
|
|
||||||
final User user1 = new User();
|
|
||||||
final User user2 = new User();
|
|
||||||
final User user3 = new User();
|
|
||||||
|
|
||||||
user1.setFirstName("Priyam");
|
|
||||||
user1.setLastName("Banerjee");
|
|
||||||
user1.setUserName("priyambanerjee");
|
|
||||||
session.save(user1);
|
session.save(user1);
|
||||||
|
|
||||||
user2.setFirstName("Navneeta");
|
|
||||||
user2.setLastName("Mukherjee");
|
|
||||||
user2.setUserName("nmukh");
|
|
||||||
session.save(user2);
|
session.save(user2);
|
||||||
|
|
||||||
user3.setFirstName("Molly");
|
|
||||||
user3.setLastName("Banerjee");
|
|
||||||
user3.setUserName("mollyb");
|
|
||||||
session.save(user3);
|
session.save(user3);
|
||||||
|
|
||||||
final OrderDetail order1 = new OrderDetail();
|
final OrderDetail order1 = new OrderDetail();
|
||||||
@ -76,35 +55,14 @@ public class FetchingAppView {
|
|||||||
final OrderDetail order4 = new OrderDetail();
|
final OrderDetail order4 = new OrderDetail();
|
||||||
final OrderDetail order5 = new OrderDetail();
|
final OrderDetail order5 = new OrderDetail();
|
||||||
|
|
||||||
order1.setOrderDesc("First Order");
|
|
||||||
order1.setOrderDate(new Date(2014, 10, 12));
|
|
||||||
order1.setUser(user1);
|
|
||||||
|
|
||||||
order2.setOrderDesc("Second Order");
|
|
||||||
order2.setOrderDate(new Date(2016, 10, 25));
|
|
||||||
order2.setUser(user1);
|
|
||||||
|
|
||||||
order3.setOrderDesc("Third Order");
|
|
||||||
order3.setOrderDate(new Date(2015, 2, 17));
|
|
||||||
order3.setUser(user2);
|
|
||||||
|
|
||||||
order4.setOrderDesc("Fourth Order");
|
|
||||||
order4.setOrderDate(new Date(2014, 10, 1));
|
|
||||||
order4.setUser(user2);
|
|
||||||
|
|
||||||
order5.setOrderDesc("Fifth Order");
|
|
||||||
order5.setOrderDate(new Date(2014, 9, 11));
|
|
||||||
order5.setUser(user3);
|
|
||||||
|
|
||||||
|
|
||||||
session.saveOrUpdate(order1);
|
session.saveOrUpdate(order1);
|
||||||
session.saveOrUpdate(order2);
|
session.saveOrUpdate(order2);
|
||||||
session.saveOrUpdate(order3);
|
session.saveOrUpdate(order3);
|
||||||
session.saveOrUpdate(order4);
|
session.saveOrUpdate(order4);
|
||||||
session.saveOrUpdate(order5);
|
session.saveOrUpdate(order5);
|
||||||
|
|
||||||
// session.saveOrUpdate(user1);
|
|
||||||
tx.commit();
|
tx.commit();
|
||||||
session.close();
|
session.close();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,10 @@
|
|||||||
<property name="hibernate.connection.password">iamtheking</property>
|
<property name="hibernate.connection.password">iamtheking</property>
|
||||||
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
|
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
|
||||||
<property name="show_sql">true</property>
|
<property name="show_sql">true</property>
|
||||||
<mapping resource="com/baeldung/hibernate/fetching/model/User.hbm.xml" />
|
<property name="hbm2ddl.auto">validate</property>
|
||||||
<mapping resource="com/baeldung/hibernate/fetching/model/OrderDetail.hbm.xml" />
|
|
||||||
|
|
||||||
|
<mapping class="com.baeldung.hibernate.fetching.model.UserEager" />
|
||||||
|
<mapping class="com.baeldung.hibernate.fetching.model.OrderDetailEager" />
|
||||||
</session-factory>
|
</session-factory>
|
||||||
</hibernate-configuration>
|
</hibernate-configuration>
|
@ -11,7 +11,7 @@
|
|||||||
<property name="hibernate.connection.password">iamtheking</property>
|
<property name="hibernate.connection.password">iamtheking</property>
|
||||||
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
|
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
|
||||||
<property name="show_sql">true</property>
|
<property name="show_sql">true</property>
|
||||||
<mapping resource="com/baeldung/hibernate/fetching/model/OrderDetail.hbm.xml" />
|
<mapping class="com.baeldung.hibernate.fetching.model.UserLazy" />
|
||||||
<mapping resource="com/baeldung/hibernate/fetching/model/UserLazy.hbm.xml" />
|
<mapping class="com.baeldung.hibernate.fetching.model.OrderDetail" />
|
||||||
</session-factory>
|
</session-factory>
|
||||||
</hibernate-configuration>
|
</hibernate-configuration>
|
@ -1,17 +1,12 @@
|
|||||||
CREATE TABLE `user` (
|
CREATE TABLE `user` (
|
||||||
`user_id` int(10) NOT NULL AUTO_INCREMENT,
|
`user_id` int(10) NOT NULL AUTO_INCREMENT,
|
||||||
`USERNAME` varchar(100) DEFAULT NULL,
|
|
||||||
`FIRST_NAME` varchar(255) NOT NULL,
|
|
||||||
`LAST_NAME` varchar(255) NOT NULL,
|
|
||||||
PRIMARY KEY (`user_id`)
|
PRIMARY KEY (`user_id`)
|
||||||
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1 ;
|
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1 ;
|
||||||
|
|
||||||
|
|
||||||
CREATE TABLE `user_order` (
|
CREATE TABLE `user_order` (
|
||||||
`ORDER_ID` int(10) NOT NULL AUTO_INCREMENT,
|
`ORDER_ID` int(10) NOT NULL AUTO_INCREMENT,
|
||||||
`ORDER_DATE` date DEFAULT NULL,
|
|
||||||
`USER_ID` int(10) NOT NULL DEFAULT '0',
|
`USER_ID` int(10) NOT NULL DEFAULT '0',
|
||||||
`ORDER_DESC` varchar(1024) DEFAULT NULL,
|
|
||||||
PRIMARY KEY (`ORDER_ID`,`USER_ID`),
|
PRIMARY KEY (`ORDER_ID`,`USER_ID`),
|
||||||
KEY `USER_ID` (`USER_ID`),
|
KEY `USER_ID` (`USER_ID`),
|
||||||
CONSTRAINT `user_order_ibfk_1` FOREIGN KEY (`USER_ID`) REFERENCES `USER` (`user_id`)
|
CONSTRAINT `user_order_ibfk_1` FOREIGN KEY (`USER_ID`) REFERENCES `USER` (`user_id`)
|
||||||
|
@ -1,24 +1,43 @@
|
|||||||
package com.baeldung.hibernate.fetching;
|
package com.baeldung.hibernate.fetching;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import com.baeldung.hibernate.fetching.model.OrderDetail;
|
||||||
|
import com.baeldung.hibernate.fetching.view.FetchingAppView;
|
||||||
|
import org.hibernate.Hibernate;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.baeldung.hibernate.fetching.view.FetchingAppView;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class HibernateFetchingTest {
|
public class HibernateFetchingTest {
|
||||||
|
|
||||||
|
|
||||||
|
//this loads sample data in the database
|
||||||
|
@Before
|
||||||
|
public void addFecthingTestData(){
|
||||||
|
FetchingAppView fav = new FetchingAppView();
|
||||||
|
fav.createTestData();
|
||||||
|
}
|
||||||
|
|
||||||
|
//testLazyFetching() tests the lazy loading
|
||||||
|
//Since it lazily loaded so orderDetalSetLazy won't
|
||||||
|
//be initialized
|
||||||
@Test
|
@Test
|
||||||
public void testLazyFetching() {
|
public void testLazyFetching() {
|
||||||
FetchingAppView fav = new FetchingAppView();
|
FetchingAppView fav = new FetchingAppView();
|
||||||
fav.createTestData();
|
Set<OrderDetail> orderDetalSetLazy = fav.lazyLoaded();
|
||||||
assertFalse(fav.lazyLoaded());
|
assertFalse(Hibernate.isInitialized(orderDetalSetLazy));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//testEagerFetching() tests the eager loading
|
||||||
|
//Since it eagerly loaded so orderDetalSetLazy would
|
||||||
|
//be initialized
|
||||||
@Test
|
@Test
|
||||||
public void testEagerFetching() {
|
public void testEagerFetching() {
|
||||||
FetchingAppView fav = new FetchingAppView();
|
FetchingAppView fav = new FetchingAppView();
|
||||||
assertTrue(fav.eagerLoaded());
|
Set<OrderDetail> orderDetalSetEager = fav.eagerLoaded();
|
||||||
|
assertTrue(Hibernate.isInitialized(orderDetalSetEager));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,26 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<!DOCTYPE hibernate-mapping PUBLIC
|
|
||||||
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
|
||||||
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
|
||||||
|
|
||||||
<hibernate-mapping>
|
|
||||||
<class name="com.baeldung.hibernate.fetching.model.OrderDetail" table="USER_ORDER">
|
|
||||||
|
|
||||||
<id name="orderId" type="java.lang.Long" column="ORDER_ID" >
|
|
||||||
<generator class="native" />
|
|
||||||
</id>
|
|
||||||
|
|
||||||
<property name="orderDate" type="date">
|
|
||||||
<column name="ORDER_DATE" />
|
|
||||||
</property>
|
|
||||||
<property name="orderDesc" type="string">
|
|
||||||
<column name="ORDER_DESC" not-null="true" />
|
|
||||||
</property>
|
|
||||||
|
|
||||||
|
|
||||||
<many-to-one name="user" class="com.baeldung.hibernate.fetching.model.User" fetch="select">
|
|
||||||
<column name="user_id" not-null="true" />
|
|
||||||
</many-to-one>
|
|
||||||
|
|
||||||
</class>
|
|
||||||
</hibernate-mapping>
|
|
@ -1,31 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<!DOCTYPE hibernate-mapping PUBLIC
|
|
||||||
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
|
||||||
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
|
||||||
<hibernate-mapping>
|
|
||||||
<class name="com.baeldung.hibernate.fetching.model.User" table="USER">
|
|
||||||
<id name="userId" type="long" unsaved-value="null">
|
|
||||||
<column name="USER_ID" />
|
|
||||||
<generator class="native" />
|
|
||||||
</id>
|
|
||||||
|
|
||||||
<property name="userName" type="string">
|
|
||||||
<column name="USERNAME" length="100" />
|
|
||||||
</property>
|
|
||||||
<property name="firstName" type="string">
|
|
||||||
<column name="FIRST_NAME" not-null="true" />
|
|
||||||
</property>
|
|
||||||
<property name="lastName" type="string">
|
|
||||||
<column name="LAST_NAME" not-null="true" />
|
|
||||||
</property>
|
|
||||||
|
|
||||||
<set name="orderDetail" table="USER_ORDER"
|
|
||||||
inverse="true" lazy="false" fetch="select">
|
|
||||||
<key>
|
|
||||||
<column name="USER_ID" not-null="true" />
|
|
||||||
</key>
|
|
||||||
<one-to-many class="com.baeldung.hibernate.fetching.model.OrderDetail" />
|
|
||||||
</set>
|
|
||||||
|
|
||||||
</class>
|
|
||||||
</hibernate-mapping>
|
|
@ -1,31 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<!DOCTYPE hibernate-mapping PUBLIC
|
|
||||||
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
|
||||||
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
|
||||||
<hibernate-mapping>
|
|
||||||
<class name="com.baeldung.hibernate.fetching.model.User" table="USER">
|
|
||||||
<id name="userId" type="long" unsaved-value="null">
|
|
||||||
<column name="USER_ID" />
|
|
||||||
<generator class="native" />
|
|
||||||
</id>
|
|
||||||
|
|
||||||
<property name="userName" type="string">
|
|
||||||
<column name="USERNAME" length="100" />
|
|
||||||
</property>
|
|
||||||
<property name="firstName" type="string">
|
|
||||||
<column name="FIRST_NAME" not-null="true" />
|
|
||||||
</property>
|
|
||||||
<property name="lastName" type="string">
|
|
||||||
<column name="LAST_NAME" not-null="true" />
|
|
||||||
</property>
|
|
||||||
|
|
||||||
<set name="orderDetail" table="USER_ORDER"
|
|
||||||
inverse="true" lazy="true" fetch="select">
|
|
||||||
<key>
|
|
||||||
<column name="USER_ID" not-null="true" />
|
|
||||||
</key>
|
|
||||||
<one-to-many class="com.baeldung.hibernate.fetching.model.OrderDetail" />
|
|
||||||
</set>
|
|
||||||
|
|
||||||
</class>
|
|
||||||
</hibernate-mapping>
|
|
@ -11,7 +11,8 @@
|
|||||||
<property name="hibernate.connection.password">iamtheking</property>
|
<property name="hibernate.connection.password">iamtheking</property>
|
||||||
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
|
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
|
||||||
<property name="show_sql">true</property>
|
<property name="show_sql">true</property>
|
||||||
<mapping resource="com/baeldung/hibernate/fetching/model/User.hbm.xml" />
|
|
||||||
<mapping resource="com/baeldung/hibernate/fetching/model/OrderDetail.hbm.xml" />
|
<mapping class="com.baeldung.hibernate.fetching.model.UserEager" />
|
||||||
|
<mapping class="com.baeldung.hibernate.fetching.model.OrderDetailEager" />
|
||||||
</session-factory>
|
</session-factory>
|
||||||
</hibernate-configuration>
|
</hibernate-configuration>
|
@ -11,7 +11,8 @@
|
|||||||
<property name="hibernate.connection.password">iamtheking</property>
|
<property name="hibernate.connection.password">iamtheking</property>
|
||||||
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
|
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
|
||||||
<property name="show_sql">true</property>
|
<property name="show_sql">true</property>
|
||||||
<mapping resource="com/baeldung/hibernate/fetching/model/OrderDetail.hbm.xml" />
|
<mapping class="com.baeldung.hibernate.fetching.model.UserLazy" />
|
||||||
<mapping resource="com/baeldung/hibernate/fetching/model/UserLazy.hbm.xml" />
|
<mapping class="com.baeldung.hibernate.fetching.model.OrderDetail" />
|
||||||
|
|
||||||
</session-factory>
|
</session-factory>
|
||||||
</hibernate-configuration>
|
</hibernate-configuration>
|
@ -0,0 +1,42 @@
|
|||||||
|
package com.baeldung.web.controller;
|
||||||
|
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
|
import com.baeldung.spring.web.config.WebConfig;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@WebAppConfiguration
|
||||||
|
@ContextConfiguration(classes = WebConfig.class)
|
||||||
|
public class EmployeeTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WebApplicationContext webAppContext;
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenEmployeeGETisPerformed_thenRetrievedStatusAndViewNameAndAttributeAreCorrect() throws Exception {
|
||||||
|
mockMvc.perform(get("/employee")).andExpect(status().isOk()).andExpect(view().name("employeeHome")).andExpect(model().attributeExists("employee")).andDo(print());
|
||||||
|
}
|
||||||
|
}
|
@ -248,11 +248,11 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<!-- <dependency> -->
|
||||||
<groupId>org.hamcrest</groupId>
|
<!-- <groupId>org.hamcrest</groupId> -->
|
||||||
<artifactId>hamcrest-core</artifactId>
|
<!-- <artifactId>hamcrest-core</artifactId> -->
|
||||||
<scope>test</scope>
|
<!-- <scope>test</scope> -->
|
||||||
</dependency>
|
<!-- </dependency> -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.hamcrest</groupId>
|
<groupId>org.hamcrest</groupId>
|
||||||
<artifactId>hamcrest-library</artifactId>
|
<artifactId>hamcrest-library</artifactId>
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.baeldung.spring.security.x509;
|
package com.baeldung.spring.security.x509;
|
||||||
|
|
||||||
|
import java.security.Principal;
|
||||||
|
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
@ -7,8 +9,6 @@ import org.springframework.stereotype.Controller;
|
|||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
import java.security.Principal;
|
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class UserController {
|
public class UserController {
|
||||||
@PreAuthorize("hasAuthority('ROLE_USER')")
|
@PreAuthorize("hasAuthority('ROLE_USER')")
|
||||||
|
@ -2,21 +2,15 @@ package com.baeldung.spring.security.x509;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
import org.springframework.security.core.authority.AuthorityUtils;
|
|
||||||
import org.springframework.security.core.userdetails.User;
|
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
|
||||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
||||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
public class X509AuthenticationServer extends WebSecurityConfigurerAdapter {
|
public class X509AuthenticationServer extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(X509AuthenticationServer.class, args);
|
SpringApplication.run(X509AuthenticationServer.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.baeldung.spring.security.x509;
|
package com.baeldung.spring.security.x509;
|
||||||
|
|
||||||
|
import java.security.Principal;
|
||||||
|
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
@ -7,8 +9,6 @@ import org.springframework.stereotype.Controller;
|
|||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
import java.security.Principal;
|
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class UserController {
|
public class UserController {
|
||||||
@PreAuthorize("hasAuthority('ROLE_USER')")
|
@PreAuthorize("hasAuthority('ROLE_USER')")
|
||||||
|
@ -23,9 +23,7 @@ public class X509AuthenticationServer extends WebSecurityConfigurerAdapter {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(HttpSecurity http) throws Exception {
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
http.authorizeRequests().anyRequest().authenticated()
|
http.authorizeRequests().anyRequest().authenticated().and().x509().subjectPrincipalRegex("CN=(.*?)(?:,|$)").userDetailsService(userDetailsService());
|
||||||
.and()
|
|
||||||
.x509().subjectPrincipalRegex("CN=(.*?)(?:,|$)").userDetailsService(userDetailsService());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
Loading…
x
Reference in New Issue
Block a user