⽅法注解 @Bean

类注解是添加到某个类上的,但是存在两个问题:

  1. 使⽤外部包⾥的类,没办法添加类注解

  2. ⼀个类,需要多个对象,⽐如多个数据源

这种场景,我们就需要使⽤⽅法注解@Bean

我们先来看看⽅法注解如何使⽤:

java 复制代码
public class BeanConfig {
 @Bean
 public User user(){
 User user = new User();
 user.setName("zhangsan");
 user.setAge(18);
 return user;
 }
}

然⽽,当我们写完以上代码,尝试获取bean对象中的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);
 }
}

以上程序的执⾏结果如下:

这是为什么呢?

1 ⽅法注解要配合类注解使⽤

在Spring框架的设计中,⽅法注解 @Bean 要配合类注解才能将对象正常的存储到Spring容器中, 如下代码所⽰:

java 复制代码
@Component
public class BeanConfig {
 @Bean
 public User user(){
 User user = new User();
 user.setName("zhangsan");
 user.setAge(18);
 return user;
 }
}

再次执⾏以上代码,运⾏结果如下:

2 定义多个对象

对于同⼀个类,如何定义多个对象呢?

⽐如多数据源的场景,类是同⼀个,但是配置不同,指向不同的数据源.

我们看下@Bean的使⽤

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);
 }
}

运⾏结果:

可以看到,@Bean可以针对同⼀个类,定义多个对象.

3 重命名 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);
 }

name={}可以省略,如下代码所⽰:

java 复制代码
@Bean({"u1","user1"})
public User user1(){
 User user = new User();
 user.setName("zhangsan");
 user.setAge(18);
 return user;
}

只有⼀个名称时,{}也可以省略,如:

java 复制代码
@Bean("u1")
public User user1(){
 User user = new User();
 user.setName("zhangsan");
 user.setAge(18);
 return user;
}
相关推荐
wangbing11257 分钟前
springboot应用关闭时触发动作
java·spring boot·后端
weixin_460443563 小时前
2026年新修订《网络安全法》实施:Java私有化考试系统如何做好SBOM、漏洞治理与安全升级?
java·开发语言·在线考试系统·企业培训考试系统·宏远培训考试系统·国企培训考试系统·国产化培训系统
海鸥-w6 小时前
什么是AOP
java
java资料站6 小时前
十一、Spring AI Alibaba · 高级 · 多智能体(Multi-agent)
人工智能·spring·microsoft
Cc.Y7 小时前
Java零基础入门:可变字符串与包装类:StringBuilder、StringBuffer 与 通讯录管理系统实战
java·开发语言·python
paopaokaka_luck7 小时前
基于springboot+vue的智慧家政管理系统(AI问答、 WebSocket实时沟通、ECharts运营分析、订单规则与服务追踪)
java·javascript·spring boot·数据分析·echarts·mybatis
sm_926787057 小时前
RFID 标签打印的技术实现要点与二次开发实践
java·大数据·前端·c++·编辑器
阿森_dev7 小时前
2027 届毕设选题怎么选:30 个 Java 方向选题的实现难度排序
java·开发语言·spring boot·毕业设计·课程设计
Sarvartha7 小时前
Lambda 表达式和函数式接口
java·开发语言
计算机毕业设计杰瑞7 小时前
【计算机毕设精品】基于SpringBoot的一体化教务与学业质量管理系统的设计与实现,附源码_增删改查_java项目_可完美运行部署_网站项目
java·spring boot·课程设计