Spring属性注入的三种方式(超详细)

您所在的位置:网站首页 文件属性有哪三种 Spring属性注入的三种方式(超详细)

Spring属性注入的三种方式(超详细)

2023-07-02 05:06| 来源: 网络整理| 查看: 265

属性注入的三种方式 使用set方法进行注入使用有参构造函数进行注入使用p名称空间注入

首先了解下面两个名词的含义:

IOC:控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入(Dependency Injection,简称DI)。通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体将其所依赖的对象的引用传递给它。也可以说,依赖被注入到对象中。

DI:依赖注入(Dependency Injection,简称DI),就是注入属性。是Spring框架的核心之一。所谓依赖注入,是指程序运行过程中,如果需要调用另一个对象协助时,无须在代码中创建被调用者,而是依赖于外部的注入。Spring的依赖注入对调用者和被调用者几乎没有任何要求,完全支持对POJO之间依赖关系的管理。

DI是IOC中的一种具体实现。

使用set方法进行注入

Book类:

package com.Keafmd.spring5; /** * Keafmd * * @ClassName: Book * @Description: * @author: 牛哄哄的柯南 * @date: 2021-01-14 21:46 */ /** * 演示使用set方法进行注入属性 */ public class Book { //创建属性 private String bname; private String bauthor; private String address; //1、set方法注入 //创建对应的set方法 public void setBname(String bname) { this.bname = bname; } public void setBauthor(String bauthor) { this.bauthor = bauthor; } public void setAddress(String address) { this.address = address; } public void testdemo(){ System.out.println(bname+" "+bauthor+" "+address); } }

bean1.xml:

>]]>

测试代码:

package com.Keafmd.spring5.testdemo; import com.Keafmd.spring5.Book; import com.Keafmd.spring5.Orders; import com.Keafmd.spring5.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Keafmd * * @ClassName: TestSpring5 * @Description: * @author: 牛哄哄的柯南 * @date: 2021-01-14 20:06 */ public class TestSpring5 { @Test public void testBook(){ //1、载Spring的配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml"); //2、获取配置文件中创建的对象 默认是执行无参的构造方法创建 Book book =applicationContext.getBean("book", Book.class); book.testdemo(); } }

输出结果:

书名 作者 Process finished with exit code 0 使用有参构造函数进行注入

Orders类:

package com.Keafmd.spring5; /** * Keafmd * * @ClassName: Orders * @Description: * @author: 牛哄哄的柯南 * @date: 2021-01-14 22:05 */ /** * 使用有参构造注入 */ public class Orders { private String oname; private String address; public Orders(String oname, String address) { this.oname = oname; this.address = address; } public void ordersTest(){ System.out.println(oname+" "+address); } }

bean1.xml

测试代码:

package com.Keafmd.spring5.testdemo; import com.Keafmd.spring5.Book; import com.Keafmd.spring5.Orders; import com.Keafmd.spring5.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Keafmd * * @ClassName: TestSpring5 * @Description: * @author: 牛哄哄的柯南 * @date: 2021-01-14 20:06 */ public class TestSpring5 { @Test public void testOrders(){ //1、载Spring的配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml"); //2、获取配置文件中创建的对象 默认是执行无参的构造方法创建 Orders orders =applicationContext.getBean("orders", Orders.class); orders.ordersTest(); } }

输出结果:

电脑 China Process finished with exit code 0 使用p名称空间注入

第一步:添加p名称空间xmlns:p="http://www.springframework.org/schema/p"。 第二步:进行属性注入,在bean标签里。

Book类:

package com.Keafmd.spring5; /** * Keafmd * * @ClassName: Book * @Description: * @author: 牛哄哄的柯南 * @date: 2021-01-14 21:46 */ /** * 演示使用p名称空间注入属性 */ public class Book { private String bname; private String bauthor; private String address; //3、p名称空间注入 public void setBname(String bname) { this.bname = bname; } public void setBauthor(String bauthor) { this.bauthor = bauthor; } public void setAddress(String address) { this.address = address; } public void testdemo(){ System.out.println(bname+" "+bauthor+" "+address); } }

bean1.xml:

测试代码:

package com.Keafmd.spring5.testdemo; import com.Keafmd.spring5.Book; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Keafmd * * @ClassName: TestSpring5 * @Description: * @author: 牛哄哄的柯南 * @date: 2021-01-14 20:06 */ public class TestSpring5 { @Test public void testBook(){ //1、载Spring的配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml"); //2、获取配置文件中创建的对象 默认是执行无参的构造方法创建 Book book =applicationContext.getBean("book", Book.class); book.testdemo(); } }

输出结果:

书名XXX 作者XXX 地址XXX Process finished with exit code 0

以上就是Spring属性注入的三种方式。

看完如果对你有帮助,感谢点赞支持! 如果你是电脑端的话,看到右下角的 “一键三连” 了吗,没错点它[哈哈] 在这里插入图片描述

加油!

共同努力!

Keafmd



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3