the mountain the mountain - 4 months ago 18
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

@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

Answer

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.

Comments