Hi I am trying to understand autowiring in beans , I am able to autowire but still getting null values here is code snippets
Edited entire code with current output
@Component
public class A {
private String value;
private B b;
public void display() {
System.out.println(value);
System.out.println(b.m());
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public B getB() {
return b;
}
@Autowired
public void setB(B b) {
this.b = b;
}
}
B class
@Component
public class B {
private String b;
public void setB(String b) {
this.b = b;
}
public String m() {
return b;
}
}
configuration.java
@Service
@ComponentScan(basePackageClasses = A.class)
public class AppConfig {
@Autowired
public A setBean() {
A a = new A();
a.setValue("inside A");
return a;
}
@Autowired
public B setB() {
B b = new B();
b.setB("inside B");
return b;
}
}
Main
@ContextConfiguration(classes = AppConfig.class)
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext(
AppConfig.class);
A app = (A) configApplicationContext.getBean("a");
app.display();
}
}
Now the question is, I am supposed to get all values but instead of that I am getting null value from each bean , So why I am getting this value as null and how can I sort this?
Edit
the output is
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. null null
Edit After debugging it i found that , in config class it is passing values as specified but when it comes to display values in display()
it gives null