博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring注解
阅读量:3933 次
发布时间:2019-05-23

本文共 2437 字,大约阅读时间需要 8 分钟。

@Configuration和@Component的区别

【重要区别】

@Component:会当做配置类,但不会为其生成CGLIB代理class,多例。例如@Controller、@Service等底层还是@Component注解。

@Configuration:会当做配置类,但会为其生成CGLIB代理class,走代理的会是单例。

在获取当前类名时,使用@Component获取的是当前类名;而@Configuration获取的是当前类名+唯一标识(CGLIB代理)

关于代理,另一篇博文做了详细的示例,参见

【一般区别】

@Configuration本质上还是@Component。

@Configuration标记的类必须符合下面的要求:

  1. 配置类不能是 final 类、都必须声明为static
  2. 配置注解通常为了通过 @Bean 注解生成 Spring 容器管理的类,
  3. 配置类必须是非本地的(即不能在方法中声明,不能是 private)。

Spring 容器在启动时,会加载默认的一些PostPRocessor,其中就有ConfigurationClassPostProcessor,

这个后置处理程序专门处理带有@Configuration注解的类,这个程序会在bean 定义加载完成后,在bean初始化前进行处理。

@PostConstruct

@PostConstruct注解好多人以为是Spring提供的。其实是Java自己的注解。

Java中该注解的说明:@PostConstruct该注解被用来修饰一个非静态的void()方法。

通常我们会是在Spring框架中使用到@PostConstruct注解 该注解的方法在整个Bean初始化中的执行顺序:

Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注释的方法)

bean创建完成空对象,就开始进行@Autowire、@PostConstruct赋值

@AutoConfigureAfter

其作用顾名思义,就是将一个配置类在另一个配置类之后加载。

研究初衷:

本人公司使用了Pagehelper,它的实现原理是Mybatis Plugin,也就是拦截器。根据拦截器的加载机制,后加载的先执行,由于某种原因(例如要兼容其他国产数据库),我需要在他之前拦截到SQL语句,这就要求拦截器要加载在MybatisAutoConfiguration之后。

package cn.com.infosec.netseal.webserver.config.datasource;import cn.com.infosec.netseal.common.config.ConfigUtil;import cn.com.infosec.netseal.common.exceptions.ToLogException;import com.github.pagehelper.PageInterceptor;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.AutoConfigureAfter;import org.springframework.context.annotation.Configuration;import javax.annotation.PostConstruct;import java.util.List;import java.util.Properties;/** * @Description 自定义分页插件自动装配策略,使用自定义配置文件 * @Author lhx */@Configuration@AutoConfigureAfter(MybatisAutoConfiguration.class)public class PageHelperAutoConfiguration {
@Autowired private List
sqlSessionFactoryList; @PostConstruct public void addPageInterceptor() {
//pageHelper的sql分页拦截器 PageInterceptor interceptor = new PageInterceptor(); //读取自定义的配置文件 try {
Properties sqlDialect = ConfigUtil.getInstance().getSqlDialect(); // System.out.println(sqlDialect); interceptor.setProperties(sqlDialect); } catch (Exception e) {
throw new ToLogException("读取配置发生错误", e); } //将分页拦截器添加到mybatis的sqlsession for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
sqlSessionFactory.getConfiguration().addInterceptor(interceptor); } }}

转载地址:http://qzqgn.baihongyu.com/

你可能感兴趣的文章
glm 中 数据类型 与 原始数据(c++ 数组)之间的转换
查看>>
Derivatives of scalars, vector functions and matrices
查看>>
the jacobian matrix and the gradient matrix
查看>>
VS2010 将背景设为保护色
查看>>
ubutun里面用命令行安装软件
查看>>
ubuntu 常用命令
查看>>
SQLite Tutorial 4 : How to export SQLite file into CSV or Excel file
查看>>
how to move pivot to origin
查看>>
Optimizate objective function in matrix
查看>>
Convert polygon faces to triangles or quadrangles
查看>>
How do I divide matrix elements by column sums in MATLAB?
查看>>
read obj in matlab
查看>>
find out the neighbour matrix of a mesh
查看>>
Operators and special characters in matlab
查看>>
As-Conformal-As-Possible Surface Registration
查看>>
qmake Variable Reference
查看>>
ML 14 part2 principal component analysis
查看>>
Lesson 2 Gradient Desent
查看>>
lesson 5 memory model
查看>>
lesson 6 threads synchronization
查看>>