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

相关文章

IDEA中配置Tomcat全过程

《IDEA中配置Tomcat全过程》文章介绍了在IDEA中配置Tomcat的六步流程,包括添加服务器、配置部署选项、设置应用服务器及启动,并提及Maven依赖可能因约定大于配置导致问题,需检查依赖版本... 目录第一步第二步第三步第四步第五步第六步总结第一步选择这个方框第二步选择+号,找到Tomca

Win10安装Maven与环境变量配置过程

《Win10安装Maven与环境变量配置过程》本文介绍Maven的安装与配置方法,涵盖下载、环境变量设置、本地仓库及镜像配置,指导如何在IDEA中正确配置Maven,适用于Java及其他语言项目的构建... 目录Maven 是什么?一、下载二、安装三、配置环境四、验证测试五、配置本地仓库六、配置国内镜像地址

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Debian系和Redhat系防火墙配置方式

《Debian系和Redhat系防火墙配置方式》文章对比了Debian系UFW和Redhat系Firewalld防火墙的安装、启用禁用、端口管理、规则查看及注意事项,强调SSH端口需开放、规则持久化,... 目录Debian系UFW防火墙1. 安装2. 启用与禁用3. 基本命令4. 注意事项5. 示例配置R

PyCharm中配置PyQt的实现步骤

《PyCharm中配置PyQt的实现步骤》PyCharm是JetBrains推出的一款强大的PythonIDE,结合PyQt可以进行pythion高效开发桌面GUI应用程序,本文就来介绍一下PyCha... 目录1. 安装China编程PyQt1.PyQt 核心组件2. 基础 PyQt 应用程序结构3. 使用 Q

Redis MCP 安装与配置指南

《RedisMCP安装与配置指南》本文将详细介绍如何安装和配置RedisMCP,包括快速启动、源码安装、Docker安装、以及相关的配置参数和环境变量设置,感兴趣的朋友一起看看吧... 目录一、Redis MCP 简介二、安www.chinasem.cn装 Redis MCP 服务2.1 快速启动(推荐)2.

Java Spring的依赖注入理解及@Autowired用法示例详解

《JavaSpring的依赖注入理解及@Autowired用法示例详解》文章介绍了Spring依赖注入(DI)的概念、三种实现方式(构造器、Setter、字段注入),区分了@Autowired(注入... 目录一、什么是依赖注入(DI)?1. 定义2. 举个例子二、依赖注入的几种方式1. 构造器注入(Con

Spring Boot配置和使用两个数据源的实现步骤

《SpringBoot配置和使用两个数据源的实现步骤》本文详解SpringBoot配置双数据源方法,包含配置文件设置、Bean创建、事务管理器配置及@Qualifier注解使用,强调主数据源标记、代... 目录Spring Boot配置和使用两个数据源技术背景实现步骤1. 配置数据源信息2. 创建数据源Be

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter