Spring 3.2属性文件自动布线失败
我在向Spring3.2.2Web应用程序添加属性文件时遇到问题 My Web.xml:Spring 3.2属性文件自动布线失败,spring,spring-mvc,annotations,Spring,Spring Mvc,Annotations,我在向Spring3.2.2Web应用程序添加属性文件时遇到问题 My Web.xml: <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param>
<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>
我得到一个例外:
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;
我错过了什么?
谢谢 这个
<context:property-placeholder location="/WEB-INF/application.properties"/>
application.properties文件应该如下所示
db.host=myserver
db.url=jdbc:mysql://localhost:3306/myapp
db.pwd=mypassword
db.mymultiline = cheesey \
chips \
ketchup
db.another = 42
非常感谢。我试过了,但没有发现编译错误。它读取文件并找到占位符。但是,这些值始终解析为null。我需要在我访问属性的类中添加其他内容吗?不,只需检查它的调用,你认为它的调用。我做了,它在运行时找到占位符。我知道这一点,因为当我将属性文件中的占位符重命名为其他占位符时,它会在运行时发出抱怨。我想知道这种注入是否只在控制器中起作用?我在另一个类中使用它,它不是@Controller。是的,它必须在SpringManagerNDbean中,例如Controller或Service。我的类用“@Service”注释,但这似乎不起作用。作为解决方法,我现在这样做了:我删除了属性placehodler,而是将其添加到我的FeatureServiceImpl类中:
Resource Resource=new ClassPathResource(“application.properties”);Properties Properties=PropertiesLoaderUtils.loadProperties(资源);字符串dbHost=properties.getProperty(“db.host”)代码>我仍然对如何实现第一种方法感兴趣。
<context:property-placeholder location="/WEB-INF/application.properties"/>
@Value("${db.host}")
private String dbHost;
db.host=myserver
db.url=jdbc:mysql://localhost:3306/myapp
db.pwd=mypassword
db.mymultiline = cheesey \
chips \
ketchup
db.another = 42