使用说明

这个注解主要用在方法上,声明当前方法体中包含了最终产生 bean 实例的逻辑,方法的返回值是一个 Bean。这个 bean 会被 Spring 加入到容器中进行管理,默认情况下 bean 的命名就是使用了 bean 注解的方法名。@Bean 一般和 @Component 或者 @Configuration 一起使用。

@Bean 显式声明了类与 bean 之间的对应关系,并且允许用户按照实际需要创建和配置 bean 实例。

该注解相当于:

<bean id="useService" class="com.test.service.UserServiceImpl"/>

普通组件

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class MyConfigration {
@Bean
public User user() {
return new User;
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class UserController {
@Autowired
User user; @GetMapping("/test")
public User test() {
return user.test();
}
}

命名组件

bean 的命名为:user,别名为:myUser,两个均可使用

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class MyConfigration {
@Bean(name = "myUser")
public User user() {
return new User;
}
}

bean 的命名为:user,别名为:myUser / yourUser,三个均可使用

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class MyConfigration {
@Bean(name = {"myUser", "yourUser"})
public User user() {
return new User;
}
}

Bean 初始化和销毁

public class MyBean {
public void init() {
System.out.println("MyBean初始化...");
} public void destroy() {
System.out.println("MyBean销毁...");
} public String get() {
return "MyBean使用...";
}
}
@Bean(initMethod="init", destroyMethod="destroy")
public MyBean myBean() {
return new MyBean();
}

只能用 @Bean 不能使用 @Component

@Bean
public OneService getService(status) {
case (status) {
when 1:
return new serviceImpl1();
when 2:
return new serviceImpl2();
when 3:
return new serviceImpl3();
}
}

参考:https://www.hangge.com/blog/cache/detail_2506.html

https://www.jianshu.com/p/2f904bebb9d0

https://www.cnblogs.com/feiyu127/p/7700090.html

https://www.jdon.com/50889

Spring @Bean 注解的使用的更多相关文章

  1. spring @bean注解

    1.@bean注解用于注册一个bean到 到ioc容器中.类似于@component注解 2.@configure注解,相当于指明这个类是配置文件 3.@bean还可以指定initMethod,des ...

  2. spring @Bean注解的使用

    @Bean 的用法 @Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里.添加的bean的id为方法名 定义bean 下面是@Co ...

  3. Spring @Bean注解 (基于java的容器注解)

    基于java的容器注解,意思就是使用Java代码以及一些注解,就可以取代spring 的 xml配置文件. 1-@Configuration & @Bean的配合 @Configuration ...

  4. spring @Bean注解解释

    解释:java config配置一个重要注解 @Bean明确地指示了一种方法,什么方法呢——产生一个bean的方法,并且交给Spring容器管理:从这   我们就明白了为啥@Bean是放在方法的注释上 ...

  5. Spring bean注解配置(1)

    Spring自带的@Component注解及扩展@Repository.@Service.@Controller,如图 在使用注解方式配置bean时,需要引进一个包: 使用方法: 1.为需要使用注解方 ...

  6. spring @bean 的理解

    1.spring @bean 注解只能注解到方法上 2. 该方法必须返回一个实例对象 3.该过程相当于,通过一个方法去构造一个实例对象 ,然后交给spring管理 4.使用场景   如需要构造出一个特 ...

  7. spring IOC装配Bean(注解方式)

    1 Spring的注解装配Bean (1) Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean (2) Spring的框架中提供了与@Componen ...

  8. Spring 框架 详解 (四)------IOC装配Bean(注解方式)

    Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component  描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注 ...

  9. spring利用注解来注册bean到容器

    1.spring利用注解来定义bean,或者利用注解来注册装配bean.包括注册到ioc中,装配包括成员变量的自动注入. 1.spring会自动扫描所有类的注解,扫描这些注解后,spring会将这些b ...

随机推荐

  1. spring注解-属性

    一.@Value 基本数值 可以写SpEL: #{} 可以写${}取出配置文件[properties]中的值(在运行环境变量里面的值) @Value("张三") private S ...

  2. CentOS 初体验三: Yum 安装、卸载软件

    一:Yum 简介 Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指 ...

  3. Spring支持5种类型的增强

    Spring支持5种类型的增强:1.前置增强:org.springframework.aop.BeforeAdvice代表前置增强,因为Spring只支持方法级的增强,所以MethodBeforeAd ...

  4. NSURLSession实现文件上传

    7.1 涉及知识点(1)实现文件上传的方法 /* 第一个参数:请求对象 第二个参数:请求体(要上传的文件数据) block回调: NSData:响应体 NSURLResponse:响应头 NSErro ...

  5. Springboot集成velocity

    1.加入maven包 <parent> <groupId>org.springframework.boot</groupId> <artifactId> ...

  6. JavaEE复习三

    Http协议是基于请求/响应模式.无状态的协议:所有请求时相互独立的.无连续的:服务器无法记住与识别用户. 对于简单的页面浏览或信息获取,http协议可以完全胜任:对于需要提供客户端和服务器端交互的网 ...

  7. 什么是JMS规范?

    一.简介 JMS是什么:JMS是Java提供的一套技术规范和关于消息中间件的协议 JMS干什么用:通过生产者Producer,消息服务器,以及消费者通力合作,使异构系统能进行集成通信,缓解系统瓶颈,提 ...

  8. Python用pandas获取Excel数据

    import pandas as pd df1 = pd.DataFrame(pd.read_excel(r'C:\python测试文件\我的三国啊.xlsx',sheet_name='Sheet1' ...

  9. 车载以太网第二弹|测试之实锤 -DoIP测试开发实践

    前言 车载以太网测试之实锤系列,之前我们已经从环境设备组成.被测对象组成再到测试过程和测试结果分析,分享了完整的PMA测试 .IOP测试 .TC8中的TCP/IP协议一致性测试 .也分享了1000BA ...

  10. IDT系统中断描述表以及绕过Xurtr检测的HOOK姿势

    什么是中断?  指当出现需要时,CPU暂时停止当前程序的执行转而执行处理新情况的程序和执行过程.即在程序运行过程中,系统出现了一个必须由CPU立即处理的情况,此时,CPU暂时中止程序的执行转而处理这个 ...