我想知道是否可以将这段 xml 配置映射到 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:切入点 with
<aop:advisor id="managerTx" advice-ref="txAdvice"
pointcut="com.myapp.configuration.AspectConfig.serviceAnnotatedClass()" order="20"/>
and
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() {}
}
有什么提示如何替换其余的吗?
如果你根本不想使用任何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");
}
}
您可以将切入点与建议注释一起使用,如上所示。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)