spring4.x依赖注入方法(DI)

2024-02-20 09:18
文章标签 依赖 方法 注入 di spring4

本文主要是介绍spring4.x依赖注入方法(DI),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.DI方法

属性注入(Setter方法注入)
构造器注入
工厂方法注入(很少使用,不推荐)

2.属性注入(Setter方法注入)

applicationContext-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/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><bean id="OutputHelper" class="cn.com.pojo.OutputHelper"><property name="outputGenerator"><ref bean="CsvOutputGenerator" /></property></bean><bean id="CsvOutputGenerator" class="cn.com.pojo.CsvOutputGenerator" /><bean id="JsonOutputGenerator" class="cn.com.pojo.JsonOutputGenerator" /></beans>

OutputHelper.java

package cn.com.pojo.output;import com.yiibai.output.IOutputGenerator;public class OutputHelper
{IOutputGenerator outputGenerator;public void setOutputGenerator(IOutputGenerator outputGenerator){this.outputGenerator = outputGenerator;}}

调用方法Main.java

package cn.com.pojo;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {//1. 创建 Spring 的 IOC 容器ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-bean.xml");//2. 从 IOC 容器中获取 bean 的实例OutputHelper outputHelper = (OutputHelper) ctx.getBean("OutputHelper");//根据类型来获取 bean 的实例: 要求在  IOC 容器中只有一个与之类型匹配的 bean, 若有多个则会抛出异常. //一般情况下, 该方法可用, 因为一般情况下, 在一个 IOC 容器中一个类型对应的 bean 也只有一个. //      OutputHelper outputHelper = ctx.getBean(OutputHelper.class);//3. 使用 beanSystem.out.println(outputHelper)}   
}

3.构造器注入
applicationContext-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/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><bean id="CustomerBean" class="cn.com.pojo.Customer"><!-- 应该为构造函数指定的确切数据类型--><constructor-arg type="java.lang.String"><value>yiibai</value></constructor-arg><constructor-arg type="java.lang.String"><value>188</value></constructor-arg><constructor-arg type="int"><value>28</value></constructor-arg></bean></beans>

Customer.java

package cn.com.pojo;public class Customer 
{private String name;private String address;private int age;public Customer(String name, String address, int age) {this.name = name;this.address = address;this.age = age;}public Customer(String name, int age, String address) {this.name = name;this.age = age;this.address = address;}//getter and setter methodspublic String toString(){return " name : " +name + "\n address : "+ address + "\n age : " + age;}}

运行类:App .java

package cn.com.pojo;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class App 
{public static void main( String[] args ){ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});Customer cust = (Customer)context.getBean("CustomerBean");System.out.println(cust);// name : yiibai// address : 28// age : 188}
}

4工厂方法注入(很少使用,不推荐)

这篇关于spring4.x依赖注入方法(DI)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

Conda虚拟环境的复制和迁移的四种方法实现

《Conda虚拟环境的复制和迁移的四种方法实现》本文主要介绍了Conda虚拟环境的复制和迁移的四种方法实现,包括requirements.txt,environment.yml,conda-pack,... 目录在本机复制Conda虚拟环境相同操作系统之间复制环境方法一:requirements.txt方法

Nginx 重写与重定向配置方法

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

MySQL 打开binlog日志的方法及注意事项

《MySQL打开binlog日志的方法及注意事项》本文给大家介绍MySQL打开binlog日志的方法及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、默认状态二、如何检查 binlog 状态三、如何开启 binlog3.1 临时开启(重启后失效)

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python pip下载包及所有依赖到指定文件夹的步骤说明

《Pythonpip下载包及所有依赖到指定文件夹的步骤说明》为了方便开发和部署,我们常常需要将Python项目所依赖的第三方包导出到本地文件夹中,:本文主要介绍Pythonpip下载包及所有依... 目录步骤说明命令格式示例参数说明离线安装方法注意事项总结要使用pip下载包及其所有依赖到指定文件夹,请按照以

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

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