属性注入的三种方式
首先了解下面两个名词的含义:
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:
<?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="<<北京>>"></property> -->
<property name="address">
<value><![CDATA[<<北京>>]]></value>
</property>
</bean>
</beans>
测试代码:
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
<?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>
测试代码:
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:
<?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>
测试代码:
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