Skip to content

Commit 644a7d2

Browse files
added module cdiexample
1 parent 5b28a3c commit 644a7d2

10 files changed

Lines changed: 195 additions & 0 deletions

File tree

‎cdiexample/pom.xml‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>cdiexample</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<properties>
11+
<spring.version>4.3.1.RELEASE</spring.version>
12+
</properties>
13+
<dependencies>
14+
<!-- https://mvnrepository.com/artifact/org.jboss.weld.se/weld-se-core -->
15+
<dependency>
16+
<groupId>org.jboss.weld.se</groupId>
17+
<artifactId>weld-se-core</artifactId>
18+
<version>2.3.5.Final</version>
19+
</dependency>
20+
<!-- https://mvnrepository.com/artifact/junit/junit -->
21+
<dependency>
22+
<groupId>junit</groupId>
23+
<artifactId>junit</artifactId>
24+
<version>4.12</version>
25+
</dependency>
26+
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
27+
<dependency>
28+
<groupId>org.springframework</groupId>
29+
<artifactId>spring-core</artifactId>
30+
<version>${spring.version}</version>
31+
</dependency>
32+
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
33+
<dependency>
34+
<groupId>org.springframework</groupId>
35+
<artifactId>spring-context</artifactId>
36+
<version>${spring.version}</version>
37+
</dependency>
38+
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
39+
<dependency>
40+
<groupId>org.springframework</groupId>
41+
<artifactId>spring-test</artifactId>
42+
<version>${spring.version}</version>
43+
<scope>test</scope>
44+
</dependency>
45+
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
46+
<dependency>
47+
<groupId>org.aspectj</groupId>
48+
<artifactId>aspectjweaver</artifactId>
49+
<version>1.8.9</version>
50+
</dependency>
51+
</dependencies>
52+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.interceptor;
2+
3+
import javax.interceptor.InterceptorBinding;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
@InterceptorBinding
10+
@Target( {ElementType.METHOD, ElementType.TYPE } )
11+
@Retention(RetentionPolicy.RUNTIME )
12+
public @interface Audited {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.interceptor;
2+
3+
import javax.interceptor.AroundInvoke;
4+
import javax.interceptor.Interceptor;
5+
import javax.interceptor.InvocationContext;
6+
import java.lang.reflect.Method;
7+
8+
@Audited @Interceptor
9+
public class AuditedInterceptor {
10+
@AroundInvoke
11+
public Object auditMethod(InvocationContext ctx) throws Exception {
12+
Object[] parameters = ctx.getParameters();
13+
Method method= ctx.getMethod();
14+
String param = (String) parameters[0];
15+
System.out.println("Method "+method.getName()+" invoked with parameter "+param);
16+
Object result = ctx.proceed();
17+
System.out.println("Method "+method.getName()+" exit");
18+
return result;
19+
}
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.service;
2+
3+
import com.baeldung.interceptor.Audited;
4+
5+
public class SuperService {
6+
@Audited
7+
public String deliverService(String uid) {
8+
System.out.println("Service delivered for uid:" + uid);
9+
return uid;
10+
}
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.spring.aspect;
2+
3+
import org.aspectj.lang.JoinPoint;
4+
import org.aspectj.lang.ProceedingJoinPoint;
5+
import org.aspectj.lang.annotation.Around;
6+
import org.aspectj.lang.annotation.Aspect;
7+
import org.aspectj.lang.annotation.Before;
8+
9+
@Aspect
10+
public class SpringTestAspect {
11+
@Around("execution(* com.baeldung.spring.service.SpringSuperService.*(..))")
12+
public Object advice(ProceedingJoinPoint jp) throws Throwable {
13+
String methodName = jp.getSignature().getName();
14+
System.out.println("Call to "+methodName);
15+
Object obj = jp.proceed();
16+
System.out.println("Method called successfully: "+methodName);
17+
return obj;
18+
}
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.spring.configuration;
2+
3+
import com.baeldung.spring.aspect.SpringTestAspect;
4+
import com.baeldung.spring.service.SpringSuperService;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
8+
9+
@Configuration
10+
@EnableAspectJAutoProxy
11+
public class AppConfig {
12+
@Bean
13+
public SpringSuperService springSuperService() {
14+
return new SpringSuperService();
15+
}
16+
17+
@Bean
18+
public SpringTestAspect springTestAspect(){
19+
return new SpringTestAspect();
20+
}
21+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.spring.service;
2+
3+
public class SpringSuperService {
4+
public String getInfoFromService(String code){
5+
System.out.println("Doing calculations");
6+
return code;
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<beans xmlns="http://java.sun.com/xml/ns/javaee"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
4+
http://java.sun.com/xml/ns/javaee/beans_1_2.xsd">
5+
<interceptors>
6+
<class>com.baeldung.interceptor.AuditedInterceptor</class>
7+
</interceptors>
8+
</beans>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.test;
2+
3+
import com.baeldung.spring.configuration.AppConfig;
4+
import com.baeldung.spring.service.SpringSuperService;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.test.context.ContextConfiguration;
9+
import org.springframework.test.context.junit4.SpringRunner;
10+
11+
import javax.inject.Inject;
12+
13+
@RunWith(SpringRunner.class)
14+
@ContextConfiguration(classes = {AppConfig.class})
15+
public class SpringTestInterceptor {
16+
@Inject
17+
SpringSuperService springSuperService;
18+
19+
@Test
20+
public void givenService_whenServiceAndAspectExecuted_thenOk(){
21+
String code = "123456";
22+
String result = springSuperService.getInfoFromService(code);
23+
Assert.assertEquals(code,result);
24+
}
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.test;
2+
3+
import com.baeldung.service.SuperService;
4+
import org.jboss.weld.environment.se.Weld;
5+
import org.jboss.weld.environment.se.WeldContainer;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
public class TestInterceptor {
10+
@Test
11+
public void givenTheService_whenMethodAndInterceptorExecuted_thenOK() {
12+
Weld weld = new Weld();
13+
WeldContainer container = weld.initialize();
14+
SuperService superService = container.instance().select(SuperService.class).get();
15+
String code = "123456";
16+
superService.deliverService(code);
17+
Assert.assertEquals("123456",code);
18+
}
19+
}

0 commit comments

Comments
 (0)