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)