child bean

kx5bkwkv  于 2021-07-23  发布在  Java
关注(0)|答案(3)|浏览(245)

我在运行时面临一个问题。我有下面的springbean定义,当我启动tomcat服务器时,“student”被加载,但是子bean“address”属性没有被注入(我相信基本上找不到“address”bean),它的值总是空的,这就是为什么在运行时抛出npe。但是,如果我对“address”属性使用@autowired注解,而不是将其添加到xml中,那么它总是有效的。
如果我不使用@autowired,您能告诉我为什么在运行时找不到“address”吗?有什么解决方法可以让容器在加载“student”之前找到bean?
注:以下定义在两个不同的XML中。另外,我在启动过程中使用verbose记录类名,我看到在这两种情况下(有@autowired和没有@autowired)“com.test.address”类都被加载,这就是为什么我认为在@autowired的情况下,container能够通过检查类型来加载该对象

<bean id="student" class="com.test.Student">
    <property name="address" ref="address" />
</bean>

<bean id="address" class="com.test.Address">
    <property name="street" value="Street1" />
    <property name="state" value="State1" />
    <property name="country" value="Country1" />
</bean>

类别定义:

public class Student {

private Address address;

    public setAddress(Address address) {
    this.address = address;
    }
}

public class Address {

private String street;
private String state;
private String country;

public setStreet(String street) {
    this.street = street;
    }

public setState(String state) {
    this.state = state;
    }

public setCountry(String country) {
    this.country = country;
    }
}

谢谢

798qvoo8

798qvoo81#

您已经将address作为student bean的setter属性。所以在学生课堂上,你需要设置addrees,如下所示

public class Student {
    private Address address; // keep same name address as in bean
    public void setAddress(Address address) {
        this.address= address;
    }
}

在此之后,无论您在哪里使用student作为@autowired student,您都将获得地址。

brc7rcf0

brc7rcf02#

如果你不使用 @Autowired 你得装东西 applicationContext.xml .

public class ExploreSpringXmlApplication {
    public static void main(String[] args) {
        try (ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml")) {
            Student s = applicationContext.getBean(Student.class)

但是如果你想用 @Autowired 您可以使用 AnnotationConfigApplicationContext .

@Configuration
@PropertySource("classpath:application.properties")
@ComponentScan({"the packages of your beans"})
public class ExploreSpringApplication {
    public static void main(String[] args) {
        try (AnnotationConfigApplicationContext applicationContext =
                     new AnnotationConfigApplicationContext(ExploreSpringApplication.class)) {

或者如果您使用的是springboot:并且想要使用 @Autowired :

@SpringBootApplication
@ComponentScan({"the packages of your beans"})
public class ExploreSpringBootApplication {
    public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = 
SpringApplication.run(ExploreSpringBootApplication.class, args)) {
ckx4rj1h

ckx4rj1h3#

以下示例对我很有用(纯Spring,无Spring Boot):

public class Student {
    private Address address;

    public void setAddress(Address address) {
        this.address = address;
    }
}

public class Address {
    private String street;
    private String state;
    private String country;

    public void setStreet(String street) {
        this.street = street;
    }

    public void setState(String state) {
        this.state = state;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}

public class Main {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/example/spring.xml");
        Student s = ctx.getBean(Student.class);
        System.out.println(s);
        ctx.close();
    }
}

spring定义xml(放在 src/resources/example/spring.xml ):

<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
       xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="student" class = "example.Student">
        <property name="address" ref="address"/>
    </bean>
    <bean id="address" class="example.Address">
        <property name="street" value="Street1" />
        <property name="state" value="State1" />
        <property name="country" value="Country1" />
    </bean>
</beans>

所以这看起来像是配置上的错误,可能是springjar版本的冲突,等等——简言之,这是问题中没有提到的。
请注意,用xml配置bean确实过时了,真正的现代应用程序不应该真正使用这种方法。

相关问题