@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的区别的更多相关文章

  1. Spring中BeanFactory与FactoryBean的区别

    在Spring中有BeanFactory和FactoryBean这2个接口,从名字来看很相似,比较容易搞混. 一.BeanFactory BeanFactory是一个接口,它是Spring中工厂的顶层 ...

  2. [转载]@Component 和 @Bean 的区别

    @Component 和 @Bean 的区别 @Component 和 @Bean 的区别 Spring帮助我们管理Bean分为两个部分,一个是注册Bean,一个装配Bean. 完成这两个动作有三种方 ...

  3. Spring中常见的bean创建异常

    Spring中常见的bean创建异常 1. 概述     本次我们将讨论在spring中BeanFactory创建bean实例时经常遇到的异常 org.springframework.beans.fa ...

  4. Spring中ClassPathXmlApplication与FileSystemXmlApplicationContext的区别

    Spring中ClassPathXmlApplication与FileSystemXmlApplicationContext的区别 一.概述 在项目中遇到加载不到Spring配置文件,简单分析后,写此 ...

  5. 【Java面试】Spring中 BeanFactory和FactoryBean的区别

    一个工作了六年多的粉丝,胸有成竹的去京东面试. 然后被Spring里面的一个问题卡住,唉,我和他说,6年啦,Spring都没搞明白? 那怎么去让面试官给你通过呢? 这个问题是: Spring中Bean ...

  6. Spring 中 ApplicationContext 和 BeanFactory 的区别,以及 Spring bean 作用域

    //从ApplicationContext 中取 bean ApplicationContext ac = new ClassPathXmlApplicationContext ( "com ...

  7. Spring中@Component注解,@Controller注解详解

    在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...

  8. 【转】Spring 中三种Bean配置方式比较

    今天被问到Spring中Bean的配置方式,很尴尬,只想到了基于XML的配置方式,其他的一时想不起来了,看来Spring的内容还没有完全梳理清楚,见到一篇不错的文章,就先转过来了. 以前Java框架基 ...

  9. Spring中@Component注解,@Controller注解详解

    在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...

随机推荐

  1. LibreOJ #110. 乘法逆元

    二次联通门 : LibreOJ #110. 乘法逆元 /* LibreOJ #110. 乘法逆元 求一个数在模意义下的所有逆元 */ #include <cstdio> void read ...

  2. 前端逼死强迫症系列之Html

    概述 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就可以让浏览器 ...

  3. 数据库隔离级别,每个级别会引发什么问题,mysql默认是哪个级别

    1.脏读  脏读是指在一个事务处理过程里读取了另一个未提交的事务中的数据. 当一个事务正在多次修改某个数据,而在这个事务中这多次的修改都还未提交,这时一个并发的事务来访问该数据,就会造成两个事务得到的 ...

  4. django中安装pillow ValueError: zlib is required unless explicitly disabled using --disable-zlib, aborting

    在windows系统上,使用  pip install pillow安装pillow时 报错 在使用 easy_install Pillow 方式安装成功,默认是最高版本 如果需要在安装时,指定安装版 ...

  5. 通过xshell在本地win主机和远程linux主机传输文件

    1.下载和安装xshell此处不再介绍 2.安装lrzsz的软件 yum install lrzsz 3.通过xshell上传文件 只需要在XShell的菜单中点击File – Transfer – ...

  6. Pluck CMS 4.7.10远程代码执行漏洞分析

    本文首发于先知: https://xz.aliyun.com/t/6486 0x01漏洞描述 Pluck是用php编写的一款的小型CMS影响版本:Pluck CMS Pluck CMS 4.7.10( ...

  7. pandas总结

    ### 一.创建对象  # 1.可以通过传递一个list对象来创建一个Series,pandas会默认创建整型索引: # s=pd.Series([1,3,5,np.nan,6,8]) # print ...

  8. JavaWeb MySQL 实现登录验证

    0. 环境准备 项目创建: IDEA 创建 Servlet 项目详细步骤:https://www.jianshu.com/p/386a79d16e05 导入 MySQL 驱动包: Java MySQL ...

  9. Oracle查询表和字段

    查看表字段.类型.注释 SELECT A.COLUMN_NAME,B.comments,A.DATA_TYPE FROM USER_TAB_COLUMNS A LEFT JOIN user_col_c ...

  10. react对字符串转义成html并渲染

    <div dangerouslySetInnerHTML={{__html: "字符串内容"}} />