Reputation: 253
I know I can not use @Autowired annotation in a non managed spring class. But I notice I do able to get a bean instance inside a non managed spring class if I am retrieving it through ApplicationContext getBean method. can someone explain me why is that ? what is the different between the options ? in both spring doesn't know the non managed class
Upvotes: 1
Views: 5415
Reputation: 3658
ok, so here's the major points:
and that's how you can access this beans out of spring application using applicationContext.
another thing is that, actually you can inject beans into non managed beans through @Configurable. in this case the AspectJ would be used for making this work
Upvotes: 0
Reputation: 1701
1) You can use @Autowired
in a non-managed spring class if you use @Configurable
-with the usage of internal aspect weaving spring will manage to autowired the referenced beans into your "Configurable" class when it is constructed with the "new" operator. Here is an article how this works: http://www.javacodegeeks.com/2013/09/spring-configurable-magic.html
If a class is not configurable spring cannot notice when a new instance is created to autowire its references.
2) The ApplicationContext is "Central interface to provide configuration for an application." Here you have access to the whole spring managed beans etc. That's why you can get everything due to accessing it via ApplicationContext. http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/ApplicationContext.html
Upvotes: 3