@Import、@ImportResource 注解
@Import、@ImportResource
@Import
该注解可以导入组件
在容器中自动创建出该类型的组件、默认组件的名字就是全类名
@ImportResource(原生配置文件引入)
该注解可以写在配置类上
可以导入配置文件
系统要求
Java 8+
Maven 3.6.6 +
创建Maven项目工程
引入 pom.xml 依赖
<!--1.导入父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
</parent>
<!--2.导入springBoot的Web场景-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<!--
SpringBoot项目默认打成jar包
传统web项目我们需要打成war包,放入tomcat中运行,springBoot项目我们可以导入一个插件,
在项目打成jar包的同时,还会顺带打包运行环境,所以只要直接运行jar包也可以访问项目
-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
创建实体类 pojo.User 类
package com.xiang.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* Created by IntelliJ IDEA.
* User: xiang
* Date: 2021/10/12 11:04
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private int age;
private String gender;
private double money;
private Dog dog;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", money=" + money +
'}';
}
}
创建实体类 pojo.Dao 类
package com.xiang.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* Created by IntelliJ IDEA.
* User: xiang
* Date: 2021/10/12 11:02
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dog {
private String name;
private int age;
private String gender;
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
'}';
}
}
新建 spring-config.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.xsd">
<bean id="list" class="com.xiang.pojo.User">
<property name="id" value="001"/>
<property name="name" value="xiang"/>
<property name="age" value="19"/>
<property name="gender" value="女"/>
<property name="money" value="99999.00"/>
</bean>
<bean id="dog" class="com.xiang.pojo.Dog">
<property name="name" value="小哈"/>
<property name="gender" value="女"/>
<property name="age" value="3"/>
</bean>
</beans>
application.yml 文件
spring:
main:
#Action:
#
#Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
allow-bean-definition-overriding: true # 意思是后来发现的bean会覆盖之前相同名称的bean
创建配置类 MyConfig 类
package com.xiang.config;
import com.xiang.pojo.Dog;
import com.xiang.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
/**
* Created by IntelliJ IDEA.
* User: xiang
* Date: 2021/10/12 11:07
*/
@Configuration//该注解表明这个类是一个配置类,效果等同于配置文件
@Import({User.class,Dog.class})
@ImportResource("classpath:spring-config.xml") //该注解是引入了一个原生的spring的配置文件
public class MyConfig {
/**
* private int id;
* private String name;
* private int age;
* private String gender;
* private double money;
*
* @return
*/
@Bean
//在spring容器中添加组件;方法名为组件的id;返回值类型为组件类型;返回值就是组件具体的实例
public User user() {
return new User(1, "向向", 20, "男", 10000.00,dog());
}
/**
* private String name;
* private int age;
* private String gender;
*
* @return
*/
@Bean("dog")
//id名也可以自定义,将id由dog改为erHa
public Dog dog() {
return new Dog("二哈", 2, "女");
}
}
主程序进行测试 MainApplication 类
package com.xiang;
import com.xiang.config.MyConfig;
import com.xiang.pojo.Dog;
import com.xiang.pojo.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
/**
* Created by IntelliJ IDEA.
* User: xiang
* Date: 2021/10/12 11:00
*/
/*这是主程序类,类名可以随意起,需要注意的是该类必须与其他类同包*/
@SpringBootApplication//说明这是一个springBoot应用
public class MainApplication {
//main方法是所有程序的入口
public static void main(String[] args) {
//将主程序跑起来,需要调用SpringApplication类的run方法
//参数:说明要将哪个主程序跑起来,传入参数
//返回值:IOC容器
ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);
//.getBeanNamesForType(User.class):该方法可以通过组件类型获取所有该类型的实例
String[] type = context.getBeanNamesForType(User.class);
for (String s : type) {
System.out.println(s);
}
/**
* 运行结果:
* com.xiang.pojo.User
* user
*/
System.out.println("/**************************************************************/");
Dog dog = context.getBean("dog", Dog.class);
User user = context.getBean("list", User.class);
System.out.println(dog);
System.out.println(user);
/**
* com.xiang.pojo.User
* user
*
* list
* Dog{name='小哈', age=3, gender='女'}
* User{id=1, name='xiang', age=19, gender='女', money=99999.0}
*/
}
}
运行结果
com.xiang.pojo.User
user
list
/**************************************************************/
Dog{name='小哈', age=3, gender='女'}
User{id=1, name='xiang', age=19, gender='女', money=99999.0}