这里是我的弹簧配置文件和类: -弹簧自动装配不能与代理
我不能自动测试服务中的代理类。运行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();
}
}
谢谢你是正确的我忘了注释配置。它的工作现在 – Manoj 2010-12-03 13:17:13