Reputation: 487
Im fairly new to Java Spring IoC and here's my problem
I have a FactoryConfig class with all beans and annotation @Configuration and @ComponentScan written as below.
import org.springframwork.*
@Configuration
@ComponentScan(basePackages="package.name")
public class FactoryConfig {
public FactoryConfig() {
}
@Bean
public Test test(){
return new Test();
}
//And few more @Bean's
}
My Test class has a simple Print method
public class Test {
public void Print() {
System.out.println("Hello Test");
}
}
Now in my Main Class Ive created an ApplicationContentext of FactoryConfig. (I'm expecting all of my @Beans in Factory config will be initialised. However, it returns null when I access the Test class using @Autowired
My Main Class
public class Main {
@Autowired
protected static Test _autoTest;
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
ApplicationContext context =
new AnnotationConfigApplicationContext(FactoryConfig.class);
FactoryConfig config = context.getBean(FactoryConfig.class);
config.test().Print();
// _autoTest.Print(); <--- Im getting NULL Pointer Ex here
}
}
What is the correct way to @Autowire and use objects/beans? any clearer explanation would be much appreciated.
Upvotes: 0
Views: 11048
Reputation: 148
Spring does not manage your Main class, that's why you are getting Nullpointer Exception. Using ApplicationContext to load beans, you can get your beans and access Methods as you are already doing -
ApplicationContext context =
new AnnotationConfigApplicationContext(FactoryConfig.class);
FactoryConfig config = context.getBean(FactoryConfig.class);
config.test().Print();
Upvotes: 0
Reputation: 2102
The reason is that your Main
is not managed by Spring. Add it as bean in your configuration:
import org.springframwork.*
@Configuration
@ComponentScan(basePackages="package.name")
public class FactoryConfig {
public FactoryConfig() {
}
@Bean
public Test test(){
return new Test();
}
@Bean
public Main main(){
return new Main();
}
//And few more @Bean's
}
And then you can edit your main()
as follows:
public class Main {
@Autowired
protected Test _autoTest;
public static void main(String[] args) throws InterruptedException {
ApplicationContext context =
new AnnotationConfigApplicationContext(FactoryConfig.class);
Test test = context.getBean(Test.class);
Main main = context.getBean(Main.class);
test.Print();
main._autoTest.Print();
}
}
Upvotes: -1
Reputation: 840
Your class
public class Test {
public void Print() {
System.out.println("Hello Test");
}
}
is not visible to Spring. Try adding an appropriate annotation to it, like @Component.
Upvotes: -1
Reputation: 11022
Only beans managed by Spring can have @Autowire
annotations. Your main class is not managed by Spring: it's created by you and not declared in a Spring context: Spring doesn't known anything about your class, and doesn't inject this property.
You can just access in your main method the Test bean with :
context.getBean(Test.class).Print();
Usually, you get a "bootstrap" from the context, and call this bootstrap to start your application.
Moreover:
print
method, not Print
.Upvotes: 8