I want to inject a plain java object using Spring programmatically without using any xml configuration. Want to inject fields/methods annotated with tags Like @EJB, @PostConstruct etc. Is that possible? Thanks!
I want to inject a plain java object using Spring programmatically without using any
Share
Creating an ApplicationContext without XML (using AnnotationConfigApplicationContext)
With AnnotationConfigApplicationContext, you don’t need any XML at all. You create the Application context programatically and either
a) manually register annotated classes
b) or scan the classpath for annotated classes
If you use one of the convenience constructors
AnnotationConfigApplicationContext(Class<?> … annotatedClasses)
or
AnnotationConfigApplicationContext(String … basePackages)
the context is created and refreshed automatically, otherwise you need to call the refresh() method manually after adding the classes or packages.
Autowiring existing non-Spring beans (using AutowireCapableBeanFactory)
For autowiring an existing bean I think the preferred idiom is to use
Or, if you want more control, use
For further reference, see
existingBean, int autowireMode,
boolean dependencyCheck)