added module cdiexample
This commit is contained in:
parent
5b28a3c19a
commit
644a7d23a4
|
@ -0,0 +1,52 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>cdiexample</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<properties>
|
||||
<spring.version>4.3.1.RELEASE</spring.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<!-- 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>
|
||||
<!-- 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>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>1.8.9</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -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 {}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.spring.service;
|
||||
|
||||
public class SpringSuperService {
|
||||
public String getInfoFromService(String code){
|
||||
System.out.println("Doing calculations");
|
||||
return code;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
<beans xmlns="http://java.sun.com/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
|
||||
http://java.sun.com/xml/ns/javaee/beans_1_2.xsd">
|
||||
<interceptors>
|
||||
<class>com.baeldung.interceptor.AuditedInterceptor</class>
|
||||
</interceptors>
|
||||
</beans>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue