属性文件未加载

时间:2013-01-31 06:10:37

标签: spring java-ee spring-mvc autowired

我有两个项目,CarpoolDB和Carpool。

CarpoolDB:包含后端内容并具有

拼车的应用程序-context.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 jar并放入Carpool Application

拼车:包含UI内容,后端联系carpoolDB jar,有

拼车的应用程序,context1.xml

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

弹簧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>

拼车的应用程序,context1.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 

弹簧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中正确注入。

我正在附加包含配置文件enter image description here

的位置的快照

1 个答案:

答案 0 :(得分:3)

<context:component-scan base-package="com.onmobile" />carpool-application-context.xml导致carpool-application-context1.xml"com.onmobile"导入,因此强制您的控制器在根网络应用上下文中创建,因为"com.onmobile.carpool.controller"包含property-placeholder 1}},the root context没有context:property-placeholder个配置。

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

作为修复,您可以在组件扫描中使用过滤器表达式仅在spring-servlet.xml中选择carpool-application-context.xml带注释的类,并将其从carpool-application-context1.xml / {{1}中排除}

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

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

使用{{1在@Controller包中。我的建议是将您的服务移动到根Web应用程序上下文(carpool-application-context1.xml)并将属性占位符配置器声明放在那里。