Spring AOP 遇到IOC注解时不起作用

jlspyaozhongkai 发布于 2014/10/10 20:53
阅读 1K+
收藏 0
接口类:
public interface IHelloWorld {
public String Go();

public void SetData(String data);
public String GetData();
}
业务类:
package com.laoyao.springmvc.test;


import org.springframework.stereotype.Component;


@Component("CHelloWorldId")
public class CHelloWorld implements IHelloWorld {


@Override
public String Go() {
System.out.println("------- CHelloWorld --------");
return "CHelloWorld";
}


@Override
public void SetData(String data) {
// TODO Auto-generated method stub

}


@Override
public String GetData() {
// TODO Auto-generated method stub
return null;
}
}
切面类:
package com.laoyao.springmvc.test;


import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.ProceedingJoinPoint;  
import org.aspectj.lang.annotation.After;  
import org.aspectj.lang.annotation.Around;  
import org.aspectj.lang.annotation.Aspect;  
import org.aspectj.lang.annotation.Before;  
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


@Component("TestAspect")
@Aspect
public class TestAspect {

    @Pointcut("execution(* com.laoyao.springmvc.test.*.*(..))")
    private void pcut(){}

    @Before(value = "pcut()")
    public void doBefore() {  
        System.out.println("************************************");  
    } 
}
测试代码:
@Controller
@RequestMapping("spring")
class TestControllr {


@RequestMapping("test")
public String test1(HttpServletRequest request) {
WebApplicationContext context = RequestContextUtils.getWebApplicationContext(request);

String tmp = null;
IHelloWorld hw = null;

System.out.println("-------------------------------");
hw = (IHelloWorld)context.getBean("CHelloWorldId");
tmp = hw.Go();
System.out.println(tmp);

return "TestOutPut";
}
}
配置:
<context:component-scan base-package="com.laoyao.springmvc.test"/>
<aop:aspectj-autoproxy/>
测试的时候通知代码调用不到
-------------------------------
------- CHelloWorld --------
CHelloWorld


如果增加配置:
<bean id="CHelloWorld" class="com.laoyao.springmvc.test.CHelloWorld"/>
注释掉业务代码中的注解
//@Component
-------------------------------
************************************
------- CHelloWorld --------
CHelloWorld


通知的代码就可以被调用到,求解。
加载中
返回顶部
顶部