SpringBoot underlying annotation @ImportResource imports Spring configuration files

Entity class:

@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 configuration file:

<?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>

Configuration class:

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

Start class test:

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

Lei Fengyang 2021 version of SpringBoot2 zero-based entry springboot full set of full version (spring boot2)

 

 

Guess you like

Origin blog.csdn.net/qq_30398499/article/details/113722387
Recommended