我有一个spring mvc控制器,我想只在某些配置文件中启用它(例如development
和test
).
我知道我可以在xml配置中使用元素的profile
属性beans
来限制我的bean的范围,但我现在正在为控制器使用方便的注释.
我可以以某种方式将带注释的控制器绑定到给定的配置文件吗?
或者我是否必须使用"旧方法"(实现和声明控制器)而不使用注释并beans
在xml配置中使用该元素?
带注释的控制器是否会与"旧的控制器"很好地混合?
编辑:另一种方式,我想到的是从自动装配的环境实例检查运行时的配置文件,但这否定了控制的反转
当我使用经典的上下文加载在Eclipse上运行我的应用程序时,不用担心,所选择的Spring Profile对应的配置类上的bean定义是正确的.
public class BasketHandlerLoader {
public static void main(String[] args) throws Exception {
@SuppressWarnings("resource")
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:config/spring/spring-archibald-basket-handler-context.xml");
context.registerShutdownHook();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我使用Spring Boot运行应用程序时,这些bean不会实例化.
@Configuration
@ImportResource("classpath:config/spring/spring-archibald-basket-handler-context.xml")
public class BasketHandlerLoader {
public static void main(String[] args) throws Exception {
SpringApplication.run(BasketHandlerLoader.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
这是"dev"Spring配置文件的java配置类:
@Configuration
@Profile("dev")
@EnableTransactionManagement
@PropertySources(value = { @PropertySource("classpath:filters/dev.properties") })
public class DevPersistenceConfig extends AbstractPersistenceConfig {
@Inject
private Environment env;
@Override
@Bean
public DataSource dataSource() {
return super.createDataSource(env);
}
@Override
public Properties hibernateProperties() {
return super.createHibernateProperties(env);
}
} …
Run Code Online (Sandbox Code Playgroud) 想检查 Spring boot 是否提供帮助来使用除文件之外的配置application.properties
文件。例如:可以是特定于配置文件的my-custom.properties文件,例如:
my-custom-dev.properties
用于开发配置文件my-custom-uat.properties
对于uat配置文件编辑:问题是,我有普通的 application-{env}.property 文件,除此之外,还有其他属性文件根据它们的数据内容(例如:用于日志记录的数据库特定属性,我想存储在`db-log.properties,我如何使其他文件配置文件敏感?
我正在攻读Spring Core认证,我对将配置文件用于JUnit测试有一些疑问.
所以我知道如果我用以下方式编写一个类:
@Profile("stub")
@Repository
public class StubAccountRepository implements AccountRepository {
private Logger logger = Logger.getLogger(StubAccountRepository.class);
private Map<String, Account> accountsByCreditCard = new HashMap<String, Account>();
/**
* Creates a single test account with two beneficiaries. Also logs creation
* so we know which repository we are using.
*/
public StubAccountRepository() {
logger.info("Creating " + getClass().getSimpleName());
Account account = new Account("123456789", "Keith and Keri Donald");
account.addBeneficiary("Annabelle", Percentage.valueOf("50%"));
account.addBeneficiary("Corgan", Percentage.valueOf("50%"));
accountsByCreditCard.put("1234123412341234", account);
}
public Account findByCreditCard(String creditCardNumber) …
Run Code Online (Sandbox Code Playgroud) 我有一个Spring启动Web应用程序.使用@Configurable注释通过java类配置应用程序.我介绍了两个配置文件:'install','normal'.如果安装配置文件处于活动状态,则不会加载任何需要数据库连接的Bean.我想创建一个控制器,用户可以在其中设置db连接参数.完成后我想将活动配置文件从'install'切换到'normal'并刷新应用程序上下文,这样Spring就可以初始化每个需要的bean数据库数据源.
我可以从代码中修改活动配置文件列表,没有问题,但是当我尝试刷新应用程序上下文时,我得到以下异常:
`java.lang.IllegalStateException:
GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once`
Run Code Online (Sandbox Code Playgroud)
这是我启动Spring启动应用程序的方式:
`new SpringApplicationBuilder().sources(MyApp.class)
.profiles("my-profile").build().run(args);`
Run Code Online (Sandbox Code Playgroud)
有没有人知道如何启动春季启动应用程序让你多次刷新应用程序上下文?
寻找在Springboot应用程序中配置多个Profile特定属性文件的最佳方法。下面是一个示例:
-resources
- application.properties
-开发
- application-dev.properties
- ldap-dev.properties
- quartz-dev.properties
- etc-dev.properties
-测试
- application-test.properties
- ldap
-test.properties-
石英-test.properties -etc -test.properties-
产品
-应用程序
-prod.properties
-ldap -prod.properties- 石英-prod.properties
-etc-prod.properties
application.properties和application-profile.properties文件正在加载。我正在寻找一种建议的方法来加载其他特定于配置文件的属性文件。我不确定是否有办法从基于配置文件的文件夹中加载所有属性文件?
我正在使用春季启动项目.
环境:
ch.qos.logback:logback-core:jar:1.1.5
ch.qos.logback:logback-classic:jar:1.1.5
org.springframework.boot:spring-boot-starter-logging:jar:1.3.3.RELEASE
Run Code Online (Sandbox Code Playgroud)
在我的项目中,我正在使用application.yml的属性(application-dev.yml和application-production.yml)
由于Logback Spring扩展在Spring之前启动,因此我无法将spring.profiles.active注入到logback.xml文件中.
这是我的logback.xml文件的更简单版本:
<configuration scan="true">
<property name="LOG_PATH" value="/var/log/" />
<property name="APP_NAME" value="xyz" />
<property name="PROFILE" value="-${spring.profiles.active}" />
<property name="CHARSET" value="utf-8" />
<property name="PATTERN" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" />
<appender name="APP-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}${APP_NAME}${PROFILE}.log</file>
<encoder>
<charset>${CHARSET}</charset>
<Pattern>${PATTERN}</Pattern>
</encoder>
</appender>
<logger name="a.b.c" level="INFO">
<appender-ref ref="APP-FILE" />
</logger>
<root level="INFO">
<appender-ref ref="APP-FILE"/>
</root>
Run Code Online (Sandbox Code Playgroud)
我正在寻找的PROFILE是属性spring.profiles.active.
我的目标是在目录/ var/log上有一个日志文件,文件xyz-dev或xyz-production,但我得到的是xyz-spring.profiles.active_IS_UNDEFINED.log.
处理办法:
1 - 使用如下组件:
@Component
public class InitializationService implements ApplicationListener<ContextRefreshedEvent> …
Run Code Online (Sandbox Code Playgroud) 需要通过带有弹簧轮廓的gradle进行测试.
gradle clean build
Run Code Online (Sandbox Code Playgroud)
我添加了任务:
task beforeTest() {
doLast {
System.setProperty("spring.profiles.active", "DEV")
}
}
test.dependsOn beforeTest
Run Code Online (Sandbox Code Playgroud)
我的测试定义是:
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("TestProfile")
public class SomeTest {
Run Code Online (Sandbox Code Playgroud)
但这种结构对我不起作用.
Gradle运行测试.
我想在一个cf push
名为 的自定义配置文件之后启动我的 spring-boot 应用程序my_profile
,但该应用程序始终使用默认的一个cloud
配置文件启动。如何指定要加载的确切配置文件?
我已经尝试在 manifest.yml 中添加环境变量,如下所示:
env:
SPRING_PROFILES_ACTIVE: my_profile
Run Code Online (Sandbox Code Playgroud)
但是应用程序加载了两个配置文件 ( cloud
& my_profile
)
您是否有解决方案来加载我的自定义配置文件而不集成默认配置文件?
我想为整个包设置一个配置文件名称,但我不知道如何做。如果哪里没有简单的方法,那么我必须用@Profile
注释标记包和子包中的每个类。
<context:component-scan/>
标签不支持这样的属性profile
,所以我不知道。
spring-profiles ×10
java ×9
spring ×8
spring-boot ×6
annotations ×1
gradle ×1
logback ×1
maven ×1
profiling ×1
refresh ×1
spring-mvc ×1