Spring @Value访问在父上下文中加载的属性(Spring @Value access to property loaded in parent context)

编程入门 行业动态 更新时间:2024-10-26 21:33:08
Spring @Value访问在父上下文中加载的属性(Spring @Value access to property loaded in parent context)

我一直在努力争取一段时间的财产,但我没有运气。 我的项目中有两个配置XML文件,一个是servlet配置,另一个是全局弹簧配置xml。

Servlet配置xml:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <task:annotation-driven /> <context:component-scan base-package="com.test.loadbalancer"></context:component-scan> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean>

Spring config xml:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration" xmlns:int-http="http://www.springframework.org/schema/integration/http" xmlns:int-amqp="http://www.springframework.org/schema/integration/amqp" xmlns:p="http://www.springframework.org/schema/p" xmlns:security="http://www.springframework.org/schema/security" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/integration/amqp http://www.springframework.org/schema/integration/amqp/spring-integration-amqp.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.1.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> <property name="ignoreUnresolvablePlaceholders" value="false"/> <property name="locations"> <list> <value>classpath:com/test/loadbalancer/config.properties</value> </list> </property> </bean> <rabbit:connection-factory id="connectionFactory" host="${RABBIT_MQ_HOST}" port="${RABBIT_MQ_PORT}" username="${RABBIT_MQ_USER}" password="${RABBIT_MQ_PASS}" /> <!-- This works fine --> </beans>

因此,spring xml上面的属性访问工作正常,因为属性文件在同一范围内加载。

这是我正在加载xml的web.xml:

<?xml version="1.0" encoding="UTF-8"?> <web-app metadata-complete="true" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>Test Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-config.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>test</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>test</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>

但是,我在servlet范围中有一个bean,我试图使用@Value,但它始终为空。

package com.test.loadbalancer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import com.test.micro.util.registry.NotificationMessage; @Component public class RegistryEndpointEnricher { private static final String defaultRetrieveServiceURL = "http://localhost:8090/registry/rest/retrieveServiceInfo"; private static final String defaultErrorServiceURL = "http://localhost:8090/registry/rest/notifyError"; @Value("${registry.retrieveserviceurl}") String registryRetrieveServicesUrl; @Value("${registry.errorserviceurl}") String registryErrorUrl; @Autowired private Environment env; public String getEndpoint(NotificationMessage nmMessage) { populateURLs(); String registryURl = null; switch (nmMessage.getNotificationType()) { case RETRIEVESERVICEINFO: registryURl = registryRetrieveServicesUrl; break; case ERROR: registryURl = registryErrorUrl; break; default: break; } return registryURl; } private void populateURLs() { if (StringUtils.isEmpty(registryRetrieveServicesUrl)) { registryRetrieveServicesUrl = defaultRetrieveServiceURL; } if (StringUtils.isEmpty(registryErrorUrl)) { registryErrorUrl = defaultErrorServiceURL; } } }

我调试时环境为空,与两个字符串属性相同。 也许我没有正确使用代码,但有没有一种方法来共享父级和servlet之间的属性?

PS我知道有一种方法可以在注释中提供默认值,但请忽略我自己手动完成的事实。

I have been struggling for some time now to get the properties to work, however I am having no luck. I have two configuration XML files in my project, one is a servlet config and the other is the global spring config xml.

Servlet config xml:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <task:annotation-driven /> <context:component-scan base-package="com.test.loadbalancer"></context:component-scan> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean>

Spring config xml:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration" xmlns:int-http="http://www.springframework.org/schema/integration/http" xmlns:int-amqp="http://www.springframework.org/schema/integration/amqp" xmlns:p="http://www.springframework.org/schema/p" xmlns:security="http://www.springframework.org/schema/security" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/integration/amqp http://www.springframework.org/schema/integration/amqp/spring-integration-amqp.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.1.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> <property name="ignoreUnresolvablePlaceholders" value="false"/> <property name="locations"> <list> <value>classpath:com/test/loadbalancer/config.properties</value> </list> </property> </bean> <rabbit:connection-factory id="connectionFactory" host="${RABBIT_MQ_HOST}" port="${RABBIT_MQ_PORT}" username="${RABBIT_MQ_USER}" password="${RABBIT_MQ_PASS}" /> <!-- This works fine --> </beans>

So the property access above in the spring xml works fine because the property file is loaded within the same scope.

Here is the web.xml where I'm loading the xml:

<?xml version="1.0" encoding="UTF-8"?> <web-app metadata-complete="true" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>Test Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-config.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>test</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>test</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>

However, I have a bean in the servlet scope where I'm trying to use @Value but it's always null.

package com.test.loadbalancer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import com.test.micro.util.registry.NotificationMessage; @Component public class RegistryEndpointEnricher { private static final String defaultRetrieveServiceURL = "http://localhost:8090/registry/rest/retrieveServiceInfo"; private static final String defaultErrorServiceURL = "http://localhost:8090/registry/rest/notifyError"; @Value("${registry.retrieveserviceurl}") String registryRetrieveServicesUrl; @Value("${registry.errorserviceurl}") String registryErrorUrl; @Autowired private Environment env; public String getEndpoint(NotificationMessage nmMessage) { populateURLs(); String registryURl = null; switch (nmMessage.getNotificationType()) { case RETRIEVESERVICEINFO: registryURl = registryRetrieveServicesUrl; break; case ERROR: registryURl = registryErrorUrl; break; default: break; } return registryURl; } private void populateURLs() { if (StringUtils.isEmpty(registryRetrieveServicesUrl)) { registryRetrieveServicesUrl = defaultRetrieveServiceURL; } if (StringUtils.isEmpty(registryErrorUrl)) { registryErrorUrl = defaultErrorServiceURL; } } }

Environment is null when I debug, same as both string properties. Maybe I am not using the code correctly but is there a way to share properties between the parent and servlet?

P.S I know that there's a way to provide a default value in the annotation, but please ignore the fact that I did it myself manually.

最满意答案

你应该:

一个。 添加默认值和

湾 您的默认值是一个包含冒号(:)的URL。 对此URL值使用单引号(或根据您的spring版本保留它。请参阅: 默认值中的Spring @Value转义冒号(:) )

@Value("${registry.retrieveserviceurl:'http://localhost:8090/registry/rest/retrieveServiceInfo'}") String registryRetrieveServicesUrl; @Value("${registry.errorserviceurl:'http://localhost:8090/registry/rest/notifyError'}") String registryErrorUrl;

或者对于Spring 4.2:

@Value("${registry.retrieveserviceurl:http://localhost:8090/registry/rest/retrieveServiceInfo}") String registryRetrieveServicesUrl; @Value("${registry.errorserviceurl:http://localhost:8090/registry/rest/notifyError}") String registryErrorUrl;

Turns out the issue was due to using the wrong property holder configurer in a dependency module I wrote. I was using ServletContextPropertyPlaceholderConfigurer and instead needed to use PropertySourcesPlaceholderConfigurer.

更多推荐

springframework,servlet,org,电脑培训,计算机培训,IT培训"/> <meta name="d

本文发布于:2023-07-30 12:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1337884.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文中   属性   加载   Spring   access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!