组件依赖(@Configuration)
定义
一个组件中调用了另一个组件。
使用场景
如果有其他对象依赖此组件的话,proxyBeanMethods设置为true,没有其他对象依赖的话proxyBeanMethods设置为false
-
false的话 ,创建出来的bean不是代理bean,springboot每次运行时不会检查容器中是否有此对象,直接启动,运行较快
-
true 的话 , 创建出来的bean是代理bean,springboot会检查容器中是否有此对象,有的话不再创建,默认为true
-
如果是代理bean的话,通过MyConfig组件每次创建出来是同一个对象
-
不是代理bean的话,通过MyConfig组件每次创建是新的对象
Pet实体类
public class Pet {
private String name;
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 "Pat{" +
"name='" + name + '\'' +
'}';
}
}
User实体类
public class User {
private String name;
private Integer age;
private Pet pet;
public Pet getPet() {
return pet;
}
public void setPet(Pet pet) {
this.pet = pet;
}
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;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", pat=" + pet +
'}';
}
}
配置类
import com.atguigu.boot.bean.Pet;
import com.atguigu.boot.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 配置类 proxyBeanMethods表示是不是代理bean
* 如果是代理bean的话,通过MyConfig组件每次创建出来是同一个对象
* 不是代理bean的话,通过MyConfig组件每次创建是新的对象
* false springboot每次运行时不会检查容器中是否有此对象,直接启动,运行较快
* true springboot会检查容器中是否有此对象,默认为true
* 如果有其他对象依赖此组件的话,设置为true,没有其他对象依赖的话设置为false
*/
@Configuration(proxyBeanMethods = false)
public class MyConfig {
/**
* 声明这个类是一个bean
* 方法名就是bean 的 id
* @return bean的实例
*/
@Bean("zhangsan")
public User user01() {
User user = new User("张三", 18);
user.setPet(pat01());
return user;
}
@Bean
public Pet pat01() {
return new Pet("tomcat");
}
}
启动类
import com.atguigu.boot.bean.Pet;
import com.atguigu.boot.bean.User;
import com.atguigu.boot.config.MyConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
/**
* 这是一个SpringBoot应用
*/
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
//创建一个IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
User tom = run.getBean("zhangsan", User.class);
User tom2 = run.getBean("zhangsan", User.class);
//单例bean,所以两个user是同一个bean
System.out.println("通过IOC创建的两个用户是不是同一个用户:" + (tom == tom2));
//判断两个user是不是一个bean
MyConfig bean = run.getBean(MyConfig.class);
User user = bean.user01();
User user1 = bean.user01();
System.out.println("通过MyConfig两次创建的用户是不是同一个用户:" + (user == user1));
//判断user里面的pet 和pet是不是同一个bean
User zhangsan = run.getBean("zhangsan", User.class);
Pet tom1 = run.getBean("pat01", Pet.class);
System.out.println("用户的宠物 和宠物是不是同一个宠物:" + (zhangsan.getPet() == tom1));
}
}
proxyBeanMethods为 false 时
通过IOC创建的两个用户是不是同一个用户:true
通过MyConfig两次创建的用户是不是同一个用户:false
用户的宠物 和宠物是不是同一个宠物:false
proxyBeanMethods为 true 时
通过IOC创建的两个用户是不是同一个用户:true
通过MyConfig两次创建的用户是不是同一个用户:true
用户的宠物 和宠物是不是同一个宠物:true