commit
7d5b613c20
|
@ -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;
|
||||||
|
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue