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 ...
随机推荐
- 数据结构实验之数组一:矩阵转置(SDUT 2130)
Problem Description 数组--矩阵的转置 给定一个m*n的矩阵(m,n<=100),求该矩阵的转置矩阵并输出. Input 输入包含多组测试数据,每组测试数据格式如下: 第一行 ...
- 7月清北学堂培训 Day 5
今天是钟皓曦老师的讲授~ 动态规划 动态规划的三种实现方法: 1.递推: 2.递归: 3.记忆化: 举个例子: 斐波那契数列:0,1,1,2,3,5,8…… Fn = Fn-1 + Fn-2 1.我们 ...
- Npoi Web 项目中(XSSFWorkbook) 导出出现无法访问已关闭的流
NPOI生产.xlsx文件件时,在使用book.Write(ms);后,会关闭流,这样导致再次使用Respons输出流的时候就出错了. 造成关闭流的主要原因有时其实是跨域,同域是没有问题的. //新建 ...
- 监控查询慢sql
mysql:--查询慢sql:业务db用户 select b.time, b.host, b.id, b.state, b.user, b.db, b.info from information_s ...
- as 什么意思?
You can denote particular console messages and variable values as having different types using four ...
- [Scikit-learn] 2.3 Clustering - kmeans
参考: 2.3. Clustering 2.4. Biclustering 2.1.2.3. The Dirichlet Process Clusering, GMM, Variational Inf ...
- 将ByteBuffer保存成文件
String dest = "d:/download/" + name; Path path = Paths.get(dest).getParent().toAbsolutePat ...
- ES6重要点学习
1.解析赋值: let [a,b,c,d,e] = '我是中国人' //我 a = wo let [a,b,c] = 123 // 出错,Numberbu不允许解析不允许解析 2.数据集合Set, m ...
- Java使用jxl写入Excel文件
首先添加jxl的maven依赖: <!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl --> < ...
- CentOS7.5使用RPM包安装MySQL5.7.22
参考:https://blog.csdn.net/sevenkoala/article/details/76163853 1,安装环境查看 2,下载安装rpm包 下载地址:https://downlo ...