SpringBoot底层注解@ImportResource导入Spring配置文件

实体类:

@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Pat {
    private String name;
    private Integer age;
}

@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private String name;
    private Integer age;
    private Pat pat;

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}

spring配置文件:

<?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.xsd">

    <bean id="hello" class="com.itcast.bean.User">
        <property name="name" value="张三"></property>
        <property name="age" value="18"></property>
    </bean>

    <bean id="world" class="com.itcast.bean.Pat">
        <property name="name" value="小白" ></property>
        <property name="age" value="10" ></property>
    </bean>
</beans>

配置类:

// 导入上面的xml配置文件
@ImportResource("classpath:beans.xml")
public class MyConfig {
    // ...
}

启动类测试:

boolean hello = run.containsBean("hello");
boolean world = run.containsBean("world");
System.out.println("hello:" + hello);// true
System.out.println("world:" + world);// true

雷丰阳2021版SpringBoot2零基础入门springboot全套完整版(spring boot2)

猜你喜欢

转载自blog.csdn.net/qq_30398499/article/details/113722387