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

举报
牛哄哄的柯南 发表于 2021/05/26 16:39:43 2021/05/26
【摘要】 属性注入的三种方式 使用set方法进行注入使用有参构造函数进行注入使用p名称空间注入 首先了解下面两个名词的含义: IOC:控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入(Dependency Injection,简称DI)。通过控...

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

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); }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

bean1.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 一、set方法注入属性--> <!-- 1、配置对象的创建--> <bean id="book" class="com.Keafmd.spring5.Book"> <!-- 2、使用property完成属性注入 name:类里面的属性名称 value:向属性中注入的值 --> <property name="bname" value="书名"></property> <property name="bauthor" value="作者"></property> <!--  向属性注入null值--> <!--<property name="address" > <null/> </property>--> <!--属性值包含特殊符号 1.把<>进行转义 2.把带特殊符号的内容写到CDATA --> <!-- <property name="address" value="&lt;&lt;北京&gt;&gt;"></property> --> <property name="address"> <value><![CDATA[<<北京>>]]></value> </property> </bean>

</beans>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

测试代码:

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(); }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

输出结果:

书名 作者 <<北京>>

Process finished with exit code 0

  
 
  • 1
  • 2
  • 3

使用有参构造函数进行注入

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); }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

bean1.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--二、有参构造注入属性--> <bean id="orders" class="com.Keafmd.spring5.Orders"> <constructor-arg name="oname" value="电脑"/> <constructor-arg name="address" value="China"/> </bean>

</beans>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

测试代码:

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(); }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

输出结果:

电脑 China

Process finished with exit code 0

  
 
  • 1
  • 2
  • 3

使用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); }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

bean1.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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--三、p名称空间注入属性--> <bean id="book" class="com.Keafmd.spring5.Book" p:bname="书名XXX" p:bauthor="作者XXX" p:address="地址XXX"> </bean>

</beans>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

测试代码:

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(); }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

输出结果:

书名XXX 作者XXX 地址XXX

Process finished with exit code 0

  
 
  • 1
  • 2
  • 3

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

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

在这里插入图片描述

加油!

共同努力!

Keafmd

文章来源: keafmd.blog.csdn.net,作者:牛哄哄的柯南,版权归原作者所有,如需转载,请联系作者。

原文链接:keafmd.blog.csdn.net/article/details/112689175

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。