Spring autowiring not working with proxies
Here are my spring configuration files and classes:-
I am not able to autowire the proxied class in test Service. After running Test.java I am getting NullPointerException obviously property 'arthmeticCalculator' is not set.
I am not getting whats going wrong? Please help me to solve this problem.
<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) 开发者_JAVA技巧context.getBean("testService");
service.test();
}
}
Does it work without proxies?
Perhaps you need <context:annotation-config/>
.
You haven't included all you configuration. Autowiring is done by the BeanPostProcessor http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.html
You can enable this by <context:annotation-config/>
.
精彩评论