我正在使用Spring4
以及Spring Boot
。注入的bean在使用AOP后变为空
在我厌倦了使用AOP之前,我在控制器中使用的Bean(CommandService)被自动注入,但是当我厌倦了使用AOP来收集一些调试消息后,这个bean就变成了空的!
这里是我的Application.java
@Configuration
@EnableAutoConfiguration
@ComponentScan({"hello","wodinow.weixin.jaskey"})
public class Application extends {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
LogUtil.info("Beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
LogUtil.info(beanName);
}
LogUtil.info("Application Boots completes!");
}
@Bean
public CommandService commandService(){
LogUtil.debug("CommandService.getInstance()"+ CommandService.getInstance()) ;//here indeed I could see spring executes this and returns a object when application boots
return CommandService.getInstance();//This returns a singleton instance
}
}
我的控制器,抛出空指针:
@Controller
public class CoreController {
@Autowired
CommandService commandService;//here the service is null after using aop
//...some request methods
}
我刚才添加的看点:
//if I comment out these two annoations, the bean will be auto injected well
@Aspect
@Component
public class LogAspect {
@Pointcut("execution(* wodinow.weixin.jaskey..*.*(..))")
private void debug_log(){};
@Around("debug_log()")
public void debug(ProceedingJoinPoint joinPoint) throws Throwable{
LogUtil.debug("enter "+joinPoint.getSignature());
try{
joinPoint.proceed();
LogUtil.debug("returns from "+joinPoint.getSignature());
}
catch(Throwable t){
LogUtil.error(t.getMessage()+"occurs in "+joinPoint.getSignature(),t);
throw t;
}
}
}
我是新来的春天,有人可以帮我吗?
您的建议不会返回任何内容。 – zeroflagL 2014-10-30 08:22:06
@zeroflagL,你什么意思不返回任何东西?我是否需要发布其他内容来描述问题? – Jaskey 2014-10-30 09:25:16
'commandService()'返回一个'CommandService' bean。当你使用AOP时,它不再被直接调用,而是调用“debug()”。而'debug()'不返回任何东西。那么你应该如何获得一个'CommandService'实例? – zeroflagL 2014-10-30 09:33:07