我不确定我是否很好地了解Spring配置文件如何与yaml和属性文件一起使用。我试图混杂这两种类型的配置(这两个文件不共享任何配置),但是从yaml配置读取配置文件时遇到了问题。
我正在使用Spring 4.1.1
这是代码。这是context:property-placeholder配置:
<context:property-placeholder location="classpath:/job-config.properties" order="1"
ignore-unresolvable="true" ignore-resource-not-found="false"/>
<context:property-placeholder properties-ref="yamlProperties" order="2"
ignore-resource-not-found="false" ignore-unresolvable="true"/>
Run Code Online (Sandbox Code Playgroud)
其中yamlProperties是以下bean
<bean id="yamlProperties"
class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
<property name="resources"
value="file:${catalina.home}/properties/test.yml"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
这是test.yml
spring:
profiles.default: default
---
spring:
profiles: default
db:
url: jdbc:oracle:thin:@##hostname##:##port##:##SID##
usr: ##USER##
pwd: ##PWD##
---
spring:
profiles: development
db:
url: jdbc:oracle:thin:@##hostname##:##port##:##SID_DEVELOPMENT##
usr: ##USER_DEVELOPMENT##
pwd: ##PWD_DEVELOPMENT##
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我尝试通过以下方式配置(通过xml)我的数据源时:
<property name="url" value="${db.url}"/>
<property name="username" value="${db.usr}"/>
<property name="password" value="${db.pwd}"/>
Run Code Online (Sandbox Code Playgroud)
Spring始终使用YAML文件中的最后一个配置,而忽略该配置文件。我试图通过web.xml中的contex-parameter传递活动配置文件,或者直接将其传递给JVM(我实现了一个实现EnvironmentAware接口的bean,以获取活动/默认配置文件,并且是正确的),但看起来一切都很好,但是,当尝试注入值将忽略配置文件。
我相信使用property-placeholder上下文(带有订单)可以获得一个Property-placeholder,它是PropertySourcesPlaceholderConfigurer的一个实例,因此可以访问Environment,但是我无法理解为什么配置文件被忽略并且spring获得了最后的yaml文件配置。
我在63.6节http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html中添加了对文档的引用(spring-boot)。
提前致谢
spring yaml spring-profiles spring-properties property-placeholder
我正在尝试配置 Spring Boot 以便将 Tomcat 连接池连接到我的生产数据库。我的应用程序不是网络(我也有一些很难告诉春天)。
我有一个启动课程和另外 3 个课程
代码
@Configuration
@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
public class Starter {
private static Logger logger;
@Autowired
private static MyController controller;
public static void main(String[] args) {
// SpringApplication.setWebEnvironment(false);
SpringApplication.run(Starter.class, args);
LogbackConfigLoader lcl = new LogbackConfigLoader();
if (lcl.init()) {
logger = LoggerFactory.getLogger(Starter.class);
logger.debug("Initialized....");
}
else{
logger = LoggerFactory.getLogger(Starter.class);
}
logger.info(controller.getProva());
}
}
Run Code Online (Sandbox Code Playgroud)
这是配置`
@Configuration
@ConfigurationProperties(prefix="datasource.NIS")
public class NISDBConfiguration {
private String jdbcInterceptors;
private long validationInterval = 30000;
private org.apache.tomcat.jdbc.pool.DataSource pool;
@Value("${driver-class-name}")
private String driverClassName; …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 spring boot ws 来创建 SOAP 服务,但我面临异常“端点没有适配器......”。
这是我的 xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://nisws.it.fastweb"
targetNamespace="http://nisws.it.fastweb" elementFormDefault="qualified">
<xs:element name="doEventRequest" type="tns:doEventRequest"/>
<xs:element name="doEventResponse" type="tns:doEventResponse"/>
<xs:complexType name="doEventRequest">
<xs:sequence>
<xs:element name="header" type="tns:header"/>
<xs:element name="trackingbody" type="tns:trackingbody"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="header">
<xs:sequence>
<xs:element name="IdMessaggio" type="xs:string"/>
<xs:element name="NomeBE" type="xs:string"/>
<xs:element name="DataInserimentoBE" type="xs:string"/>
<xs:element name="Release" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="trackingbody">
<xs:all>
<xs:element name="Sorgente" type="xs:string"/>
<xs:element name="Parametri" type="tns:Parametri" minOccurs="0"/>
<xs:element name="Componenti" type="tns:Componenti" minOccurs="0"/>
</xs:all>
</xs:complexType>
<xs:complexType name="Parametri">
<xs:sequence>
<xs:element name="Parametro" type="tns:TagParametro" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="TagParametro">
<xs:sequence>
<xs:element name="Descrizione" type="xs:string"/> …Run Code Online (Sandbox Code Playgroud) 我正在编写一个公开REST API的简单微服务.所以我开始与泽西岛合作,当然我需要将我的对象注入球衣资源.基本上我有2个类定义了一组资源,其中一些需要使用另一个服务.
基本上我有:
public interface MyService {
String getServiceName();
void doService();
Run Code Online (Sandbox Code Playgroud)
}
此接口的2个实现(MyServiceBean和MyAlternativeServiceBean)
而且,据我所知阅读球衣文件,我定义了一个hk2 Binder:
public class MyBinder implements Binder{
@Override
public void bind(DynamicConfiguration config) {
DescriptorImpl descriptor = BuilderHelper.link(MyServiceBean.class).named("MyServiceBean").to(MyService.class).build();
config.bind(descriptor);
config.bind(BuilderHelper.link(MyAlternativeServiceBean.class).named("MyAlternativeServiceBean").to(MyService.class).build());
}
Run Code Online (Sandbox Code Playgroud)
我将此绑定程序注册到ApplicationConfig类
public class ApplicationConfig extends ResourceConfig{
public ApplicationConfig(){
property("property.value", "MyAlternativeServiceImplementation");
registerInstances(new MyBinder());
}
Run Code Online (Sandbox Code Playgroud)
}
并正确注释到资源中
@Path("first")
public class First {
@Inject @Named(value = "MyServiceBean")
private MyService myService;
//...
}
@Path("second")
public class Second {
@Inject @Named(value = "MyAlternativeServiceBean")
private MyService myService;
//...
}
Run Code Online (Sandbox Code Playgroud)
一直有效,直到MyService实现没有args构造函数.但在一种情况下,我还需要为MyAlternativeServiceBean提供依赖.
这是构造函数
@Inject @Named("property.value") …Run Code Online (Sandbox Code Playgroud) java ×3
spring ×3
spring-boot ×2
autowired ×1
hk2 ×1
jersey-2.0 ×1
rest ×1
soap ×1
spring-jdbc ×1
spring-ws ×1
yaml ×1