【发布时间】:2014-08-14 14:18:48
【问题描述】:
所以我想要的是在配置文件处于活动状态时将特定的 Spring Aspect 应用到我的类,但我找不到解决方案,我尝试了 http://city81.blogspot.com/2012/05/using-spring-profile-with.html 中提出的方法,但它已经很老了并且不起作用就我而言,我有一个 Spring Started 项目进行测试,我根据链接执行以下操作:
配置应用程序:
@Configuration
@ComponentScan(basePackages= {
"demo",
"demo.aspect"
})
@EnableAutoConfiguration(exclude=AopAutoConfiguration.class)
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class Application {
@Bean
@Profile("asdasd") //testing profile to bean level
public TestAspect testAspect() { //this allow @autowired in my aspect
TestAspect aspect = Aspects.aspectOf(TestAspect.class);
return aspect;
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我的观点:
//TESTING IN ALL THIS WAYS BUT NOTHING
//@Component
//@Profile("asdasd")
@Configurable
//@Configuration
@Aspect
public class TestAspect{
public static final Logger LOGGER = LogManager.getLogger(testControllerEX.class);
@Autowired
private testService testService;
public TestAspect() {
LOGGER.info("TEST ASPECT INITIALIZED");
}
/*@Before("execution(* demo.testControllerEX.test(*)) && args(param)")
public void beforeSampleMethod(Object param) {
LOGGER.info("ASPECT" + param.getClass());
}*/
@Before("execution(demo.testControllerEX.new())")
public void constructor(JoinPoint point) {
LOGGER.info("ASPECT CONSTRUCTOR" + point.getThis().getClass().getAnnotation(Controller.class));
LOGGER.info("SERVICE" + testService);
}
@Around("execution(* demo.testControllerEX.testPrevent(*)) && args(param)")
public String prevent(ProceedingJoinPoint point, String param) throws Throwable{
//LOGGER.info("ASPECT AROUND" + param);
LOGGER.info("ASPECT AROUND " + testService);
String result = (String)point.proceed();
return result;
}
/*@DeclareParents(value="(demo.testControllerEX)",defaultImpl=testControllersssImpl.class)
private ITestControllerEX itestControllerEX;*/
}
最后我尝试将我的方面应用于控制器的构造函数,它可以工作,但我需要在配置文件处于活动状态时应用,我发现的另一个错误是我的 testService 在构造函数切入点之后初始化,所以它是null,但在 testPrevent 方法中,显然服务是在之前初始化的,我可以接受完成我想要的其他形式
编辑
我知道我的 testService 在我的构造函数切入点之前加载但仍然为空:
@Configuration
@ComponentScan(basePackages= {
"demo",
"demo.aspect"
})
@EnableAutoConfiguration(exclude=AopAutoConfiguration.class)
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class Application {
@Autowired
private testService testService;
...
【问题讨论】:
-
将@Profile("asdasd") 添加到TestAspect 类并删除testAspect @Bean 方法。
标签: java spring aspectj spring-aop spring-boot