Spring中@Component与@Bean的区别
@Component和@Bean的目的是一样的,都是注册bean到Spring容器中。
@Component VS @Bean
@Component 和 它的子类型(@Controller, @Service and @Repository)注释在类上。告诉Spring,我是一个bean,通过类路径扫描自动检测并注入到Spring容器中。
@Bean不能注释在类上,只能用于在配置类中显式声明单个bean。意思就是,我要获取这个bean的时候,spring要按照这种方式去获取这个bean。默认情况下@Bean注释的方法名作为对象的名字,也可以用name属性定义对象的名字。
所有组件类型及其用途
| 组件注解 | 用途 |
| @Component | 标注最普通的组件 |
| @Controller | 标注控制层(spring-mvc的注解)(如:*Controller) |
| @Service | 标注业务层(如:*Service) |
| @Repository | 标注持久层(如:*Dao) |
所有组件类型都以相同的方式处理。子类型仅仅是标记,有利于代码可读性而不是特性。
验证代码如下:
@Controller
@RequestMapping("/web")
public class WebController {
@ResponseBody
@RequestMapping("/msg")
public String message(){
return "msg";
}
}
@Component
@RequestMapping("/web")
public class WebController {
@ResponseBody
@RequestMapping("/msg")
public String message(){
return "msg";
}
}
@Service
@RequestMapping("/web")
public class WebController {
@ResponseBody
@RequestMapping("/msg")
public String message(){
return "msg";
}
}
访问url=locahost:8080/web/msg,三段代码均返回字符串msg。(此web项目我自己用的端口8080)
@Bean的使用
// Just a POJO
public class MessageBuilder {
public String getMsg(){
return "msgBuilder";
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
// Let's turn the POJO into a bean
@Configuration
public class AppConfig {
@Bean
public MessageBuilder messageBuilder(){
return new MessageBuilder();
}
}
@Controller
@RequestMapping("/web")
public class WebController {
// Finally, hook it up
@Autowired
private MessageBuilder messageBuilder; @ResponseBody
@RequestMapping("/msg")
public String message(){
return messageBuilder.getMsg();
} }
参考文章:
http://www.tomaszezula.com/2014/02/09/spring-series-part-5-component-vs-bean/
Spring中@Component与@Bean的区别的更多相关文章
- Spring中BeanFactory与FactoryBean的区别
在Spring中有BeanFactory和FactoryBean这2个接口,从名字来看很相似,比较容易搞混. 一.BeanFactory BeanFactory是一个接口,它是Spring中工厂的顶层 ...
- [转载]@Component 和 @Bean 的区别
@Component 和 @Bean 的区别 @Component 和 @Bean 的区别 Spring帮助我们管理Bean分为两个部分,一个是注册Bean,一个装配Bean. 完成这两个动作有三种方 ...
- Spring中常见的bean创建异常
Spring中常见的bean创建异常 1. 概述 本次我们将讨论在spring中BeanFactory创建bean实例时经常遇到的异常 org.springframework.beans.fa ...
- Spring中ClassPathXmlApplication与FileSystemXmlApplicationContext的区别
Spring中ClassPathXmlApplication与FileSystemXmlApplicationContext的区别 一.概述 在项目中遇到加载不到Spring配置文件,简单分析后,写此 ...
- 【Java面试】Spring中 BeanFactory和FactoryBean的区别
一个工作了六年多的粉丝,胸有成竹的去京东面试. 然后被Spring里面的一个问题卡住,唉,我和他说,6年啦,Spring都没搞明白? 那怎么去让面试官给你通过呢? 这个问题是: Spring中Bean ...
- Spring 中 ApplicationContext 和 BeanFactory 的区别,以及 Spring bean 作用域
//从ApplicationContext 中取 bean ApplicationContext ac = new ClassPathXmlApplicationContext ( "com ...
- Spring中@Component注解,@Controller注解详解
在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...
- 【转】Spring 中三种Bean配置方式比较
今天被问到Spring中Bean的配置方式,很尴尬,只想到了基于XML的配置方式,其他的一时想不起来了,看来Spring的内容还没有完全梳理清楚,见到一篇不错的文章,就先转过来了. 以前Java框架基 ...
- Spring中@Component注解,@Controller注解详解
在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...
随机推荐
- lxr看代码的时候出现中文乱码问题
修改lxr.conf 修改 , 'encoding' => 'utf-8',为utf-8
- 获取 Django版本号的两种方式
one k@ubuntu:~$ python Python ( , ::) [GCC ] on linux2 Type "help", "copyright", ...
- 工作流调度系统Azkaban的简介和使用
1 概述 1.1 为什么需要工作流调度系统 l 一个完整的数据分析系统通常都是由大量任务单元组成: shell脚本程序,java程序,mapreduce程序.hive脚本等 l 各任务单元之间存在时间 ...
- mysql 常见面试题
附录: https://mp.weixin.qq.com/s/pC0_Y7M7BkoUmlRwneZZdA 一.为什么用自增列作为主键 1.如果我们定义了主键(PRIMARY KEY),那么InnoD ...
- Mybatis 传入多个参数查询数据 (3种方法)
第一种方案 DAO层的函数方法 public User selectUser(String name,String area); 对应的Mapper.xml <select id="s ...
- Vue 目录
什么是 vue-cli 通过 vue-cli 建立出来的 vue
- 记录学习Linux遇到的问题
shl@shl-tx:~$ ifconfig Command 'ifconfig' not found, but can be installed with: sudo apt install net ...
- linux 测试磁盘iops 方法详解
一.FIO安装 wget http://brick.kernel.dk/snaps/fio-2.0.7.tar.gz yum -y install libaio-devel tar -zxvf ...
- Qt编写数据可视化大屏界面电子看板6-窗体打开关闭
一.前言 二级窗体的打开与关闭,这个功能也很有必要,由于整个系统中各种模块数量窗体数量比较多,后期可能还会增加更多,在4K屏幕上可以显示很多的模块,但是有时候有些模块不想显示出来,就需要将该模块关闭掉 ...
- Example config file /etc/vsftpd.conf
# Example config file /etc/vsftpd.conf # # The default compiled in settings are fairly paranoid. Thi ...