`
weiqingfei
  • 浏览: 311718 次
  • 性别: Icon_minigender_1
  • 来自: 黑洞
社区版块
存档分类
最新评论

谈谈spring boot里的@import

    博客分类:
  • Java
 
阅读更多

使用spring boot时,如果想自己实现一些初始设置比较复杂的bean时,可以在类上用@Configuration注解,然后类内部在返回具体bean的方法上使用@Bean注解。

那么如何让容器找到这个配置类呢?

 

1.最简单的方法,当然是把它放到程序可以扫描到的package里,也就是@ComponentScan注解所指定的package里。

   平时自己创建的配置类通常用这种方法,简单明了。

 

2.如果没有在package扫描路径里,比如引入的第三方包,可以通过META-INF/spring.factories里用org.springframework.boot.autoconfigure.EnableAutoConfiguration来制定。

   spring-boot-autoconfigure包里的配置类都是通过这种方式引入的。

   当然,这个方式需要程序使用@EnableAutoConfiguration注解,这个注解是通过AutoConfigurationImportSelector来扫描spring.factories文件,把定义的配置类引入的。

 

3.使用@Import注解

   这个注解可以引入三种类

   a.使用了@Configuration注解的类

       这个比较简单,如果明确知道需要引入哪个配置类,直接引入就可以。

   b.ImportSelector的子类

       如果并不确定引入哪个配置类,需要根据@Import注解所标识的类或者另一个注解(通常是注解)里的定义信息选择配置类的话,用这种方式。

       实际上上面2种所描述的AutoConfigurationImportSelector就是用的这种方式。

       另外一个比较典型的是注解@EnableTransactionManagement

       

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(TransactionManagementConfigurationSelector.class)
public @interface EnableTransactionManagement {
	boolean proxyTargetClass() default false;
	AdviceMode mode() default AdviceMode.PROXY;
	int order() default Ordered.LOWEST_PRECEDENCE;

}

      是通过TransactionManagementConfigurationSelector类,根据注解@EnableTransactionManagement所指定的AdviceMode来选择使用哪个配置类的。

     

public class TransactionManagementConfigurationSelector extends AdviceModeImportSelector<EnableTransactionManagement> {
	protected String[] selectImports(AdviceMode adviceMode) {
		switch (adviceMode) {
			case PROXY:
				return new String[] {AutoProxyRegistrar.class.getName(), ProxyTransactionManagementConfiguration.class.getName()};
			case ASPECTJ:
				return new String[] {TransactionManagementConfigUtils.TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME};
			default:
				return null;
		}
	}

}

   c.ImportBeanDefinitionRegistrar的子类。

      这个其实跟注解@Configuration没啥关系了,因为是注解@Import的功能,所以就放在这儿一并说了。

      一般只要用户确切的知道哪些bean需要放入容器的话,自己便可以通过spring boot里所提供的注解来标识了,比如@Configuration里的@Bean,比如@Component,如果是spring mvc的话,还有一些专用的@Controller,@Service,@Repository。

      但是,如果是第三方包,而且又不是确定的类,并且这些类并不是spring专用,所以不想用spring的注解进行侵入式标识,那么如果找到这些类放到spring的容器呢?

      这时候就用到了用注解@Import引入ImportBeanDefinitionRegistrar子类的方式,最典型的应用就是mybatis,使用工具自动生成了一批mapper和entity,而如何把这些普通的类放入容器,就是通过注解@MapperScan

     

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(MapperScannerRegistrar.class)
public @interface MapperScan {
  String[] value() default {};
  String[] basePackages() default {};
  Class<?>[] basePackageClasses() default {};
  Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
  Class<? extends Annotation> annotationClass() default Annotation.class;
  Class<?> markerInterface() default Class.class;
  String sqlSessionTemplateRef() default "";
  String sqlSessionFactoryRef() default "";
  Class<? extends MapperFactoryBean> factoryBean() default MapperFactoryBean.class;

}

      这个注解用@Import引入了MapperScannerRegistrar类,这个类里会取得注解@MapperScan作设置的package,然后扫描这个package下所有的类,并放入容器中。

      代码就不贴了,有兴趣可以自行查看。

 

根据上面的描述,可以看出,如果不是制作模块,只是使用的话,通常用第一种就可以了,如果是制作第三方包,并且根据需求想更加灵活的把bean注册到容器的话,可以考虑其它方式。

 

上一篇文章Spring boot + doma2里所述doma-gen所生成的类没有办法自动注册,其实可以考虑实现ImportBeanDefinitionRegistrar的一个子类来解决这个问题。

分享到:
评论

相关推荐

    Spring boot——@DeclareParents例子

    Spring boot——@DeclareParents例子...

    如何使用Spring Boot的@Pointcut注解

    在本文中,我们深入了解了Spring Boot中的@Pointcut注解。我们首先介绍了@Pointcut注解的作用和其在AspectJ框架中的重要性。然后,我们详细解释了@Pointcut注解的语法和常用的切点表达式规则。 为了帮助读者更好地...

    Spring Boot利用@Async如何实现异步调用:自定义线程池

    主要给大家介绍了关于Spring Boot利用@Async如何实现异步调用:自定义线程池的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    详解Spring 注解之@Import 注入的各种花活

    主要介绍了详解Spring 注解之@Import 注入的各种花活,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    Spring Boot中的@Scheduled注解:定时任务的原理与实现

    本文将详细探讨Spring Boot中@Scheduled注解的使用,包括其原理、实现流程、步骤和代码示例。通过本文,读者将能够了解如何在Spring Boot应用中轻松创建和管理定时任务。 # @Scheduled注解简介 在Spring框架中,@...

    Spring Boot系列四 Spring @Value 属性注入使用总结一

    Spring Boot系列四 Spring @Value 属性注入使用总结一

    Spring boot 示例 官方 Demo

    spring-boot-helloWorld:spring-boot的helloWorld版本 spring-boot-mybaits-annotation:注解版本 spring-boot-mybaits-xml:xml配置版本 spring-boot-mybatis-mulidatasource:springboot+mybatis多数据源最简解决...

    Spring boot中@Conditional和spring boot的自动配置实例详解

    本文通过实例给大家介绍了Spring boot中@Conditional和spring boot的自动配置,需要的朋友可以参考下

    浅谈Spring中@Import注解的作用和使用

    主要介绍了浅谈Spring中@Import注解的作用和使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    Spring Boot 2 Recipes

    获取Spring Boot 2微框架的可重用代码配方和代码段 了解Spring Boot 2如何与其他Spring API,工具和框架集成 访问Spring MVC和新的Spring Web Sockets,以实现更简单的Web开发 使用微服务进行Web服务开发并与Spring ...

    从零开始学Spring Boot

    1.3 spring boot起步之Hello World 1.4 Spring Boot返回json数据 1.5 Spring Boot热部署 1.6 Spring Boot使用别的json解析框架 1.7 全局异常捕捉 1.8 Spring Boot datasource - mysql 1.9 JPA - Hibernate 1.10 使用...

    Pro Spring Boot 2第2版-2009-EPUB版

    Pro Spring Boot 2: An Authoritative Guide to Building Microservices, Web and Enterprise Applications, and Best Practices Quickly and productively develop complex Spring applications and microservices...

    Beginning Spring Boot 2

    Beginning Spring Boot 2 Beginning Spring Boot 2 Beginning Spring Boot 2

    基于 Spring Boot + MySQL 开发的博客系统源码.zip

    基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring ...

    基于spring boot餐厅管理系统源码.zip

    基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 ...

    spring boot @scheduled定时任务配置

    spring boot @scheduled注解 cron 表达式实现计划任务调度。

    Beginning Spring Boot 2 Applications and Microservices with the Spring Framework

    This book will help you understand what Spring Boot is, how Spring Boot helps you build Spring-based applications quickly and easily, and the inner workings of Spring Boot using easy-to-follow ...

    Spring cloud和Spring boot介绍

    Spring Boot简化了基于Spring的应用开发,通过少量的代码就能创建一个独立的、产品级别的Spring应用。Spring Boot为Spring平台及第三方库提供开箱即用的设置,这样你就可以有条不紊地开始。多数Spring Boot应用只...

    如何在Spring Boot中使用@Before注解

    Spring Boot是一个流行的Java开发框架,它提供了一种方便的方式来构建高效的、可扩展的企业级应用程序。Aspect-Oriented Programming(AOP)是Spring Boot框架中的一个重要组成部分,它允许开发者通过将横切关注点...

Global site tag (gtag.js) - Google Analytics