本文主要是介绍Srping配置文件属性注入,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
GITHUB:https://github.com/a422478514/java-practice/tree/master/src/main/java/com/daquan/_202007/_01/spring/propertyplaceholder
Spring中可以通过两种方式把资源文件中properties里的key-value注入到bean中。
第一种:xml方式
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsd"><context:annotation-config /><!--自动扫描含有@Service将其注入为bean --><context:component-scan base-package="com.daquan._202007._01.spring.propertyplaceholder" /><!--注入属性值,xml方式实现--><context:property-placeholder location="classpath:placeholder.properties" /><bean id="myPropertyServiceBean" class="com.daquan._202007._01.spring.propertyplaceholder.XmlMyPropertyServiceBean"><property name="id" value="${id}"/><property name="name" value="${name}"/></bean>
</beans>
第二种:注解方式
/*** 注解注入属性值,通过注解注入bean和属性*/
@Service
@PropertySource(value = "classpath:placeholder.properties",ignoreResourceNotFound = true)
public class AnnotationMyPropertyServiceBean {@Value("${id}")private int id;@Value("${name}")private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}
以下为xml配置和注解配置的完整代码示例:
AnnotationMyPropertyServiceBean.java
package com.daquan._202007._01.spring.propertyplaceholder;import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;/*** 注解注入属性值,通过注解注入bean和属性*/
@Service
@PropertySource(value = "classpath:placeholder.properties",ignoreResourceNotFound = true)
public class AnnotationMyPropertyServiceBean {@Value("${id}")private int id;@Value("${name}")private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}
XmlMyPropertyServiceBean.java
package com.daquan._202007._01.spring.propertyplaceholder;/*** 通过xml方式配置bean以及注入*/
public class XmlMyPropertyServiceBean {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}
TestMyPropertyServiceBean.java
package com.daquan._202007._01.spring.propertyplaceholder;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestMyPropertyServiceBean {public static void main(String[] args) {System.out.println("加载spring容器");ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("application.xml");//xml方式XmlMyPropertyServiceBean myPropertyServiceBean = (XmlMyPropertyServiceBean) classPathXmlApplicationContext.getBean("myPropertyServiceBean");System.out.println(myPropertyServiceBean.getId());System.out.println(myPropertyServiceBean.getName());//注解方式AnnotationMyPropertyServiceBean annotationMyPropertyServiceBean = (AnnotationMyPropertyServiceBean) classPathXmlApplicationContext.getBean("annotationMyPropertyServiceBean");System.out.println(annotationMyPropertyServiceBean.getId());}
}
application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsd"><context:annotation-config /><!--自动扫描含有@Service将其注入为bean --><context:component-scan base-package="com.daquan._202007._01.spring.propertyplaceholder" /><!--注入属性值,xml方式实现--><context:property-placeholder location="classpath:placeholder.properties" /><bean id="myPropertyServiceBean" class="com.daquan._202007._01.spring.propertyplaceholder.XmlMyPropertyServiceBean"><property name="id" value="${id}"/><property name="name" value="${name}"/></bean><!--如果存在多个配置文件,可以如下配置。在Spring3.1版本之前是通过PropertyPlaceholderConfigurer实现的。而3.1之后则是通过PropertySourcesPlaceholderConfigurer实现的,注意他们不在同一个包下。--><!--<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:placeholder.properties</value></list></property></bean>-->
</beans>
如果存在多个配置文件:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:placeholder_1.properties</value><value>classpath:placeholder_2.properties</value><value>classpath:placeholder_3.properties</value><value>classpath:placeholder_4.properties</value></list></property>
</bean>
这篇关于Srping配置文件属性注入的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!