首页 > 编程语言 > 详细

Spring DI(依赖注入)

时间:2021-04-24 00:50:13      阅读:20      评论:0      收藏:0      [点我收藏+]

Spring04:DI(依赖注入)

概念

DI依赖注入(Dependency Injection,DI)就是IOC的一个实现,spring通过DI向javaBean(java类)注入属性

依赖:指javaBean的对象创建依赖于Spring容器

注入:指javaBean对象依赖的资源

构造器注入

public class User{
    
    private User(int id,String name){
        this.id = id;
        this.name = name;
    }
    
    private int id;
    private String name;
}
<?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="user" class="com.znsd.spring.vo.UserVO">
        <!--根据参数名字-->
       <constructor-arg name="id" value="1"/>
       <constructor-arg name="name" value="刘志辉"/>
     
        <!--根据下标名字-->
         <constructor-arg name="0" value="1"/>
       <constructor-arg name="1" value="刘志辉"/>
       
       <!--根据参数类型-->
         <constructor-arg name="java.lang.Integer" value="1"/>
       <constructor-arg name="java.lang.String" value="刘志辉"/>

   </bean>
    
</beans>

setter注入 【重点】

public class Student {
	
	private String name;
	private Address address;
	private String[] books;
	private List<String> hobbys;
	private Map<String,String> card;
	private Set<String> games;
	private String wife;
	private Properties info;
	public String getNameString() {
		return nameString;
	}
	public void setNameString(String nameString) {
		this.nameString = nameString;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public String[] getBooks() {
		return books;
	}
	public void setBooks(String[] books) {
		this.books = books;
	}
	public List<String> getHobbys() {
		return hobbys;
	}
	public void setHobbys(List<String> hobbys) {
		this.hobbys = hobbys;
	}
	public Map<String, String> getCard() {
		return card;
	}
	public void setCard(Map<String, String> card) {
		this.card = card;
	}
	public Set<String> getGames() {
		return games;
	}
	public void setGames(Set<String> games) {
		this.games = games;
	}
	public String getWife() {
		return wife;
	}
	public void setWife(String wife) {
		this.wife = wife;
	}
	public Properties getInfo() {
		return info;
	}
	public void setInfo(Properties info) {
		this.info = info;
	}
	@Override
	public String toString() {
		return "Student [nameString=" + nameString + ", address=" + address + ", books=" + Arrays.toString(books)
				+ ", hobbys=" + hobbys + ", card=" + card + ", games=" + games + ", wife=" + wife + ", info=" + info
				+ "]";
	}
		
}
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:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

	<bean id="address" class="com.znsd.spring.vo.Address"></bean>

	<bean id="student" class="com.znsd.spring.vo.Student">
	
		<property name="name" value="刘志辉"></property>
		<!-- 常量注入 -->
		<property name="address" ref="address"></property>
		
		<!-- 数组注入 -->
		<property name="books">
			<list>
				<value>book1</value>
				<value>book2</value>
				<value>book3</value>
				<value>book4</value>
			</list>
		</property>
		
		<!-- 集合注入 -->
		<property name="hobbys">
			<list>
				<value>吃</value>
				<value>喝</value>
				<value>玩</value>
				<value>乐</value>
			</list>
		</property>
		
		<!-- map注入 -->
		<property name="card">
			<map>
				<entry key="1" value="one"></entry>
				<entry key="2" value="two"></entry>
				<entry key="3" value="three"></entry>
				<entry key="3" value="four"></entry>
			</map>
		</property>
		
		<!-- set注入 -->
		<property name="games">
			<set>
				<value>求生之路</value>
				<value>文明</value>
				<value>欧陆风云</value>
			</set>
		</property>
		
		<!-- null注入 -->
		<property name="wife">
			<null />
		</property>
		
		<!-- Properties注入 -->
		<property name="info">
			<props>
				<prop key="one">INDIA</prop>
				<prop key="two">Pakistan</prop>
				<prop key="three">USA</prop>
			</props>

		</property>
	</bean>
</beans>
测试
class Test {

	@org.junit.jupiter.api.Test
	void test() {
		
		ApplicationContext aContext = new ClassPathXmlApplicationContext("beans.xml");
		Student student = aContext.getBean("student",Student.class);
		System.out.println(student);
	}

}

结果

技术分享图片

p命名和c命名注入

1.p标签注入

 导入约束 : xmlns:p="http://www.springframework.org/schema/p"
 
 <!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
 <bean id="user" class="com.znsd.vo.User" p:name="刘志辉" p:age="18"/>

2.c标签注入

 导入约束 : xmlns:p="http://www.springframework.org/schema/p"
 
 <!--C(构造: Constructor)命名空间 , 要设置有参构造-->
 <bean id="user" class="com.znsd.vo.User" c:name="刘志辉" c:age="18"/>

bean的作用域

在spring中声明一个bean时,你可以指定它的作用域(使用scope属性)

作用域 描述
singleton 在spring IOC容器中只存在一个Bean实例,Bean以单例的方式存在,默认
prototype 每次从容器中调用Bean时,都会返回一个新的实例,每次调用getBean()时
request 每一次HTTP请求都会创建一个新的Bean,适用于Web
session 同一个HTTP请求共享一个Bean,不同Session使用不同的Bean,适用有Web
global-session 一般用于Portlet应用环境,该作用域仅适用于Webt
singleton作用域(单例模式)
<bean id="student" class="com.znsd.spring.vo.Student" scope="singleton">
    	<property name="id" value="1"></property>
    	<property name="name" value="刘志辉"></property>
    </bean>

Singleton测试

技术分享图片

prototype(原型模式)
<bean id="student" class="com.znsd.spring.vo.Student" scope="prototype">
    	<property name="id" value="1"></property>
    	<property name="name" value="刘志辉"></property>
    </bean>

prototype测试

技术分享图片

Spring Bean 生命周期

技术分享图片

更加详细的

技术分享图片

技术分享图片

Spring DI(依赖注入)

原文:https://www.cnblogs.com/liuzhihui021221/p/14695732.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!