I'm working with a Spring AOP tutorial designed to intercept and time method calls. It's using an XML configuration but my company framework utilizes a Java Configuration. Im new to Spring and could use some help converting this from xml config to java config
The spring.xml contents are as follows:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<!-- Enable AspectJ style of Spring AOP -->
<aop:aspectj-autoproxy />
<!-- Configure Employee Bean and initialize it -->
<bean name="employee" class="com.journaldev.spring.model.Employee">
<property name="name" value="Dummy Name"></property>
</bean>
<!-- Configure EmployeeService bean -->
<bean name="employeeService" class="com.journaldev.spring.service.EmployeeService">
<property name="employee" ref="employee"></property>
</bean>
<!-- Configure Aspect Beans, without this Aspects advices wont execute -->
<bean name="employeeAroundAspect" class="com.journaldev.spring.aspect.EmployeeAroundAspect" />
<bean name="employeeAnnotationAspect" class="com.journaldev.spring.aspect.EmployeeAnnotationAspect" />
<bean name="employeeXMLConfigAspect" class="com.journaldev.spring.aspect.EmployeeXMLConfigAspect" />
<!-- Spring AOP XML Configuration -->
<aop:config>
<aop:aspect ref="employeeXMLConfigAspect" id="employeeXMLConfigAspectID" order="1">
<aop:pointcut expression="execution(* com.journaldev.spring.model.Employee.getName())" id="getNamePointcut"/>
<aop:around method="employeeAroundAdvice" pointcut-ref="getNamePointcut" arg-names="proceedingJoinPoint"/>
</aop:aspect>
</aop:config>
</beans>
How would AspectJ style be enabled in java config?
XML block:
<!-- Enable AspectJ style of Spring AOP -->
<aop:aspectj-autoproxy />
Would this require simply importing the AspectJ libraries in the Java files?
How would Employee and Employee Service beans be initialized?
xml block:
<!-- Configure Employee Bean and initialize it -->
<bean name="employee" class="com.journaldev.spring.model.Employee">
<property name="name" value="Dummy Name"></property>
</bean>
<!-- Configure EmployeeService bean -->
<bean name="employeeService" class="com.journaldev.spring.service.EmployeeService">
<property name="employee" ref="employee"></property>
</bean>
<!-- Configure Aspect Beans, without this Aspects advices wont execute -->
<bean name="employeeAroundAspect" class="com.journaldev.spring.aspect.EmployeeAroundAspect" />
<bean name="employeeAnnotationAspect" class="com.journaldev.spring.aspect.EmployeeAnnotationAspect" />
<bean name="employeeXMLConfigAspect" class="com.journaldev.spring.aspect.EmployeeXMLConfigAspect" />
Possible Java Config block? unsure how to pass the property names and values - using an Application.config? Also it may be easier to put the beans into one config file?
package com.journaldev.spring.*;
import org.springframework.context.annotation.*;
@Configuration
public class EmployeeConfig {
@Bean
public Employee employee(){
return new Employee();
}
@Bean
public EmployeeService employeeService(){
return new EmployeeService();
}
@Bean
public EmployeeAroundAspect employeeAroundAspect(){
return new EmployeeAroundAspect();
}
@Bean
public EmployeeAnnotationAspect employeeAnnotationAspect(){
return new EmployeeAnnotationAspect();
}
@Bean
public EmployeeXMLConfigAspect employeeXMLConfigAspect(){
return new EmployeeXMLConfigAspect();
}
How would I configure Spring AOP XML block:
<!-- Spring AOP XML Configuration -->
<aop:config>
<aop:aspect ref="employeeXMLConfigAspect" id="employeeXMLConfigAspectID" order="1">
<aop:pointcut expression="execution(* com.journaldev.spring.model.Employee.getName())" id="getNamePointcut"/>
<aop:around method="employeeAroundAdvice" pointcut-ref="getNamePointcut" arg-names="proceedingJoinPoint"/>
</aop:aspect>
</aop:config>
</beans>
Java Config block:
Do I create a “ConfigAspect.java” with an “Around Advice object” and “ProceedingJoinPoint” interface that references the method in the AspectPointCut.java?
package com.journaldev.spring.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
public class ConfigAspect {
public Object AroundAdvice(ProceedingJoinPoint proceedingJoinPoint){
System.out.println("ConfigAspect:: Before invoking getName() method");
Object value = null;
try {
value = proceedingJoinPoint.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("ConfigAspect:: After invoking getName() method. Return value="+value);
return value;
}
}
Create a AspectPointCut.java file with the ‘Pointcut’ annotation similar to -
package com.journaldev.spring.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class AspectPointcut {
@Before("getNamePointcut()")
public void loggingAdvice(){
System.out.println("Executing loggingAdvice on getName()");
}
@Before("getNamePointcut()")
public void secondAdvice(){
System.out.println("Executing secondAdvice on getName()");
}
@Pointcut("execution(public String getName())")
public void getNamePointcut(){}
@Before("allMethodsPointcut()")
public void allServiceMethodsAdvice(){
System.out.println("Before executing service method");
}
//Pointcut to execute on all the methods of classes in a package
@Pointcut("within(com.journaldev.spring.service.*)")
public void allMethodsPointcut(){}
}