Three data types of Bean dependency injection

There are three types of Bean dependency injection, which are ordinary data types, collections, and reference data types.
The set dependency injection method I mainly use here

1. Common data types

public class Book{
    
    
	private String bid;
	private String bname;
	private String btype;
	public void setBid(String bid){
    
    
		this.bid = bid;
	}

	public void setBname(String bname){
    
    
		this.bname = bname;
	}

	public void setBtype(String btype){
    
    
		this.btype = btype;
	}
}

I need to perform dependency injection on bid, bname, and btype in the Book class

Configuration file

<bean id="book" class="book">
	<property name="bid" value="123" />
	<property name="bname" value="java spring学习笔记" />
	<property name="btype" value="计算机" />
</bean>

2. Collection

In order to be simple and easy to understand, I directly add a collection to the Book class

public class Book{
    
    
	// 为了简单易懂,我将集合写到了这里来
	private List<Book> bookList;
	private Map<String,Book> bookMap;
	private Properties properties;		// 配置文件也可依赖注入
	// 同样需要创建set方法
	public void setBookList(List<Book> bookList){
    
    
		this.bookList = bookList;
	}
	public void setBookMap(Map<String,Book> bookMap){
    
    
		this.bookMap = bookMap;
	}
	public void setProperties(Properties properties){
    
    
		this.properties = properties;
	}
	

	private String bid;
	private String bname;
	private String btype;
	public void setBid(String bid){
    
    
		this.bid = bid;
	}

	public void setBname(String bname){
    
    
		this.bname = bname;
	}

	public void setBtype(String btype){
    
    
		this.btype = btype;
	}
}

Configuration file

<!-- 由于是Book类的集合,所以需要提前准备好几个Book对象 -->
<bean id="book1" class="book">
	<property name="bid" value="123" />
	<property name="bname" value="java spring学习笔记" />
	<property name="btype" value="计算机" />
</bean>

<bean id="book2" class="book">
	<property name="bid" value="124" />
	<property name="bname" value="C++学习笔记" />
	<property name="btype" value="计算机" />
</bean>

<bean id="book3" class="book">
	<property name="bid" value="125" />
	<property name="bname" value="Python学习笔记" />
	<property name="btype" value="计算机" />
</bean>

<bean id="book" class="book">
	<!-- List -->
	<property name="bookList">
		<list>
			<!-- 若是普通数据类型的话,直接在value标签中输入值即可,如下 -->
			<!-- <value>b1</value> -->
			<ref bean="book1"/>
			<ref bean="book2"/>
			<ref bean="book3"/>
		</list>
	</property>

	<!-- Map -->
	<property name="bookMap">
		<map>
			<!-- key为map的关键字,value-ref为具体对象 -->
			<entry key="b1" value-ref="book1"></entry>
			<entry key="b2" value-ref="book2"></entry>
			<entry key="b3" value-ref="book3"></entry>
		</map>
	</property>
	<property name="properties">
		<props>
			<prop key="p1">1</prop>
			<prop key="p2">2</prop>
			<prop key="p3">3</prop>
		</props>
	</property>
</bean>

3. Quote

Object reference injection requires two classes. For example, I use a BookDao and a BookService class
BookDao class

public class BookDao{
    
    
	
}

BookService class

public class BookService{
    
    
	private BookDao bookDao;
	public void setBookDao(BookDao bookDao){
    
    
		this.bookDao = bookDao;
	}
}

Configuration file

<!-- 对象注入,首先要创建一个对象,名为bookDao -->
<bean id="bookDao" class="bookDao"></bean>
<bean id="bookService" class="bookService">
	<!-- 此处的name是为BookService中的属性名的bookDao -->
	<!-- ref是用于对象引用的,所以ref的bookDao是上面创建的对象 -->
	<property name="bookDao" ref="bookDao"></property>
</bean>

The above is my personal learning experience, if there are any mistakes, please correct me!

Guess you like

Origin blog.csdn.net/interestANd/article/details/112555912
Recommended