spring - java - 如何用Java配置替换spring.xml配置?
问题描述
我正在使用旨在拦截和计时方法调用的 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 config 中启用 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 配置块:
我是否创建了一个带有“Around Advice 对象”和“ProceedingJoinPoint”接口的“ConfigAspect.java”,该接口引用了 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;
}
}
创建一个带有“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(){}
}
解决方案
推荐阅读
- asciidoctor - 删除 asciidoctor 词干块中的高行距
- c# - 复制 ReadOnlySequence 的更好方法
到一个结构 - angular - 有没有办法通过使用 rxjs 运算符重试来获取角度的 HTTP 状态代码?
- javascript - 选择复选框时如何克隆元素
- html - 如何使用 CSS 在 Bootstrap 中更改某些样式
- variables - 为什么不能在块中更改全局变量?
- vue.js - BootstrapVue 模态在第一次单击时未显示,但在第二次单击时工作正常
- visual-studio-code - VS Code 终端在执行完成之前不会显示打印语句输出
- java - 如何在 ArchUnit 规则中排除一个类?
- json - JavaScript:访问嵌套的 JSON 对象