从PropertySourcesPlaceholderConfigurer

时间:2017-06-15 14:49:50

标签: java spring

长时间用户。希望是一个有用的问题: 我在Component类中自动装配属性。我在上下文xml中使用PropertySourcesPlaceholderConfigurer从属性文件中提取值。并在课堂上引用该属性:

 @Value("${my.url}")
 @Autowired
private transient String myURL;

价值得到解决并起作用;但是,我希望在实例化Bean时显示一次值。如果我在方法中记录值,它显示正常。但是如果在构造函数中它显示为null。这里我编写的内容在日志中显示为null:

 public MyHelper(){
    init();
}

private void init(){
    LOGGER.info("MY_URL...{}", myURL);

}

如果我记录它的方法,它会显示值(部分代码):

 public String getReport(final String report) throws ReportOrderingException {
    URL wsdlURL = null;
    try {

        wsdlURL = new URL(myURL);
        LOGGER.info("MY_URL...{}", myURL);
        }

这里是context.xml

     <bean id="propertyConfigurer"  class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:/my.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

这是启动日志,myURl是vendor.wsdl.url。它在解析属性之前创建bean:

[INFO]:[localhost-startStop-1] 2017-06-15 10:38:48.562 com.kemper.ros.helper.ExternalServiceHelper:VENDOR_WSDL_URL ... null [DEBUG]:[localhost-startStop-1] 2017-06-15 10:38:48.562 org.springframework.beans.factory.annotation.InjectionMetadata:在[com.kemper.ros.helper.ExternalServiceHelper]类上注册注入的元素:用于私有瞬态的AutowiredFieldElement java.lang.String com.kemper.ros.helper.ExternalServiceHelper.vendorWsdlURL [DEBUG]:[localhost-startStop-1] 2017-06-15 10:38:48.562 org.springframework.beans.factory.support.DefaultListableBeanFactory:急切地缓存bean&#39; externalServiceHelper&#39;允许解决潜在的循环引用 [DEBUG]:[localhost-startStop-1] 2017-06-15 10:38:48.562 org.springframework.beans.factory.annotation.InjectionMetadata:处理bean的注入元素&#39; externalServiceHelper&#39;:AutowiredFieldElement for private transient java.lang.String com.kemper.ros.helper.ExternalServiceHelper.vendorWsdlURL [DEBUG]:[localhost-startStop-1] 2017-06-15 10:38:48.563 org.springframework.jndi.JndiTemplate:查找名为[java:comp / env / vendor.wsdl.url]的JNDI对象 [DEBUG]:[localhost-startStop-1] 2017-06-15 10:38:48.563 org.springframework.jndi.JndiLocatorDelegate:转换的JNDI名称[java:comp / env / vendor.wsdl.url]未找到 - 尝试原创name [vendor.wsdl.url]。 javax.naming.NameNotFoundException:名称[vendor.wsdl.url]未绑定在此Context中。无法找到[vendor.wsdl.url]。 [DEBUG]:[localhost-startStop-1] 2017-06-15 10:38:48.563 org.springframework.jndi.JndiTemplate:查找名称为[vendor.wsdl.url]的JNDI对象 [DEBUG]:[localhost-startStop-1] 2017-06-15 10:38:48.563 org.springframework.jndi.JndiPropertySource:名称[vendor.wsdl.url]的JNDI查找抛出NamingException并显示消息:Name [vendor.wsdl .url]不受此上下文约束。无法找到[vendor.wsdl.url] ..返回null。 [DEBUG]:[localhost-startStop-1] 2017-06-15 10:38:48.563 org.springframework.core.env.PropertySourcesPropertyResolver:找不到键&#39; vendor.wsdl.url&#39;在任何财产来源 [DEBUG]:[localhost-startStop-1] 2017-06-15 10:38:48.563 org.springframework.core.env.PropertySourcesPropertyResolver:找到密钥&#39; vendor.wsdl.url&#39;在[localProperties]中,类型为[String]

0 个答案:

没有答案
相关问题