我正在使用Spring AOP教程,该教程旨在拦截和计时方法调用。它使用XML配置,但是我的公司框架使用Java配置。我是Spring的新手,可以使用一些帮助将其从xml配置转换为Java配置
spring.xml的内容如下:
<?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>
如何在Java配置中启用AspectJ样式?
XML块:
<!-- Enable AspectJ style of Spring AOP -->
<aop:aspectj-autoproxy />
这是否仅需要在Java文件中导入AspectJ库?
如何初始化Employee和Employee Service bean?
xml块:
<!-- 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" />
可能的Java Config块?不确定如何传递属性名称和值-使用Application.config?另外,将bean放入一个配置文件中可能会更容易?
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();
}
如何配置Spring AOP XML块:
<!-- 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块:
我是否使用引用AspectPointCut.java中的方法的“ Around Advice对象”和“ ProceedingJoinPoint”接口创建“ ConfigAspect.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;
}
}
使用类似于-
的“ Pointcut”注释创建AspectPointCut.java文件。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(){}
}