目前我有一个Spring xml配置(Spring 4),它加载了一个属性文件.
context.properties
my.app.service = myService
my.app.other = ${my.app.service}/sample
Run Code Online (Sandbox Code Playgroud)
Spring xml配置
<bean id="contextProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="ignoreResourceNotFound" value="true" />
<property name="fileEncoding" value="UTF-8" />
<property name="locations">
<list>
<value>classpath:context.properties</value>
</list>
</property>
</bean>
<bean id="placeholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="properties" ref="contextProperties" />
<property name="nullValue" value="@null" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
</bean>
Run Code Online (Sandbox Code Playgroud)
使用属性的Bean
@Component
public class MyComponent {
@Value("${my.app.other}")
private String others;
}
Run Code Online (Sandbox Code Playgroud)
这是完美的,others
价值是MyService/sample
,例外.但是当我尝试用JavaConfig替换这个配置时@Value
,我的组件中的工作方式不同.值不myService/sample
但是${my.app.service}/sample
.
@Configuration
@PropertySource(name="contextProperties", ignoreResourceNotFound=true, value={"classpath:context.properties"})
public class PropertiesConfiguration { …
Run Code Online (Sandbox Code Playgroud) 目前,使用属性Spring Boot
为Thymeleaf
模板位置提供一个值spring.thymeleaf.prefix
.
默认值为
classpath:/templates/
.
我想在jar之外为百万富翁模板设置另一个位置(但保留默认值),例如:
spring.thymeleaf.prefix = classpath:/templates/, file:/resources/templates
我是否必须为我想要的新位置定义另一个模板解析器?
我正在尝试使用具有基于文件的存储库的Spring Cloud配置服务器在Spring Cloud客户端之间共享配置:
@Configuration
@EnableAutoConfiguration
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
// application.yml
server:
port: 8888
spring:
profiles:
active: native
test:
foo: world
Run Code Online (Sandbox Code Playgroud)
我的一个Spring Cloud客户端使用test.foo
配置服务器中定义的配置,其配置如下:
@SpringBootApplication
@RestController
public class HelloWorldServiceApplication {
@Value("${test.foo}")
private String foo;
@RequestMapping(path = "/", method = RequestMethod.GET)
@ResponseBody
public String helloWorld() {
return "Hello " + this.foo;
}
public static void main(String[] args) {
SpringApplication.run(HelloWorldServiceApplication.class, args);
}
}
// …
Run Code Online (Sandbox Code Playgroud)