0

我在将属性文件添加到 spring 3.2.2 Web 应用程序时遇到问题。

我的 Web.xml:

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/mvc-dispatcher-servlet.xml,
                /WEB-INF/spring-security.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>


  <!-- Spring Security -->  
 <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
        <param-name>contextAttribute</param-name>
        <param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

我的 mvc-dispatcher-servlet.xml:

<mvc:default-servlet-handler />
<mvc:annotation-driven />
 <context:annotation-config/>
<context:property-placeholder location="/WEB-INF/application.properties"/>
<context:component-scan base-package="com.mypackage.controller" />
<context:component-scan base-package="com.mypackage.model" />
<context:component-scan base-package="com.mypackage.model.service" />
<context:component-scan base-package="com.mypackage.model.serviceImpl" />

我还在 xml 标头中添加了相应的“上下文”模式定义。

另外还有一个 spring-security.xml (我不在这里发布内容)。

我的类 com.mypackage.model.serviceImpl.FeatureServiceImpl 从 application.properties 读取,如下所示:

…
@Autowired
private Environment env;
env.getProperty("db.host");

我得到这个例外:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'featureController': Injection of autowired dependencies failed;
…
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.mypackage.model.serviceImpl.FeatureServiceImpl com.mypackage.controller.FeatureController.featureService;

我在这里想念什么?谢谢!

4

1 回答 1

1

这个

<context:property-placeholder location="/WEB-INF/application.properties"/>

意味着您可以db.host像这样访问该属性:

@Value("${db.host}")
private String dbHost;

application.properties 文件应该看起来像这样

db.host=myserver
db.url=jdbc:mysql://localhost:3306/myapp
db.pwd=mypassword
db.mymultiline = cheesey \
chips \
ketchup
db.another = 42
于 2013-04-17T15:53:00.633 回答