【问题标题】:Properties file not getting loaded属性文件未加载
【发布时间】:2013-01-15 04:25:34
【问题描述】:

我有两个项目,CarpoolDB 和 Carpool。

CarpoolDB : 包含后端的东西并且有

拼车应用程序上下文.xml

<context:annotation-config />
<context:component-scan base-package="com.onmobile" />
<context:property-placeholder location="classpath:server.properties" />

server.properties

cm.db.driverClassName=com.mysql.jdbc.Driver
cm.db.url=jdbc:mysql://localhost:3306/carpool1
cm.db.username=abc
cm.db.password=xyz

我制作了一罐 carpoolDB 并放在 Carpool Application 中

拼车:包含 UI 的东西和后端联系人 carpoolDB jar 并有

拼车应用程序上下文1.xml

<import resource="classpath:carpool-application-context.xml"/>
<context:annotation-config />
<context:component-scan base-package="com.onmobile.carpool.authentication" />

spring-servlet.xml

<context:property-placeholder location="classpath:carpool.properties" /> 

<context:annotation-config />
<context:component-scan base-package="com.onmobile.carpool.controller, com.onmobile.carpool.util" />

carpool.properties

cm.email.sender.mail.smtp.host=mail.on.com

现在,我有一个类 com.onmobile.carpool.util.EmailSender,它有一个属性 smtpHost,并希望 Spring 使用 @Value 注入该值,但它没有被注入。

@Controller
public class EmailSender {

    public static final Log logger = LogFactory.getLog(EmailSender.class);

    @Value("${cm.email.sender.mail.smtp.host}")
    private String smtpHost;

}

我收到错误

java.lang.IllegalArgumentException: Could not resolve placeholder 'cm.email.sender.mail.smtp.host'

carpool.properties 存在于 src 文件夹中。

为什么它没有从 carpool.properties 文件中选择 cm.email.sender.mail.smtp.host。与jar文件中存在的属性文件有任何关系。

实际上属性文件已加载,因为我在日志中看不到文件未找到但字段未自动连接。



删除导入后发布更新的完整配置文件

web.xml

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
            /WEB-INF/carpool-application-context1.xml
            /WEB-INF/applicationContext-security.xml
        </param-value>
  </context-param>
  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>
            org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/jsp/*</url-pattern>
  </servlet-mapping>
  <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <listener>
    <listener-class>
        org.springframework.security.web.session.HttpSessionEventPublisher
        </listener-class>
  </listener>

拼车应用程序上下文1.xml

<!-- Configure annotated beans --> 
<context:annotation-config />
<context:component-scan base-package="com.onmobile.carpooldb.db" />
<context:property-placeholder location="classpath:carpool.properties" />


   <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <beans:property name="driverClassName"><beans:value>${cm.db.driverClassName}</beans:value></beans:property>
        <beans:property name="url"><beans:value>${cm.db.url}</beans:value></beans:property>
        <beans:property name="username"><beans:value>${cm.db.username}</beans:value></beans:property>
        <beans:property name="password"><beans:value>${cm.db.password}</beans:value></beans:property>
        <beans:property name="testOnBorrow"><beans:value>true</beans:value></beans:property>
        <beans:property name="testOnReturn"><beans:value>true</beans:value></beans:property>
        <beans:property name="validationQuery"><beans:value>select 1</beans:value></beans:property>
        <beans:property name="maxIdle"><beans:value>-1</beans:value></beans:property>
        <beans:property name="maxActive"><beans:value>-1</beans:value></beans:property>
        <beans:property name="maxOpenPreparedStatements"><beans:value>-1</beans:value></beans:property>
        <beans:property name="maxWait"><beans:value>30000</beans:value></beans:property>
    </beans:bean>

    //session factory bean and other configuration 

spring-servlet.xml

<context:property-placeholder location="classpath:carpool.properties" />

<context:annotation-config />
<context:component-scan base-package="com.onmobile.carpool.authentication, com.onmobile.carpool.controller, com.onmobile.carpool.util" />


<!-- Declare a view resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="100000"/>
</bean>

carpool.properties

cm.db.driverClassName=com.mysql.jdbc.Driver
cm.db.url=jdbc:mysql://localhost:3306/carpool
cm.db.username=abc
cm.db.password=xyz

user.profile.pic.base.folder=D:\\carpoolRepository\\carpoolProfilePicUpload
google.places.autocomplete.response.xml.base.folder=D:\\carpoolRepository\\googleMapXML

#EMAIL - FORGOT PASSWORD 
cm.email.sender.mail.smtp.host=mail.on.com

我试图在上面提到的 EmailSender“smtpHost”属性方式中注入 cm.email.sender.mail.smtp.host 的值,当我读到它时它显示为 null。 其他属性如 cm.db.driverClassName 等被正确注入 carpool-application-context1.xml。

我正在附上包含配置文件位置的快照

【问题讨论】:

  • 您是否遇到同样的异常?
  • 考虑使用实际的 web.xml 和配置文件发布问题的更新
  • 感谢您的帮助鲍里斯。我已经用所有配置文件更新了我的帖子。我也没有得到任何异常,但 EmailSender“smtpHost”属性没有被正确的值注入。

标签: spring jakarta-ee spring-mvc autowired


【解决方案1】:

carpool-application-context.xml 中有 &lt;context:component-scan base-package="com.onmobile" /&gt;,它是从 carpool-application-context1.xml 导入的,这会强制在根 Web 应用程序上下文中创建控制器,因为 "com.onmobile" 包含 "com.onmobile.carpool.controller",并且没有 @987654328 @配置为the root context

您在 servlet 上下文 (spring-servlet.xml) 中有一个属性占位符配置器。属性占位符配置器(由context:property-placeholder 标签定义)是 bean 后处理器,并在每个容器的基础上工作,因此它们无法修改未定义的上下文的 bean 定义。因此它无法修改控制器的 bean 定义在根上下文中声明的实例(carpool-application-context.xml、carpool-application-context1.xml)。因此,由于双重扫描,您的控制器被创建了两次 - 在 root 和 servlet 上下文中,并且只有一个由正确的占位符配置器处理。

作为修复,您可以在组件扫描中使用过滤器表达式仅在 spring-servlet.xml 中提取 @Controller 注释类,并将其从 carpool-application-context.xml/carpool-application-context1.xml 中排除

有关过滤器的示例,请参阅@Service are constructed twice

请保持您的 Spring 配置简单,您的配置非常令人费解。

更新您混淆了控制器(用@Controller 注释,我认为应该更好地放入aaa.bbb.controllers 包中)与应该在@987654337 中用@Service 注释的服务@ 包裹。我的建议是将您的服务移动到根 Web 应用程序上下文(carpool-application-context1.xml)并将属性占位符配置器声明放在那里。

【讨论】:

  • “请保持您的 Spring 配置简单,您的配置非常令人费解。” +1。
  • 嗨,鲍里斯,感谢您解释并指出有助于解决问题的参考资料,但我仍然不太清楚。你能详细说明你的第一段......请它会很有帮助......谢谢
  • 标签好像被markdown吞噬了,等我到PC上去修。问题是第一个配置文件扫描“com.onmobile”,其中包括具有值注释的控制器类。配置器未在根上下文中定义(处理 appcontext 和 appcontext1 文件),情况与答案的第一个链接类似。根上下文中没有占位符配置器,servlet 上下文中的配置器无法对在根上下文中错误创建的控制器执行任何操作,因为后处理器在每个应用程序上下文的基础上工作。
  • 嗨,鲍里斯,非常感谢您的快速回复。我已经重组了我的应用程序,现在我只有 carpool-application-context1.xml 和数据 和 spring-servlet.xml 数据
  • 我仍然面临@Value 不起作用的问题。我应该怎么做才能让它发挥作用。
猜你喜欢
  • 2011-06-14
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 2020-02-13
  • 1970-01-01
  • 2015-12-15
  • 2019-03-04
相关资源
最近更新 更多