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中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

javacv依赖太大导致jar包也大的解决办法

《javacv依赖太大导致jar包也大的解决办法》随着项目的复杂度和依赖关系的增加,打包后的JAR包可能会变得很大,:本文主要介绍javacv依赖太大导致jar包也大的解决办法,文中通过代码介绍的... 目录前言1.检查依赖2.更改依赖3.检查副依赖总结 前言最近在写项目时,用到了Javacv里的获取视频

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

Spring 依赖注入与循环依赖总结

《Spring依赖注入与循环依赖总结》这篇文章给大家介绍Spring依赖注入与循环依赖总结篇,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Spring 三级缓存解决循环依赖1. 创建UserService原始对象2. 将原始对象包装成工

504 Gateway Timeout网关超时的根源及完美解决方法

《504GatewayTimeout网关超时的根源及完美解决方法》在日常开发和运维过程中,504GatewayTimeout错误是常见的网络问题之一,尤其是在使用反向代理(如Nginx)或... 目录引言为什么会出现 504 错误?1. 探索 504 Gateway Timeout 错误的根源 1.1 后端