标签: spring-profiles

方法级别的Spring配置文件?

我想介绍一些只在开发过程中执行的方法.

我以为我可以Spring @Profile在这里使用注释?但是,如何在类级别上应用此批注,以便仅在属性中配置特定的配置文件时才调用此方法?

spring.profiles.active=dev
Run Code Online (Sandbox Code Playgroud)

将以下内容作为伪代码.如何才能做到这一点?

class MyService {

    void run() { 
       log();
    }

    @Profile("dev")
    void log() {
       //only during dev
    }
}
Run Code Online (Sandbox Code Playgroud)

java spring spring-profiles

20
推荐指数
6
解决办法
4万
查看次数

Spring Boot配置跳过多个@Profile上的注册

我曾与不同的配置文件设置一个春天启动应用程序:dev,prod,qc,console等.

两个配置类的设置如下.MyConfigurationA应该注册所有配置文件除外console.MyConfigurationB应该注册,除了consoledev.

当我使用配置文件运行应用程序时console,MyConfigurationA没有注册 - 这很好.但MyConfigurationB得到注册 - 我不想要.我已@Profile按如下方式设置注释,以便不注册MyConfigurationBfor profile consoledev.

但是MyConfigurationB当我使用配置文件运行应用程序时,它正在注册console.

@Profile({ "!" + Constants.PROFILE_CONSOLE ,  "!" + Constants.PROFILE_DEVELOPMENT })
Run Code Online (Sandbox Code Playgroud)

的文件(http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Profile.html)具有包括一个简档和不包括其他的一个例子.在我的例子中,我将两者都排除在外@Profile({"!p1", "!p2"}):

@Profile({"p1","!p2"}),如果配置文件'p1'处于活动状态配置文件'p2'未激活,则会发生注册.

我的问题是:我们如何跳过两个配置文件的配置注册?@Profile({"!p1", "!p2"})正在做OR操作.我们需要AND操作.


代码 :

@Configuration
@Profile({ "!" + Constants.PROFILE_CONSOLE  })
public class MyConfigurationA {
    static{ …
Run Code Online (Sandbox Code Playgroud)

java spring spring-profiles spring-boot

15
推荐指数
3
解决办法
8437
查看次数

Spring:根据配置文件选择@Service

我有一个接口定义如下:

public interface MyService {
}
Run Code Online (Sandbox Code Playgroud)

实现它的两个类:

@Service
@Profile("dev")
public class DevImplementation implements MyService {
}
Run Code Online (Sandbox Code Playgroud)

@Service
@Profile("prod")
public class ProdImplementation implements MyService {
}
Run Code Online (Sandbox Code Playgroud)

还有另一项服务尝试使用它:

@Service
public MyClass {
    @Autowired
    MyService myservice;
}
Run Code Online (Sandbox Code Playgroud)

问题是我NoSuchBeanException在运行代码时遇到了问题.它运行使用

mvn spring-boot:run -P dev

我究竟做错了什么?

java spring autowired spring-profiles

15
推荐指数
2
解决办法
6828
查看次数

正确使用Spring环境配置文件以管理PropertySourcesPlaceholderConfigurer和属性文件集

我正在处理Spring应用程序,我意识到我的管理属性的方式存在问题.我使用Spring环境配置文件来加载我的属性,我最近添加了更多配置文件,这使我的属性文件无法管理.

属性文件位于不同的文件夹中src/main/resources/META-INF/props/,eah文件夹与不同的Spring环境配置文件匹配.

我现在至少有5个配置文件,这意味着我有5个子文件夹,每个子文件夹包含具有相同名称但仅对某些键具有不同值的属性文件.

以下是它的外观:

属性文件屏幕截图

以下是我配置PropertyPlaceholders的方法:

@Configuration
public class PropertyPlaceholderConfiguration {

    @Profile(Profiles.CLOUD)
    static class cloudConfiguration {
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException {
            PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
            propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(Boolean.TRUE);
            propertySourcesPlaceholderConfigurer.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:META-INF/props/cloud/*.properties"));
            return propertySourcesPlaceholderConfigurer;
        }
    }

    @Profile(Profiles.DEFAULT)
    static class defaultConfiguration {
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException {
            PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
            propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(Boolean.TRUE);
            propertySourcesPlaceholderConfigurer.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:META-INF/props/default/*.properties"));
            return propertySourcesPlaceholderConfigurer;
        }
    }

    @Profile(Profiles.TEST)
    static class testConfiguration {
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException {
            PropertySourcesPlaceholderConfigurer …
Run Code Online (Sandbox Code Playgroud)

spring environment-variables properties-file spring-profiles

14
推荐指数
2
解决办法
2万
查看次数

如何在引导程序文件中正确设置不同的Spring配置文件(Spring Boot以不同的Cloud Config服务器为目标)?

每个环境都有不同的配置服务器.每个Spring启动应用程序应针对其相应的配置服务器.我试图通过在bootstrap.properties文件中设置配置文件来实现这一点,例如:

spring.application.name=app-name
spring.cloud.config.uri=http://default-config-server.com

---
spring.profiles=dev
spring.cloud.config.uri=http://dev-config-server.com

---
spring.profiles=stage
spring.cloud.config.uri=http://stage-config-server.com

---
spring.profiles=prod
spring.cloud.config.uri=http://prod-config-server.com
Run Code Online (Sandbox Code Playgroud)

然后我设置了cla,-Dspring.profiles.active=dev但是加载的配置服务器始终是文件中设置的最后一个(即prod配置服务器将在上面的设置中加载,然后如果删除prod,则会加载stage).

是否可以为云配置服务器设置引导配置文件?我按照这个例子,但似乎无法使它工作.对于它的价值,这些配置文件非常适合加载正确的配置(即,如果dev配置文件处于活动状态,则会加载app-name-dev.properties),但不会从正确的配置服务器中提取.

java spring-profiles spring-boot spring-cloud-config

14
推荐指数
2
解决办法
1万
查看次数

spring.profiles.active 与 spring.config.activate.on-profile 之间有什么区别?

spring.profiles.activespring.config.activate.on-profile之间的确切区别是什么?

“WARN”,“msg”:“从位置“类路径资源[application.yaml]”导入的属性“spring.profiles”无效,应替换为“spring.config.activate.on-profile”[origin:class路径资源 [application.yaml]

spring-profiles spring-boot spring-properties

14
推荐指数
1
解决办法
3万
查看次数

Spring"spring.profiles.include"覆盖

我的目的是在spring boot应用程序中有两个配置文件 - 开发和生产.开发配置文件只是为了覆盖生产轮廓一些变量(如内存数据库,而不是数据库云).由于我预计将来会对生产配置文件进行一些更改,因此在开发配置文件中复制变量似乎不是一种解决方案.

因此,在Spring Reference中我读过,spring.profiles.include它应该只添加引用的配置文件中的属性,但是从我检查它的内容中反而覆盖了它.因此,当有两个配置文件foo和bar时,在单独的yaml文件中:

应用foo.yaml:

myproperty: 44
Run Code Online (Sandbox Code Playgroud)

应用bar.yaml:

spring:
  profiles:
    include: foo
    active: bar,foo
myproperty: 55
Run Code Online (Sandbox Code Playgroud)

-Dspring.profiles.active=bar在IDE中设置变量,运行时值为myproperty44.这意味着bar,覆盖foo它应该只添加属性,但不覆盖它们.启动应用程序时,我得到:

以下配置文件处于活动状态:foo,bar

我加入spring.profiles.active=barapplication-bar.yaml这个所建议的答案,在另一个问题,但它没有任何效果-有当时的物业是否有与否没有差别(我也使用破折号上市逗号分隔值,而不是尝试过).

我的问题是,它是如何工作的(那么Spring Reference是误导性的)?如果是这样,有什么解决方案吗?

在github上添加应用程序源代码的链接.

spring spring-profiles spring-boot

13
推荐指数
4
解决办法
1万
查看次数

Spring Boot以编程方式设置配置文件

如何在spring boot应用程序中设置活动配置文件 此应用程序将部署在独立的Tomcat中.

我有2个属性文件application- {profile} .properties.

我的应用程序类

    @SpringBootApplication
        public class Application extends SpringBootServletInitializer {

            @Override
            protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {        
                return application.sources(Application.class);
            }

            public static void main(String[] args) {
               System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev");
 ApplicationContext ctx = SpringApplication.run(Application.class, args);
      }
    }
Run Code Online (Sandbox Code Playgroud)

如果我使用嵌入式tomcat运行应用程序,dev配置文件将设置为活动状态,并且工作正常.但是当我单独部署tomcat时.这是行不通的.

我尝试在configure方法中设置活动配置文件.但是当我从上下文中获取环境时,我得到空指针异常.

有关如何设置活动配置文件的任何帮助.

spring-profiles tomcat8 spring-boot

11
推荐指数
3
解决办法
7060
查看次数

Spring MVC @Controller和配置文件

我有一个spring mvc控制器,我想只在某些配置文件中启用它(例如developmenttest).

我知道我可以在xml配置中使用元素的profile属性beans来限制我的bean的范围,但我现在正在为控制器使用方便的注释.

我可以以某种方式将带注释的控制器绑定到给定的配置文件吗?

或者我是否必须使用"旧方法"(实现和声明控制器)而不使用注释并beans在xml配置中使用该元素?

带注释的控制器是否会与"旧的控制器"很好地混合?

编辑:另一种方式,我想到的是从自动装配的环境实例检查运行时的配置文件,但这否定了控制的反转

java spring spring-mvc spring-profiles

10
推荐指数
1
解决办法
6149
查看次数

设置某个配置文件时不加载Spring bean

背景:所以,我有几个bean连接外部系统.对于开发,模拟外部系统并将接口bean替换为产生或多或少静态响应的一些实现是很方便的.所以我一直在做的是创建一个接口,真正的实现和这样的存根实现:

public interface ExternalService {
// ...
}

@Service
public class ExternalServiceImpl implements ExternalService {
// ...
}

@Service
@Primary
@Profile({"stub"})
public class StubExternalService implements ExternalService {
// ...
}
Run Code Online (Sandbox Code Playgroud)

...这很有效:如果存根配置文件不存在,则根本不会加载存根bean.如果它存在,它很好地取代了真正的实现,因为@Primary注释.

问题:然而,现在,我已经第一次运行了我实际上有两个相同接口的实际实现的情况.其中一个被定义为主要,但另一个也可以通过从应用程序上下文加载它来使用.

我仍然想创建一个存根服务来替换它们,但这次我将存根定义为@Primary的旧方法不起作用,因为已经有一个主要实现.基本上我需要的是在设置存根配置文件时不加载主bean的方法,但我不知道如何做到这一点.Web搜索或其他Stack Overflow问题似乎没有帮助.

java spring spring-profiles spring-bean

10
推荐指数
1
解决办法
6638
查看次数