方法级别的Spring配置文件?

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

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

 spring.profiles.active=dev 

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

 class MyService { void run() { log(); } @Profile("dev") void log() { //only during dev } } 

您可以在http://docs.spring.io/spring/docs/3.1.x/javadoc-api/org/springframework/context/annotation/Profile.html上阅读。

@Profile注释可以通过以下任何方式使用:

作为直接或间接使用@Component注释的任何类的类型级注释,包括@Configuration类作为元注释,用于编写自定义构造型注释如果@Configuration类用@Profile标记,则所有@除非一个或多个指定的配置文件处于活动状态,否则将绕过与该类关联的Bean方法和@Import注释。 这与Spring XML中的行为非常相似:如果提供了beans元素的profile属性,则除非已激活配置文件’p1’和/或’p2’,否则不会解析beans元素。 同样,如果使用@Profile({“p1”,“p2”})标记@Component或@Configuration类,除非已激活配置文件’p1’和/或’p2’,否则不会注册/处理该类。

因此,类的@Profile注释适用于所有方法和导入。 不上课。

你可能想要做的是通过让两个类实现相同的接口,并根据配置文件注入一个或另一个来实现。 看看这个问题的答案。

注释驱动的dependency injection,处理不同的环境

对于不希望使用@Profile注释多个@Beans未来读者,这也可以是一个解决方案:

 class MyService { @Autowired Environment env; void run() { if (Arrays.asList(env.getActiveProfiles()).contains("dev")) { log(); } } void log() { //only during dev } } 

只想添加一个答案,说明当前弹簧在方法级别上是可能的,这是明显错误的。 通常在方法上使用@Profile是行不通的 – 它将使用的唯一方法是在@Configuration类中使用@Bean注释。

我使用Spring 4.2.4进行快速测试,在那里可以看到

  • @Profile在@Configuration类的bean创建方法中起作用
  • bean的方法中的@Profile不起作用(并且预计不起作用 – 文档有点模棱两可)
  • 当且仅当它与bean定义在相同的上下文中时,在配置类中或者如果使用上下文扫描时,类级别@Profile才有效
  • env.acceptsProfiles(“profile”),Arrays.asList(env.getActiveProfiles())。contains(“profile”)的作品

测试类:

 import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Profile; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.Arrays; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { ProfileTest.ProfileTestConfiguration.class }) @ActiveProfiles("test") public class ProfileTest { static class SomeClass {} static class OtherClass {} static class ThirdClass { @Profile("test") public void method() {} } static class FourthClass { @Profile("!test") public void method() {} } static class ProfileTestConfiguration { @Bean @Profile("test") SomeClass someClass() { return new SomeClass(); } @Bean @Profile("!test") OtherClass otherClass() { return new OtherClass(); } @Bean ThirdClass thirdClass() { return new ThirdClass(); } @Bean FourthClass fourthClass() { return new FourthClass(); } } @Autowired ApplicationContext context; @Test public void testProfileAnnotationIncludeClass() { context.getBean(SomeClass.class); } @Test(expected = NoSuchBeanDefinitionException.class) public void testProfileAnnotationExcludeClass() { context.getBean(OtherClass.class); } @Test public void testProfileAnnotationIncludeMethod() { context.getBean(ThirdClass.class).method(); } @Test(expected = Exception.class) // fails public void testProfileAnnotationExcludeMethod() { context.getBean(FourthClass.class).method(); } } 

可能在4.1

@Profile注释可以通过以下任何方式使用:

作为直接或间接使用@Component注释的任何类的类型级注释,包括@Configuration类。 作为元注释,用于组成自定义构造型注释。 作为任何@Bean方法的方法级注释

http://docs.spring.io/spring/docs/4.1.x/javadoc-api/org/springframework/context/annotation/Profile.html

@Profile可以与方法一起使用,也可以与基于Java的配置一起使用

例如,PostgreSQL(LocalDateTime)和HSQLDB(2.4.0时间戳之前)的单独DB时间戳:

 @Autowired private Function dateTimeExtractor; @Bean @Profile("hsqldb") public Function getTimestamp() { return Timestamp::valueOf; } @Bean @Profile("postgres") public Function getLocalDateTime() { return dt -> dt; } 

另请参阅Spring Profiles示例 :3。更多…(示例方法级别的Spring @Profile示例)