tags:

views:

126

answers:

3

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!

+2  A: 

Yes, you can annotate any POJO with @Component, @Service, @Controller, or @Respository (depending on its responsibilities), and it becomes a spring bean. You just have to put this line into the applicationContext.xml:

<context:component-scan base-package="org.yourbasepackage" />

You can also use @PostConstruct and @PreDestroy instead of the xml init-method and destroy-method.

Update: There is a project called spring-javaconfig. Parts of it have become part of the core spring and you can see more details here. In short, it allows you to use java classes instead of xml for configuring your beans.

Bozho
What is the difference with <context:annotation-config> ?
Kartoch
spring-javaconfig docs - http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-java
Jon Freedman
yeah, I linked it, but perhaps not readable enough.
Bozho
`context:annotation-config` wouldn't work. It detects annotations on existing beans.
Bozho
+1  A: 

The documentation around annotation-based Spring configuration can be found here. You will need a minimal amount of XML, so that Spring knows to look for annotation-based configuration:

<context:component-scan base-package=".." />

Once that is done, you can use the @Autowired annotation to set up your beans.

Noel M
What is the difference with <context:component-scan> ?
Kartoch
Did you read the documentation? **annotation-config** *`<context:annotation-config/>` only looks for annotations on beans in the same application context in which it is defined.***component-scan***In the example above, the `com.acme package` will be scanned, looking for any `@Component`-annotated classes, and those classes will be registered as Spring bean definitions within the container.*
Jon Freedman
I fixed your answer.
Bozho
+7  A: 

Creating an ApplicationContext without XML

With AnnotationConfigApplicationContext, you don't need any XML at all. You create the Application context programatically and either

a) manually register annotated classes

appContext.register( MyTypeA.class,
                     MyTypeB.class,
                     MyTypeC.class );

b) or scan the classpath for annotated classes

appContext.scan(
    "com.mycompany.myproject.mypackagea",
    "com.mycompany.myproject.mypackageb"
)

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

For autowiring an existing bean I think the preferred idiom is to use

appContext.getAutowireCapableBeanFactory().autowireBean(existingBean)

Or, if you want more control, use

appContext.getAutowireCapableBeanFactory()
      .autowireBeanProperties(
          existingBean,
          autowireMode,
          // e.g. AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE 
          dependencyCheck
      ) 

For further reference, see

seanizer
This is what I want. Btw, if I created the object (not by Spring), can I use Spring to inject this object the dependencies after that?
codeplay
i think this one is too trivial, seems ctx.autowireBean(bean) and many more methods could do this.
codeplay
Ok, why the downvote? I can't see anything wrong with my answer...
seanizer
@codeplay: see my update about autowiring
seanizer
@seanizer: Thanks for your response! downvote? definitely not me
codeplay
@codeplay I didn't think it was you :-)
seanizer