diff --git a/cdiexample/pom.xml b/cdiexample/pom.xml new file mode 100644 index 0000000000..042d22a6e9 --- /dev/null +++ b/cdiexample/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + com.baeldung + cdiexample + 1.0-SNAPSHOT + + 4.3.1.RELEASE + + + + + org.jboss.weld.se + weld-se-core + 2.3.5.Final + + + + junit + junit + 4.12 + + + + org.springframework + spring-core + ${spring.version} + + + + org.springframework + spring-context + ${spring.version} + + + + org.springframework + spring-test + ${spring.version} + test + + + + org.aspectj + aspectjweaver + 1.8.9 + + + \ No newline at end of file diff --git a/cdiexample/src/main/java/com/baeldung/interceptor/Audited.java b/cdiexample/src/main/java/com/baeldung/interceptor/Audited.java new file mode 100644 index 0000000000..4065450b09 --- /dev/null +++ b/cdiexample/src/main/java/com/baeldung/interceptor/Audited.java @@ -0,0 +1,12 @@ +package com.baeldung.interceptor; + +import javax.interceptor.InterceptorBinding; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@InterceptorBinding +@Target( {ElementType.METHOD, ElementType.TYPE } ) +@Retention(RetentionPolicy.RUNTIME ) +public @interface Audited {} diff --git a/cdiexample/src/main/java/com/baeldung/interceptor/AuditedInterceptor.java b/cdiexample/src/main/java/com/baeldung/interceptor/AuditedInterceptor.java new file mode 100644 index 0000000000..6501c60ad0 --- /dev/null +++ b/cdiexample/src/main/java/com/baeldung/interceptor/AuditedInterceptor.java @@ -0,0 +1,20 @@ +package com.baeldung.interceptor; + +import javax.interceptor.AroundInvoke; +import javax.interceptor.Interceptor; +import javax.interceptor.InvocationContext; +import java.lang.reflect.Method; + +@Audited @Interceptor +public class AuditedInterceptor { + @AroundInvoke + public Object auditMethod(InvocationContext ctx) throws Exception { + Object[] parameters = ctx.getParameters(); + Method method= ctx.getMethod(); + String param = (String) parameters[0]; + System.out.println("Method "+method.getName()+" invoked with parameter "+param); + Object result = ctx.proceed(); + System.out.println("Method "+method.getName()+" exit"); + return result; + } +} diff --git a/cdiexample/src/main/java/com/baeldung/service/SuperService.java b/cdiexample/src/main/java/com/baeldung/service/SuperService.java new file mode 100644 index 0000000000..5309c80b10 --- /dev/null +++ b/cdiexample/src/main/java/com/baeldung/service/SuperService.java @@ -0,0 +1,11 @@ +package com.baeldung.service; + +import com.baeldung.interceptor.Audited; + +public class SuperService { + @Audited + public String deliverService(String uid) { + System.out.println("Service delivered for uid:" + uid); + return uid; + } +} diff --git a/cdiexample/src/main/java/com/baeldung/spring/aspect/SpringTestAspect.java b/cdiexample/src/main/java/com/baeldung/spring/aspect/SpringTestAspect.java new file mode 100644 index 0000000000..ab3036285e --- /dev/null +++ b/cdiexample/src/main/java/com/baeldung/spring/aspect/SpringTestAspect.java @@ -0,0 +1,19 @@ +package com.baeldung.spring.aspect; + +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; + +@Aspect +public class SpringTestAspect { + @Around("execution(* com.baeldung.spring.service.SpringSuperService.*(..))") + public Object advice(ProceedingJoinPoint jp) throws Throwable { + String methodName = jp.getSignature().getName(); + System.out.println("Call to "+methodName); + Object obj = jp.proceed(); + System.out.println("Method called successfully: "+methodName); + return obj; + } +} diff --git a/cdiexample/src/main/java/com/baeldung/spring/configuration/AppConfig.java b/cdiexample/src/main/java/com/baeldung/spring/configuration/AppConfig.java new file mode 100644 index 0000000000..e8dbf264b5 --- /dev/null +++ b/cdiexample/src/main/java/com/baeldung/spring/configuration/AppConfig.java @@ -0,0 +1,21 @@ +package com.baeldung.spring.configuration; + +import com.baeldung.spring.aspect.SpringTestAspect; +import com.baeldung.spring.service.SpringSuperService; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableAspectJAutoProxy; + +@Configuration +@EnableAspectJAutoProxy +public class AppConfig { + @Bean + public SpringSuperService springSuperService() { + return new SpringSuperService(); + } + + @Bean + public SpringTestAspect springTestAspect(){ + return new SpringTestAspect(); + } +} diff --git a/cdiexample/src/main/java/com/baeldung/spring/service/SpringSuperService.java b/cdiexample/src/main/java/com/baeldung/spring/service/SpringSuperService.java new file mode 100644 index 0000000000..5b96a24390 --- /dev/null +++ b/cdiexample/src/main/java/com/baeldung/spring/service/SpringSuperService.java @@ -0,0 +1,8 @@ +package com.baeldung.spring.service; + +public class SpringSuperService { + public String getInfoFromService(String code){ + System.out.println("Doing calculations"); + return code; + } +} diff --git a/cdiexample/src/main/resources/META-INF/beans.xml b/cdiexample/src/main/resources/META-INF/beans.xml new file mode 100644 index 0000000000..d41b35e7d9 --- /dev/null +++ b/cdiexample/src/main/resources/META-INF/beans.xml @@ -0,0 +1,8 @@ + + + com.baeldung.interceptor.AuditedInterceptor + + \ No newline at end of file diff --git a/cdiexample/src/test/java/com/baeldung/test/SpringTestInterceptor.java b/cdiexample/src/test/java/com/baeldung/test/SpringTestInterceptor.java new file mode 100644 index 0000000000..6085187ecd --- /dev/null +++ b/cdiexample/src/test/java/com/baeldung/test/SpringTestInterceptor.java @@ -0,0 +1,25 @@ +package com.baeldung.test; + +import com.baeldung.spring.configuration.AppConfig; +import com.baeldung.spring.service.SpringSuperService; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.inject.Inject; + +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = {AppConfig.class}) +public class SpringTestInterceptor { + @Inject + SpringSuperService springSuperService; + + @Test + public void givenService_whenServiceAndAspectExecuted_thenOk(){ + String code = "123456"; + String result = springSuperService.getInfoFromService(code); + Assert.assertEquals(code,result); + } +} diff --git a/cdiexample/src/test/java/com/baeldung/test/TestInterceptor.java b/cdiexample/src/test/java/com/baeldung/test/TestInterceptor.java new file mode 100644 index 0000000000..1ed0d99748 --- /dev/null +++ b/cdiexample/src/test/java/com/baeldung/test/TestInterceptor.java @@ -0,0 +1,19 @@ +package com.baeldung.test; + +import com.baeldung.service.SuperService; +import org.jboss.weld.environment.se.Weld; +import org.jboss.weld.environment.se.WeldContainer; +import org.junit.Assert; +import org.junit.Test; + +public class TestInterceptor { + @Test + public void givenTheService_whenMethodAndInterceptorExecuted_thenOK() { + Weld weld = new Weld(); + WeldContainer container = weld.initialize(); + SuperService superService = container.instance().select(SuperService.class).get(); + String code = "123456"; + superService.deliverService(code); + Assert.assertEquals("123456",code); + } +}