作用
通过@Import直接将指定类作为组件给容器注入
编码实现
MyConfig.java
package com.zwy.boot.config;
import ch.qos.logback.core.db.DBHelper;
import com.zwy.boot.bean.Pet;
import com.zwy.boot.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* 1.配置类里面使用@Bean标注在方法上,给容器注册组件,默认也是单实例的
* 2.配置类本身也是组件
* 3.proxyBeanMethods: 代理bean的方法
* Full(proxyBeanMethods = true) 默认,调用多次方法,只产生一个对象
* Lite(proxyBeanMethods = false) 每次调用这些方法,都会产生一个新的对象
* 4.@Import({})
* 给容器中自动创建出这两个类型的组件,组件名字默认是全类名
* @author zwy
*/
@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = true)
public class MyConfig {
/**
* @Bean 给容器中添加组件,以方法名作为组件的名字。
* 返回类型就是组件的类型,返回的值,就是组件在容器中的实例
* 外部无论对配置类中这个组件注册方法调用多少次,获取的都是之前注册容器中的单实例对象
* @return User
*/
@Bean
public User user01() {
User zhangsan = new User("zhangsan", 18);
// User组件依赖了Pet组件
zhangsan.setPet(tomcatPet());
return zhangsan;
}
// 自定义组件名字"tom"
@Bean("tom")
public Pet tomcatPet() {
return new Pet("tomcat");
}
}
MainApplication.java
package com.zwy.boot;
import ch.qos.logback.core.db.DBHelper;
import com.zwy.boot.bean.Pet;
import com.zwy.boot.bean.User;
import com.zwy.boot.config.MyConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
/**
* 主程序类; 主配置类
* 注解@SpringBootApplication:这是一个springboot应用
* @author zwy
*/
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
// 1.返回IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
// 2.查看容器里面的组件
String[] names = run.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
System.out.println("======================");
// 3.从容器中获取组件,注册
Pet pet1 = run.getBean("tom", Pet.class);
Pet pet2 = run.getBean("tom", Pet.class);
System.out.println("组件:" + (pet1 == pet2));
MyConfig bean = run.getBean(MyConfig.class);
System.out.println(bean);
User user1 = bean.user01();
User user2 = bean.user01();
System.out.println(user1 == user2);
// 4.验证proxyBeanMethods=false时,用户的宠物是不是容器中的宠物?
User user01 = run.getBean("user01", User.class);
Pet tom = run.getBean("tom", Pet.class);
System.out.println("用户的宠物:" + (user01.getPet() == tom));
// 5.获取组件
String[] beanNamesForType = run.getBeanNamesForType(User.class);
System.out.println("=====================");
for (String s : beanNamesForType) {
System.out.println(s);
}
DBHelper bean1 = run.getBean(DBHelper.class);
System.out.println(bean1);
}
}
另外有:
Pet.java
package com.zwy.boot.bean;
/**
* 宠物
* @author zwy
*/
public class Pet {
private String name;
public Pet() {
}
public Pet(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Pet{" +
"name='" + name + '\'' +
'}';
}
}
User.java
package com.zwy.boot.bean;
/**
* 用户
* @author zwy
*/
public class User {
private String name;
private Integer age;
private Pet pet;
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Pet getPet() {
return pet;
}
public void setPet(Pet pet) {
this.pet = pet;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", pet=" + pet +
'}';
}
}
文件结构
文件结构如下:
运行
运行结果有:
参考
https://blog.csdn.net/tuoni123/article/details/80213050
|