Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

AnnotationDrivenConfig in Spring 3.0 with @Configuration

 
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When using the @Configuration annotation on a Configuration class, the @AnnotationDrivenConfig needs to accompany the @Configuration annotation in order for the container to recognize the @Autowired annotation in other places of the code. There's a good tutorial on the topic here: http://www.basilv.com/psd/blog/2009/java-based-configuration-of-spring-dependency-injection

But I'm in Spring 3.0, and the AnnotationDrivenConfig object isn't in Spring 3.0? So, how do I set autowiring without using an XML file?

To enable the @Autowired, you have to register an ‘AutowiredAnnotationBeanPostProcessor’. You can register it in two ways.


To enable @Autowired, I believe there are two ways to do it in the xml file:


<context:annotation-config />
or
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

But how do you do it if you're not using XML, and Spring 3.0 doesn't have the handy @AnnotationDrivenConfig?


 
Cameron Wallace McKenzie
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nevermind.

Simply using the AnnotationConfigApplicationContext class in the @Configuration class is sufficient. I couldn't seem to get it to work with a constructor, but no problem with a setter method.


org.springframework.context.annotation.AnnotationConfigApplicationContext;


AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SimpleConfig.class);
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using <context:component-scan base-package="package structure here">

that tag includes <context:annotation-config/> so you won't need both in your xml files.

Mark
 
Cameron Wallace McKenzie
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read a post on TheServerSide.com by some handsome fellow that talked about getting rid of XML files, and that's exactly what I'm trying to do. I don't want any XML files in my Spring application. I spit on XML. Pffth!!!

So, without any XML files, there's nowhere to put <context:annotation-config/>

I was having problems, and I was reading a post that described how to do this in Spring 2.5. It used the following two annotations together:



However, when you create your beanFactory, or context, in Spring 3.0, you don't need the @AnnotationDrivenConfig. What you do instead is create an AnnotationConfigApplicationContext object, which acts as your beanFactory:



By the way, I now refer to this AnnotationConfigApplicationContext class simply as ACAC. It's much easier to write out.

It appears that this class is by default, capable of doing autowiring. You then just get your beans from this context as you normally would:



My SimpleConfig class is simply a POJO annotated with @Configuration



And the autowiring now works in the GameService, which is loaded up from the ACAC.


The problem was I had the @Autowired originally on the constructor, which would not work. I can put the @Autowired on the setter, or on the property itself, and all is fine, but it won't work on the constructor:


AUTOWIRING THE SETTER WORKS:


AUTOWIRING THE CONSTRUCTOR DOES NOT WORK



I'm still not sure of the rationale for allowing setters to be autowired but not constructors?

Mark Spritzler's Article on Spring Without XML





 
Mark Spritzler
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can do it code based where you have to use the ApplicationContext implementation from JavaConfig.

When I want to get rid of xml, I still have one xml file with one entry. <context:component-scan>



Mark
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic