@Import、@ImportResource注解@Import、@ImportResource
@Import
该注解可以导⼊组件
在容器中⾃动创建出该类型的组件、默认组件的名字就是全类名
@ImportResource(原⽣配置⽂件引⼊)
该注解可以写在配置类上
可以导⼊配置⽂件
系统要求
Java 8+
Maven 3.6.6 +
创建Maven项⽬⼯程
引⼊ l 依赖
<!--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 + '\'' +
'}';
}
}
新建 l ⽂件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans
/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>
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 fig;
import com.xiang.pojo.Dog;
import com.xiang.pojo.User;
import t.annotation.Bean;
import t.annotation.Configuration;
import t.annotation.Import;
import t.annotation.ImportResource;
/**
* Created by IntelliJ IDEA.
* User: xiang
* Date: 2021/10/12 11:07
*/
@Configuration//该注解表明这个类是⼀个配置类,效果等同于配置⽂件
@Import({User.class,Dog.class})
@ImportResource("l") //该注解是引⼊了⼀个原⽣的spring的配置⽂件public class MyConfig {
/**
* private int id;
* private String name;
spring framework组件* 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 fig.MyConfig;
import com.xiang.pojo.Dog;
import com.xiang.pojo.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import t.ConfigurableApplicationContext;
import nv.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 = BeanNamesForType(User.class);
for (String s : type) {
System.out.println(s);
}
/**
* 运⾏结果:
* com.xiang.pojo.User
* user
*/
System.out.println("/**************************************************************/");        Dog dog = Bean("dog", Dog.class);
User user = Bean("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}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。