spring - 属性文件未加载

标签 spring jakarta-ee spring-mvc autowired

我有两个项目,CarpoolDB 和 Carpool。

CarpoolDB:包含后端内容并具有

拼车应用程序上下文.xml

<context:annotation-config />
<context:component-scan base-package="com.onmobile" />
<context:property-placeholder location="classpath: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 应用程序中

Carpool:包含 UI 内容和后端联系 carpoolDB jar 并具有

拼车-application-context1.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" />

拼车.属性

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

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

@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>

拼车-application-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 

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>

拼车.属性

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”属性中注入(inject) cm.email.sender.mail.smtp.host 的值,当我读到它时,它显示为 null。 cm.db.driverClassName 等其他属性已正确注入(inject) carpool-application-context1.xml 中。

我正在附加包含配置文件位置的快照 enter image description here

最佳答案

您有<context:component-scan base-package="com.onmobile" />carpool-application-context.xml这是从 carpool-application-context1.xml 导入的这样就强制在根 Web 应用程序上下文中创建 Controller ,因为 "com.onmobile"包括"com.onmobile.carpool.controller" ,并且没有 property-placeholder配置the root context .

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

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

参见@Service are constructed twice有关过滤器的示例

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

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

关于spring - 属性文件未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14619515/

相关文章:

java - HikariDataSource.getConnection() 在低流量时缓慢,在大流量时快速

java - 如何为 Spring Batch 应用响应式(Reactive)

java - GWT 和 Spring 集成

java - 当我注释 Controller 时,@RequestMapping 不适用于方法

java - 使用 Spring :eval inside hasRole

spring - 无法连接到 Command Metric Stream。在 Hystrix 仪表板问题中

java - 带有同步关键字的 Spring @Transactional 不起作用

java - @PostConstruct & 检查异常

java - 集群中的静态变量访问

mysql - 我想忽略成为@Transactional 的特定实体(使用 Spring/Hibernate)