Question

I can not find any reason why every autowired bean are not autowired by proxy. I know that becasue @Transactional annotations do not work and I checked autowired component during debugging in eclipse. Of course every component implements some interface and I use @Autowired annotations in relation to the interface. I have only one configuration of aop:

<tx:annotation-driven transaction-manager="transactionManager" />

I use JPA with hibernate, spring-mvc,spring-webflow, spring-security and spring-data. Interfaces which extends org.springframework.data.repository.CrudRepository are autowired by proxy. But my components are not. For example I have class MyClass which implement MyInterface:

@Service
public class MyClass implements MyInterface {
@Autowired
MyCrudReposiotry reposiotry;
....
}

If I autowire MyInterface somewhere:

@Autowired
MyInterface mi;

then mi is just reference to MyClass object, repository is refrence to proxy org.springframework.aop.framework.JdkDynamicAopProxy. Very interesting is that in testing mi is reference to proxy. My test's context does not contain web-flow and mvc configuration.

Maybe there is some indirect aop configuration which I should check. What can switch the autowiring by proxy off?

Was it helpful?

Solution

My guess is that you are scanning for the same components twice. You probably have a in your root context (for the ContextLoaderListener) and one for the DispatcherServlet. NO if the both scan for the same classes you end up with duplicated (and one proxied and one non proxied instance).

OTHER TIPS

Proxying and auto wiring are independent of each other. When you use @AutoWired it finds another bean that implements the required interface and injects it. The bean instance it finds might be a normal object or a proxy - it doesn't matter to Autowired.

Proxies are created for certain beans automatically by spring. As you have noticed one scenario in which this happens is when you use @Transactional. When the spring container instantiates a bean which has the @Transactional annotation the object gets wrapped in a proxy. The actual object is replaced by the proxy in the context. This is done so that spring can intercept calls to those methods and add the begin / commit transaction calls before and after the method call. This is implemented by the spring-aop module. Any feature that relies on AOP (@Transactional, @Secured) will result in creation of a proxy.

The other case where proxies are used is to create an implementation on the fly. In case of the CRUDRepository you are required to only implement the interface. The implementation of that is created on the fly using the same proxy infrastructure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top