如何将Spring配置文件设置为包?

发布于 2021-01-29 20:34:42

我想将配置文件名称设置为整个程序包,但我不知道如何。如果在哪里不是简单的方法,那么我必须用@Profile注释标记包和子包中的每个类。

<context:component-scan/>标签不支持这样的属性,profile所以我不知道。

关注者
0
被浏览
66
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    您可以将个人资料设置为:

    • Spring Beans XML文件-用于xml配置
        <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
               profile="your-profile">
    
            <context:component-scan base-package="your.package" />
        </beans>
    
    • @Configuration Java配置类
        @Configuration
        @Profile("your-profile")
        @Componentscan("your.package")
        class AppConfig {
        }
    

    在它们的每一个中,您都可以对特定的软件包使用组件扫描。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看