Reputation: 331
I have problems adding properties file to a spring 3.2.2 web application.
My 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>
My 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" />
I also added the corresponding 'context' schema definition to the xml header.
Additionally there is a spring-security.xml (I don't post the content here).
My class com.mypackage.model.serviceImpl.FeatureServiceImpl reads from the application.properties like this:
…
@Autowired
private Environment env;
env.getProperty("db.host");
I get this exception:
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;
What am I missing here? Thanks!
Upvotes: 0
Views: 1556
Reputation: 47290
This
<context:property-placeholder location="/WEB-INF/application.properties"/>
Means you can access the property db.host
like so:
@Value("${db.host}")
private String dbHost;
the application.properties file shoud look something like this
db.host=myserver
db.url=jdbc:mysql://localhost:3306/myapp
db.pwd=mypassword
db.mymultiline = cheesey \
chips \
ketchup
db.another = 42
Upvotes: 1