用⼀句更具体的话来概括Spring,那就是: Spring 是包含了众多工具方法的 IoC 容器,工具如StringUtils,容器装的是对象(Spring管理的对象称为bean,和javase的bean不一样)。而SpringBoot在Spring上进行了封装(Spring指的是Spring framework)。
public class Car {
private Framework framework;
public Car(){
this.framework = new Framework();
System.out.println("car init...");
}
public void run(){
System.out.println("car run...");
}
}
public class Framework {
private Bottom bottom;
public Framework() {
this.bottom = new Bottom();
System.out.println("framework init...");
}
}
public class Bottom {
private Tire tire;
public Bottom()
{
this.tire = new Tire();
}
}
public class Tire {
private int size = 20;
public Tire()
{
System.out.println("Tire()");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.run();
}
}
public class Car {
private Framework framework;
public Car(Framework framework){
this.framework = framework;
System.out.println("Car init...");
}
public void run(){
System.out.println("Car run...");
}
}
public class Framework {
private Bottom bottom;
public Framework(Bottom bottom) {
this.bottom = bottom;
System.out.println("framework init...");
}
}
public class Bottom {
private Tire tire;
public Bottom(Tire tire)
{
this.tire = tire;
System.out.println("bottom init...");
}
}
public class Tire {
private int size;
public Tire(int size)
{
System.out.println("tire init...");
}
}
public class Main {
public static void main(String[] args) {
Tire tire = new Tire(20);
Bottom bottom = new Bottom(tire);
Framework framework = new Framework(bottom);
Car car = new Car(framework);
car.run();
}
}
在 Spring 框架的设计中,方法注解 @Bean 要配合类注解才能将对象正常的存储到 Spring 容器中。
java复制代码
@Component
public class BeanConfig {
@Bean
public User user(){
User user = new User();
user.setName("zhangsan");
user.setAge(18);
return user;
}
}
不配合使用类注解会出现下面问题:
同一个类定义多个对象
java复制代码
@Component
public class BeanConfig {
@Bean
public User user1(){
User user = new User();
user.setName("zhangsan");
user.setAge(18);
return user;
}
@Bean
public User user2(){
User user = new User();
user.setName("lisi");
user.setAge(19);
return user;
}
}
定义了多个对象的话, 我们根据类型获取对象会报错:
java复制代码
@SpringBootApplication
public class SpringIocDemoApplication {
public static void main(String[] args) {
//获取Spring上下⽂对象
ApplicationContext context = SpringApplication.run(SpringIocDemoApplication.class, args);
//从Spring上下⽂中获取对象
User user = context.getBean(User.class);
//使⽤对象
System.out.println(user);
}
}
报错信息显示: 期望只有⼀个匹配, 结果发现了两个, user1, user2
从报错信息中, 可以看出来, @Bean 注解的bean, bean的名称就是它的方法名
应该根据名称来获取bean对象。
java复制代码
@SpringBootApplication
public class SpringIocDemoApplication {
public static void main(String[] args) {
//获取Spring上下⽂对象
ApplicationContext context = SpringApplication.run(SpringIocDemoApplication.class, args);
//根据bean名称, 从Spring上下⽂中获取对象
User user1 = (User) context.getBean("user1");
User user2 = (User) context.getBean("user2");
System.out.println(user1);
System.out.println(user2);
}
}
3.2 重命名Bean
通过设置 name 属性给 Bean 对象进⾏重命名操作(只有⼀个名称时, {}也可以省略)
java复制代码
@Bean(name = {"u1","user1"})
public User user1(){
User user = new User();
user.setName("zhangsan");
user.setAge(18);
return user;
}
使用u1 就可以获取到 User 对象了
java复制代码
@SpringBootApplication
public class SpringIocDemoApplication {
public static void main(String[] args) {
//获取Spring上下⽂对象
ApplicationContext context = SpringApplication.run(SpringIocDemoApplication.class, args);
//从Spring上下⽂中获取对象
User u1 = (User) context.getBean("u1");
//使⽤对象
System.out.println(u1);
}
}
@Component
public class BeanConfig {
@Primary //指定该bean为默认bean的实现
@Bean("u1")
public User user1(){
User user = new User();
user.setName("zhangsan");
user.setAge(18);
return user;
}
@Bean
public User user2() {
User user = new User();
user.setName("lisi");
user.setAge(19);
return user;
}
}
@Controller
public class UserController {
@Qualifier("user2") //指定bean名称
@Autowired
private User user;
public void sayHi(){
System.out.println("hi,UserController...");
System.out.println(user);
}
}
4.2.3 使用@Resource注解
是按照bean的名称进行注入。通过name属性指定要注入的bean的名称。
java复制代码
@Controller
public class UserController {
@Resource(name = "user2")
private User user;
public void sayHi(){
System.out.println("hi,UserController...");
System.out.println(user);
}
}