我想知道我是否可以将这段xml-configuration映射到Spring JavaConfig:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
default-autowire="byName">
<aop:config>
<aop:pointcut id="serviceAnnotatedClass" expression="@within(org.springframework.stereotype.Service)" />
<aop:advisor id="managerTx" advice-ref="txAdvice" pointcut-ref="serviceAnnotatedClass" order="20" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="load*" read-only="true" />
<tx:method name="is*" read-only="true" />
<tx:method name="ownTransaction*" propagation="REQUIRES_NEW" rollback-for="Exception" />
<tx:method name="*" rollback-for="Exception" />
</tx:attributes>
</tx:advice>
</beans>
到目前为止,我想出了如何用
替换 aop:pointcut<aop:advisor id="managerTx" advice-ref="txAdvice"
pointcut="com.myapp.configuration.AspectConfig.serviceAnnotatedClass()" order="20"/>
和
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class AspectConfig
{
@Pointcut("@within(org.springframework.stereotype.Service)")
public void serviceAnnotatedClass() {}
}
任何提示如何更换其余部分?
答案 0 :(得分:5)
目前,无法将所有基于XML的AspectJ设置转换为基于Java的配置。可能它永远不会。主要原因是Java不支持方法文字。但是有一种解决方法,首先在这里提出:https://jira.springsource.org/browse/SPR-8148
- 使用
包含相关的XML代码段,继续使用<aop:config>
@ImportResource
- 将任何现有的
醇><aop:config>
元素转换为使用@Aspect
样式。
参考documentation,我会说你已经完成了上面描述的配置。你只需要改变你的配置:
<aop:config>
<aop:advisor id="managerTx" advice-ref="txAdvice" pointcut="com.myapp.configuration.AspectConfig.serviceAnnotatedClass()" order="20" />
</aop:config>
保持原样并导入该资源:
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
@ImportResource("classpath:/aop-config.xml")
public class AspectConfig
{
@Pointcut("@within(org.springframework.stereotype.Service)")
public void serviceAnnotatedClass() {}
}
我希望我能帮忙......
答案 1 :(得分:5)
如果您根本不想使用任何xml,那么您可以为方面创建单独的Java配置类
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "com.myAspects")
public class AspectConfig {
//Here you can define Aspect beans or just use @ComponentScan (as above)
//to scan the @Aspect annotation in com.myAspects package
}
并在主AppConfig类中导入上面的配置类
@Configuration
@EnableWebMvc
@Import({ AspectConfig.class })
@ComponentScan(basePackages = { "pkg1", "pkg2", "pkg3" })
public class AppConfiguration extends WebMvcConfigurationSupport {
//Other configuration beans or methods
}
现在创建方面bean
import com.myAspects;
@Component
@Aspect
public class LoggingAspect {
@Before("execution(* com.service.*.*(..))")
public void logBefore(){
System.out.println("before advice called");
}
@After("execution(* com.service.*.*(..))")
public void logAfter(){
System.out.println("after advice called");
}
}
您可以使用切入点和建议注释,如上所示。