(012)Spring Boot之改变SpringBootApplication注解默认扫描路径
Posted 明月之诗
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(012)Spring Boot之改变SpringBootApplication注解默认扫描路径相关的知识,希望对你有一定的参考价值。
SpringBootApplication默认扫描当前包以及子包的所有类,如下所示,SpringBootApplication 注解与User在同一级,无法获取User的bean:
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.edu.spring</groupId> <artifactId>springboot</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>springboot</name> <!-- FIXME change it to the project\'s website --> <url>http://www.example.com</url> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.6.RELEASE</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> </project>
User.java
package com.edu.spring.user; import org.springframework.stereotype.Component; @Component public class User { }
App.java
package com.edu.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import com.edu.spring.user.User; @SpringBootApplication public class App { public static void main( String[] args) { SpringApplication app=new SpringApplication(App.class); ConfigurableApplicationContext context= app.run(args); System.out.println(context.getBean(User.class)); context.close(); } }
目录结构:
运行结果如下:
可以通过scanBasePackages指定扫描路径
App.java,修改为@SpringBootApplication(scanBasePackages={"com.edu.spring"})
package com.edu.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import com.edu.spring.user.User; @SpringBootApplication(scanBasePackages={"com.edu.spring"}) public class App { public static void main( String[] args) { SpringApplication app=new SpringApplication(App.class); ConfigurableApplicationContext context= app.run(args); System.out.println(context.getBean(User.class)); context.close(); } }
运行结果如下:
exclude和excludeName可以排除掉要扫描的类,如下将不会获取User的bean:
@SpringBootApplication(scanBasePackages={"com.edu.spring"},exclude=User.class)
@SpringBootApplication(scanBasePackages={"com.edu.spring"},excludeName="com.edu.spring.user.User")
以上是关于(012)Spring Boot之改变SpringBootApplication注解默认扫描路径的主要内容,如果未能解决你的问题,请参考以下文章
Spring boot Sample 012之spring-boot-web-upload
012-Spring Boot web创建请求使用jspfreemarker,tomcatjetty
Spring Boot实战之Filter实现简单的Http Basic认证