我在将属性文件添加到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标题中添加了相应的“context”模式定义。
另外还有一个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;
我在这里缺少什么? 谢谢!
答案 0 :(得分: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