• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How @Autowired works with JavaConfig

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am learning Spring Annotation and I want to know how each annotation works similair to the xml configuration.
Anyway, I want to use Spring without any xml file. So I started to use the JavaConfig.

In order to tell Spring, that we are using JavaConfig, we are using @Configuration.

Now I am just studying how the @Bean and @Autowired are working.

@Bean is for defining the bean which is similiar to the xml coniguration <bean></bean>

Below is my sample code, which is working fine for testing @Configuration and @Bean:



Output:
In Greeting Constructor
obj3 (com.example.service.Greeting@3043fe0e)
Hello Greeting Message


Now I want to use @Autowired in the above example:



Error: Not working!!!

Oct 15, 2016 2:49:04 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@439f5b3d: startup date [Sat Oct 15 02:49:04 SGT 2016]; root of context hierarchy
Oct 15, 2016 2:49:05 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7d417077: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor,anotherConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,getGreet]; root of factory hierarchy
Exception in thread "main" obj3 (null)
java.lang.NullPointerException
at com.example.HelloApp.main(HelloApp.java:12)


I have googled and in one the example they have mentioned @AnnotationDrivenConfig is required for @Autowired to work, (http://www.basilv.com/psd/blog/2009/java-based-configuration-of-spring-dependency-injection)
I am using Spring 3.2, but @AnnotationDrivenConfig is not resolving and it seems not available?!!

So, I have tried other option of ComponentScan:


Output: Working with construtor injection

In Greeting Constructor
obj3 (com.example.service.Greeting@7a9273a8)
Hello Greeting Message


But why same thing not working with property setter?

Error:
Oct 15, 2016 2:54:41 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@439f5b3d: startup date [Sat Oct 15 02:54:41 SGT 2016]; root of context hierarchy
Oct 15, 2016 2:54:42 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1134affc: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor,anotherConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,greeting,getGreet]; root of factory hierarchy
In Greeting Constructor
obj3 (null)
Exception in thread "main" java.lang.NullPointerException
at com.example.HelloApp.main(HelloApp.java:12)

 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Autowiring is used to inject bean which you create. In this case you are not creating any instance of the Greeting class (there is no new Greeting() getting called). Spring will not automatically create an object of greeting class and inject it for you. You should be using it something like this:

 
Ranch Hand
Posts: 285
2
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit Garg. Your answer above also helps me. But what if we want more objects to be created and given to Spring container ? So that they can Autowired in other related pojos ? Can we specify a package of more java classes to be created as well instead creating each one ?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic