the mountain the mountain - 5 months ago 30
Java Question

getting null values from bean

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

public class A {
private String value;
private B b;

public void display() {

public String getValue() {
return value;

public void setValue(String value) {
this.value = value;

public B getB() {
return b;

public void setB(B b) {
this.b = b;


B class

public class B {

private String b;

public void setB(String b) {
this.b = b;

public String m() {
return b;


@ComponentScan(basePackageClasses = A.class)
public class AppConfig {

public A setBean() {
A a = new A();
a.setValue("inside A");
return a;

public B setB() {
B b = new B();
b.setB("inside B");
return b;


@ContextConfiguration(classes = AppConfig.class)
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext(
A app = (A) configApplicationContext.getBean("a");


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?


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 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
it gives null


Possible causes of your receiving null values:

  1. @Component should be written with a capital letter and the proper annotation from the Spring package must be imported;

  2. Class 'ApCfg' should look like this:

    @Configuration // this annotation is needed by Spring to know it is a config class
    @ComponentScan(basePackages = { "java" }) // you have to add the package NAMES here, not a regex
    public class ApCfg {
    @Bean // here, you're telling Spring to register a bean in its container, so you have to mark it as so, by using the @Bean annotation
    public A getA() {
        A a = new A(); // constructors are methods too, so they need to be called, using paranthesis
        a.setAValue("asdf"); // setters usually return void, so you shouldn't be able to return this
        return a; // this instance here is what will be registered in the container

If you've annotated class A with @Component, then you shouldn't be able to register it twice, so you should choose a way - either use the @Component annotation, or define it manually in the Config class in a method annotated by @Bean.

Also, I tend to agree that your choice of package name is rather peculiar. Are you certain that the package definition at the top of your classes is:

package java;


If you don't have any statement starting with the word package, then all you're classes are in the default package and, in that case, I strongly recommend grouping them in real packages instead.