简体   繁体   中英

Spring constructor injection in bean. Is it a bean anymore?

I am learning spring framework and have a very basic question. I tried to find the answer, but couldn't find it, so bear with me. I have seen the following kind of wiring(it that is what it is called) in spring.

public class A {

    private B b;

    public A(B b) {
        this.b = b;
    }

    public B getB() {
        return b;
    }

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


public class B {

    private String foo;

    public String getFoo() {
        return foo;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }
}

So I understand that this autowiring is done using constructor injection. Then in the context.xml I have the following

    <bean id="a" class="A" autowire="constructor">
    </bean>

    <bean id="b" class="B" >
        <property name="foo" value="foo1" />
    </bean>

(I am learning the configuration using annotation rather than context.xml , but using it here since it seems to provide a more clear picture). So my question is, since a bean, by definition, should have only no-args constructors and getters and setters, doesn't doing a constructor injection, disqualify it from being a bean? What obvious thing am I missing here?

Bean is a loaded term. While the JavaBean specification did at least at one point require a no-args constructor, this does not mean Spring beans do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM