Spring 弹簧自动接线不与代理一起工作

Spring 弹簧自动接线不与代理一起工作,spring,Spring,以下是我的spring配置文件和类:- 我无法在测试服务中自动连接代理类。在运行Test.java之后,我得到了NullPointerException,显然属性“Arthmeticalculator”没有设置 我不明白怎么回事?请帮我解决这个问题 <bean id="arthmeticCalculator" class="com.manoj.aop.test.CalculatorImpl"/> <bean class="org.springframework.aop.fram

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

我无法在测试服务中自动连接代理类。在运行Test.java之后,我得到了NullPointerException,显然属性“Arthmeticalculator”没有设置

我不明白怎么回事?请帮我解决这个问题

<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>
CacculatorImpl:-

public class CalculatorImpl implements Calculator {

    public double add(double a, double b) {
        return a+b;
    }    
}
记录周围警告:-

public class LoggingAroundAdvice implements MethodInterceptor{


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

}
测试服务:-

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();
    }  
}

它在没有代理的情况下工作吗


也许您需要

您还没有包括所有配置。自动布线由BeanPostProcessor完成


您可以通过

启用此功能,谢谢您的回答,我忘记了注释配置。现在可以用了顺便问一下,和之间有什么区别吗?是的。指示Spring框架进行自动连接,检查@Required并处理JSR-250注释。这并不要求框架加载更多bean。另一方面,指示框架扫描给定包中新的spring托管组件(@Component、@Repository、@Service和@Controller)。当您不想在xml中配置这些托管bean时,通常使用这种方法。
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();
    }  
}