Srping配置文件属性注入

2024-05-10 20:48

本文主要是介绍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配置文件属性注入的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/977518

相关文章

Nginx 重写与重定向配置方法

《Nginx重写与重定向配置方法》Nginx重写与重定向区别:重写修改路径(客户端无感知),重定向跳转新URL(客户端感知),try_files检查文件/目录存在性,return301直接返回永久重... 目录一.try_files指令二.return指令三.rewrite指令区分重写与重定向重写: 请求

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

Redis Cluster模式配置

《RedisCluster模式配置》:本文主要介绍RedisCluster模式配置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录分片 一、分片的本质与核心价值二、分片实现方案对比 ‌三、分片算法详解1. ‌范围分片(顺序分片)‌2. ‌哈希分片3. ‌虚

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示

Springboot3+将ID转为JSON字符串的详细配置方案

《Springboot3+将ID转为JSON字符串的详细配置方案》:本文主要介绍纯后端实现Long/BigIntegerID转为JSON字符串的详细配置方案,s基于SpringBoot3+和Spr... 目录1. 添加依赖2. 全局 Jackson 配置3. 精准控制(可选)4. OpenAPI (Spri

maven私服配置全过程

《maven私服配置全过程》:本文主要介绍maven私服配置全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录使用Nexus作为 公司maven私服maven 私服setttings配置maven项目 pom配置测试效果总结使用Nexus作为 公司maven私

springboot加载不到nacos配置中心的配置问题处理

《springboot加载不到nacos配置中心的配置问题处理》:本文主要介绍springboot加载不到nacos配置中心的配置问题处理,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录springboot加载不到nacos配置中心的配置两种可能Spring Boot 版本Nacos