为了简化读取properties文件中的配置值,spring支持@value注解的方式来获取,这种方式大大简化了项目配置,提高业务中的灵活性。
一、两种使用方法
1、@Value("#{configProperties['key']}")
2、@Value("${key}")
二、配置
2.1 @Value("#{configProperties['key']}")使用
2.1.1配置文件:
- 配置方法1:
- <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
- <property name="locations">
- <list>
- <value>classpath:value.properties</value>
- </list>
- </property>
- </bean>
- 配置方法2:
- <util:properties id="configProperties" location="classpath:value.properties"></util:properties>
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd"
value.properties
- key=1
ValueDemo.java
- @Component
- public class ValueDemo {
- @Value("#{configProperties['key']}")
- private String value;
- public String getValue() {
- return value;
- }
- }
2.2 @Value("${key}")使用
2.2.1 配置文件
1、在2.1.1的配置文件基础上增加:
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
- <property name="properties" ref="configProperties"/>
- </bean>
或者使用PropertyPlaceholderConfigurer直接指定配置文件,完整的配置:
- <bean id="appProperty"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <array>
- <value>classpath:value.properties</value>
- </array>
- </property>
- </bean>
ValueDemo.java
- @Component
- public class ValueDemo {
- @Value("${key}")
- private String value;
- public String getValue() {
- return value;
- }
- }
- @Controller
- @RequestMapping("/value")
- public class ValuePropertyController extends ApplicationController{
- @Value("#{configProperties['jdbc.jdbcUrl']}")
- private String jdbcUrl;
- @RequestMapping
- public String value(){
- System.out.println(jdbcUrl);
- return "";
- }
- }
applicationContext.xml
- <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
- <property name="locations">
- <list>
- <value>classpath:database.properties</value>
- </list>
- </property>
- </bean>
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
- <property name="properties" ref="configProperties" />
- </bean>
database.properties
@Value("${key}")使用还有一种配置方法:
Spring : How To Load Properties File using @Value Annotation
This example shows how to load the properties file values using the @Value annotation. Accessing the property file values in the Spring application involves the following steps:
Add the property file details to spring bean configuration file. You have to use the “classpath” prefix if you want to load the files from the classpath.
Create a properties file in the same name that is configured in the XML file and put it under the classpath folders. In most scenarios, source folders will be by default kept under the classpath.
Use @Value annotation to get the property valye. @Value annotation takes the string parameter which is “key” used in the properties file. This annotation has to be used with variables to inject the value.
Lets look at the following example code:
1. Spring MVC Configurations
spring4-mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/websocket
http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd">
<context:component-scan base-package="com.javabeat.controller" />
<context:property-placeholder location="classpath:application.properties"/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
2. Application Properties File
application.properties
msg=Spring Application Properties!!
3. Spring MVC Controller
HelloController.java
package com.javabeat.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/welcome")
public class HelloController {
@Value("${msg}")
private String msg;
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("msg", this.msg);
return "hello";
}
}
4. Views
hello.jsp
<html>
<body>
Your Message : ${msg}
</body>
</html>
5. Web Deployment Descriptor
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Spring MVC 4.0 Web Application</display-name>
<servlet>
<servlet-name>spring4-mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring4-mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
6. Demo
If you run the above example, you would get the below output.