这是我参与更文挑战的第6天,活动详情查看:更文挑战html
@Component
和 @Bean
的区别是什么?@Component
注解做用于类,而 @Bean
注解做用于方法、@Component
一般是经过路径扫描来自动侦测以及自动装配到 Spring
容器中(咱们可使用 @ComponentScan
注解定义要扫描的路径从中找出标识了须要装配的类自动装配到 Spring
的 bean
容器中)。@Bean
注解一般是咱们在标有该注解的方法中定义产生这个 bean
,@Bean
告诉了 Spring
这是某个类的实例,当咱们须要用它的时候还给我。@Bean
注解比 @Component
注解的自定义性更强,并且不少地方咱们只能经过 @Bean
注解来注册 bean
。好比当咱们引用第三方库中的类须要装配到 Spring
容器时,只能经过 @Bean
来实现。@Bean
注解使用示例:前端
@Configuration
public class AppConfig {
@Bean
public TransferService transferService() {
return new TransferServiceImpl();
}
}
复制代码
@Component
注解使用示例:java
@Component
public class ServiceImpl implements AService {
....
}
复制代码
下面这个例子是经过 @Component
没法实现的:web
@Bean
public OneService getService(status) {
case (status) {
when 1:
return new serviceImpl1();
when 2:
return new serviceImpl2();
when 3:
return new serviceImpl3();
}
}
复制代码
Autowire
和 @Resource
的区别@Autowire
和 @Resource
均可以用来装配bean,均可以用于字段或setter方法。@Autowire
默认按类型装配,默认状况下必需要求依赖对象必须存在,若是要容许 null 值,能够设置它的 required 属性为 false。@Resource
默认按名称装配,当找不到与名称匹配的 bean 时才按照类型进行装配。名称能够经过 name 属性指定,若是没有指定 name 属性,当注解写在字段上时,默认取字段名,当注解写在 setter 方法上时,默认取属性名进行装配。注意:若是 name 属性一旦指定,就只会按照名称进行装配。面试
@Autowire
和@Qualifier
配合使用效果和@Resource
同样:spring
@Autowired(required = false) @Qualifier("example")
private Example example;
@Resource(name = "example")
private Example example;
复制代码
@Resource
装配顺序数据库
@Component
:通用的注解,可标注任意类为 Spring
的组件。若是一个 Bean 不知道属于哪一个层,可使用 @Component
注解标注。@Repository
:对应持久层即 Dao 层,主要用于数据库相关操做。@Service
:对应服务层,主要设计一些复杂的逻辑,须要用到 Dao 层。@Controller
:对应 Spring MVC 控制层,主要用来接受用户请求并调用 Service 层返回数据给前端页面。@Configuration
:声明该类为一个配置类,能够在此类中声明一个或多个 @Bean
方法。@Configuration
:配置类注解@Configuration
标明在一个类里能够声明一个或多个 @Bean
方法,而且能够由 Spring
容器处理,以便在运行时为这些 bean 生成 bean 定义和服务请求,例如:json
@Configuration
public class AppConfig {
@Bean
public MyBean myBean() {
// instantiate, configure and return bean ...
}
}
复制代码
咱们能够经过 AnnotationConfigApplicationContext
来注册 @Configuration
类:api
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();
MyBean myBean = ctx.getBean(MyBean.class);
// use myBean ...
复制代码
另外也能够经过组件扫描(component scanning)来加载,@Configuration
使用 @Component
进行原注解,所以 @Configuration
类也能够被组件扫描到(特别是使用 XML 的 <context:component-scan />
元素)。@Configuration
类不只可使用组件扫描进行引导,还可使用 @ComponentScan
注解自行配置组件扫描:浏览器
@Configuration
@ComponentScan("com.acme.app.services")
public class AppConfig {
// various @Bean definitions ...
}
复制代码
使用 @Configuration
的约束:
static
。@Bean
方法可能不会反过来建立更多的配置类。除了单独使用 @Configuration
注解,咱们还能够结合一些外部的 bean 或者注解共同使用,好比 Environment
API,@PropertySource
,@Value
,@Profile
等等许多,这里就不作详细介绍了,更多的用法能够参看 Spring @Configuration
的相关文档 。
@ControllerAdvice
:处理全局异常利器在 Spring 3.2 中,新增了 @ControllerAdvice
、@RestControllerAdvice
、@RestController
注解,能够用于定义 @ExceptionHandler
、@InitBinder
、@ModelAttribute
,并应用到全部 @RequestMapping
、@PostMapping
、@GetMapping
等这些 Controller 层的注解中。
默认状况下,@ControllerAdvice
中的方法应用于全局全部的 Controller。而使用选择器 annotations()
,basePackageClasses()
和 basePackages()
(或其别名value()
)来定义更小范围的目标 Controller 子集。 若是声明了多个选择器,则应用 OR
逻辑,这意味着所选的控制器应匹配至少一个选择器。 请注意,选择器检查是在运行时执行的,所以添加许多选择器可能会对性能产生负面影响并增长复杂性。
@ControllerAdvice
咱们最常使用的是结合 @ExceptionHandler
用于全局异常的处理。能够结合如下例子,咱们能够捕获自定义的异常进行处理,而且能够自定义状态码返回:
@ControllerAdvice("com.developlee.errorhandle")
public class MyExceptionHandler {
/** * 捕获CustomException * @param e * @return json格式类型 */
@ResponseBody
@ExceptionHandler({CustomException.class}) //指定拦截异常的类型
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) //自定义浏览器返回状态码
public Map<String, Object> customExceptionHandler(CustomException e) {
Map<String, Object> map = new HashMap<>();
map.put("code", e.getCode());
map.put("msg", e.getMsg());
return map;
}
}
复制代码
更多信息能够参看 Spring @ControllerAdvice
的官方文档 。
@Component
, @Repository
, @Service
的区别注解 | 含义 |
---|---|
@Component | 最普通的组件,能够被注入到 Spring 容器进行管理 |
@Repository | 做用于持久层 |
@Service | 做用于业务逻辑层 |
@Controller | 做用于表现层(spring-mvc的注解) |
@Component
是一个通用的Spring容器管理的单例bean组件。而@Repository
, @Service
, @Controller
就是针对不一样的使用场景所采起的特定功能化的注解组件。
所以,当你的一个类被@Component所注解,那么就意味着一样能够用@Repository
, @Service
, @Controller
来替代它,同时这些注解会具有有更多的功能,并且功能各异。
最后,若是你不知道要在项目的业务层采用@Service
仍是@Component
注解。那么,@Service
是一个更好的选择。
以上简单介绍了几种 Spring 中的几个注解及代码示例,就我我的而言,均是平时用到且不容易理解的几个,或者容易忽略的几个。固然,这篇文章并无彻底介绍完,在从此还会继续补充完善。