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 ...
随机推荐
- P1106 删数问题 自己码风好菜
一个人的码风好坏究竟会影响多少
- scrapy框架之shell
scrapy shell scrapy shell是一个交互式shell,您可以在其中快速调试 scrape 代码,而不必运行spider.它本来是用来测试数据提取代码的,但实际上您可以使用它来测试任 ...
- ImportError: No module named rospy
Traceback (most recent call last): File "manage.py", line 4, in <module> import rosp ...
- 常使用的VIM命令及文件颜色代表含义
编辑模式--->输入模式 i : insert 在光标所在处输入: a:append 在光标所在处后面输入: o:在当前光标所在行的下方打开一个新行: I:在当前光标所在行的行首输入: A:在当 ...
- Mybatis 批量操作-删除、修改和查询
批量操作的核心就是一次传入多个数据然后进行相关操作,增删改查中掌握其中一个,其它的就可以举一反三,触类旁通.它之所以执行效率高,是因为合并后日志量(MySQL的binlog和InnoDB的 ...
- SQL Server Management Studio 清除用户名和密码
SQL Server Management Studio 2018 delete the file C:\Users\%username%\AppData\Roaming\Microsoft\SQL ...
- python —— 生成器
通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面绝大多数元素 ...
- Highcharts 使用总结
一.Highcharts series属性 1.下面是一个基本曲线图的例子: <html> <head> <meta charset="UTF-8" ...
- kotlin 之相等判断
在kotlin 中存在二种相等的判断: 1.引用相等 也就是说,两个引用指向同一个对象,使用===操作 ,相反操作为!==来判断 2.结构相等 使用equals 函数相等和==操作符 a?.equal ...
- TortoiseSVN commit 停止工作
TortoiseSVN commit 便停止工作,详细原因是igc64.dll故障,该动态链接库与Intel HD Graphics Driver有关(即显卡驱动),由于重装系统后,进行了显卡驱动的更 ...