Spring自动装配不使用代理

时间:2010-12-03 12:57:05

标签: spring

以下是我的spring配置文件和类: -

我无法在测试服务中自动装配代理类。运行Test.java后,我得到NullPointerException,显然属性'arthmeticCalculator'未设置。

我没有弄错了什么?请帮我解决这个问题。

<bean id="arthmeticCalculator" class="com.manoj.aop.test.CalculatorImpl"/>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="beanNames">
        <list>
        <value>*Calculator</value>
        </list>
  </property>
  <property name="interceptorNames">
        <list>
        <value>methodNameAdvisor</value>
        </list>
  </property>
</bean>
<bean id="methodNameAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
    <property name="mappedNames">
        <list>
            <value>add</value>
            <value>sub</value>
        </list>
    </property>
    <property name="advice" ref="loggingAroundAdvice" />
</bean>
<bean id="loggingAroundAdvice" class="com.manoj.aop.test.LoggingAroundAdvice"/>
<bean id="testService" class="com.manoj.aop.test.TestService">

</bean>

Calculator.java: -

public interface Calculator {

    public double add(double a,double b);
}

CacculatorImpl: -

public class CalculatorImpl implements Calculator {

    public double add(double a, double b) {
        return a+b;
    }    
}

LoggingAroundAdvice: -

public class LoggingAroundAdvice implements MethodInterceptor{


    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("Around Invoice called");
        Object result = methodInvocation.proceed();
        return result;
    }

}

TestService的: -

public class TestService {

    @Autowired
     private Calculator  arthmeticCalculator;


     public void test(){
         System.out.println(arthmeticCalculator.add(5, 10.5));
     }  
}

Test.java: -

public class Test {    
    /**
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("com/manoj/aop/test/aop.xml");
        TestService service = (TestService) context.getBean("testService");
        service.test();
    }  
}

2 个答案:

答案 0 :(得分:2)

没有代理可以吗?

也许你需要<context:annotation-config/>

答案 1 :(得分:0)

您尚未包含所有配置。自动装配由BeanPostProcessor http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.html

完成

您可以<context:annotation-config/>启用此功能。