Spring Boot引入某个包下部分Bean

  Spring Boot环境下,假如有个第三方包third.jar,内有com.xxx.config目录,在com.xxx.config目录下有3个被@Component注释的类分别是A, B, C,现在我们需要在应用中让A注册到Spring容器中,而B,C不注册进来。有2个方法:

  1. 使用@ComponetScan的Filter,类似这样:@ComponentScan(basePackageClasses = A.class, useDefaultFilters = false, includeFilters = Filter(type = FilterType.ASSIGNABLE_TYPE, classes = A.class)),说明basePackageClasses = A.class表示扫描A.class所在的包,也就是com.xxx.config,useDefaultFilters = false表示被@Service,@Componet,@Configuration这类注释的类不能被扫描注册,includeFilters下的ype = FilterType.ASSIGNABLE_TYPE表示手动指定需要被include类(还有个类似的exclude),classes = A.class表示A被注入到容器。总体来说就是:让om.xxx.config被include到Spring容器。
  2. 上述方式较长,是一种标准方式,还有一种比较简单:通过Spring Boot的@Import(A.class)这种方式也能实现上面的功能。不过我习惯第一种方式一点,第二这种方式总感觉有点怪怪的。

原文地址:https://www.cnblogs.com/dreamroute/p/12194402.html

时间: 2024-07-30 18:05:21

Spring Boot引入某个包下部分Bean的相关文章

Spring Boot 打war包并利用docBase指定根目录为打包的工程

指定根目录有两种方式 1:直接将打的war包名称定义为ROOT 2:利用docBase 比如笔者war包名为xibu.war,将该war包丢到/Users/archerlj/Library/apache-tomcat-8.5.8/webapps目录下即可.为了访问方便,我修改了tomcat安装目录下的conf文件下的server.xml文件: 在该文件最后的Host节点添加如下: <!-- path留空代表访问域名后面不需要带项目的名称 --> <Context path="&

Spring Boot 报错:Error creating bean with name &#39;entityManagerFactory&#39; defined in class path resource

spring boot 写一个web项目,在使用spring-data-jpa的时候,启动报如下错误: Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.2018-04-02 17:00:47.076 ERROR 4648 --- [ main] o.s.boot.SpringApplication : A

Spring Boot 打 war 包的步骤

## Spring Boot 打 war 包的步骤 1. 添加 spring-boot-start-tomcat 的 provided 依赖 ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </

spring boot 打jar包,获取resource路径下的文件

前言:最近在spring boot项目静态类中获取resource路径下文件,在idea中启动都可以获取,但是打包后变成了jar包 就无法获取到.我想到了两种方法,一种是根据http访问静态资源比如 localhost:9080/static/template/xxx.ftl文件.另外一种是根据流获取到文件,然后拷贝到新的文件夹下面.下面说的就是第二种方式的代码 public class DocUtil { //此路径是其他方法进行调用,且只需要加载一次private static String

spring boot 引入模板

今天主要说下,配置在resources文件中的内容怎样被spring boot所引用. 引用静态模板的值 thymeleaf和spring boot的整合,及相关配置 根据springboot 推荐的格式,我们知道放在resources 都是一些资源,包括整个项目的配置啊 ,自定义配置啊  ,静态资源文件 (js,css等),以properties结尾.字面意思就是属性暂且就这么翻译吧. application.properties是项目资源的配置,比如jar包的配置啊,比如默认端口是8080

Spring Boot 以 jar 包方式运行在后台

spring-boot jar 包方式启动: 首先,为了防止和常用的 Tomcat 8080 端口冲突,将 Spring-boot 项目的端口号设置为 9090. 具体方法:在 application.properties 文件里写 server.port=9090. 使用 maven 命令:clean package 打包. 放到服务器上一个合适的位置. 使用 Java -jar spring-boot01-1.0-SNAPSHOT.jar 命令,启动 jar 包. 然后,在浏览器输入 htt

【转】Spring boot 打成jar包问题总结

http://www.cnblogs.com/xingzc/p/5972488.html 1.Unable to find a single main class from the following candidates 1.1.问题描述 maven build时出现以下错误提示日志: [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.5.RELEASE:repackage

Spring Boot导出jar包发布

一:事由 现在的项目组开发项目使用的是Spring Boot的技术,开发的时候是直接通过一个入口主函数来启动项目的.如果将项目交给客户,怎样才能正确的发布运行呢?百度了一下有关的知识,大概了解到是通过导出jar包,直接运行来实现的.不过从网上查阅的资料,都不太理想,于是自己和另外一个同事就倒腾开了.倒腾了好一会,最终找到了一个比较简单有效的方法来实现,现在分享如下. 二:从Eclipse中导出对应的可运行的jar包(这一步是最为关键的) 1:选中对应的java项目——右键——导出 2:选择导出j

Spring Boot项目在Mac下使用Maven启动时碰到的神奇问题:Unregistering JMX-exposed beans on shutdown

错误如下: ? springboottest1 mvn spring-boot:run [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building springboottest1 0.0.1-SNAPSHOT [INFO] ---------------------------------