标签:基于 set return String void private 依赖 address public
基于set方式的依赖注入
概述
依赖注入的方式分为基于构造器的注入方式和基于set的注入方式,而基于set方式的依赖注入是是依赖注入的核心,本文详细讲解多种类型的数据注入的方式,包括普通注入,bean注入,array|list|map|set|props|null
更详尽内容请查看官网
实践
地址类,用于测试bean注入
package com.kuangstudy.di;
/**
* 功能描述
*
* @since 2022-06-25
*/
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
学生类,用于测试全部类型
package com.kuangstudy.di;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* 功能描述
*
* @since 2022-06-25
*/
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String, String> card;
private Set<String> games;
private String wife;
private Properties info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
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;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
测试类,用于执行main函数测试
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.kuangstudy.di.Student;
/**
* 功能描述
*
* @since 2022-06-25
*/
public class Test01 {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Student s1 = (Student) applicationContext.getBean("s1");
System.out.println(s1);
}
}
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address1" class="com.kuangstudy.di.Address">
<property name="address" value="xi'an"></property>
</bean>
<bean id="s1" class="com.kuangstudy.di.Student">
<property name="name" value="xiaoming"></property>
<property name="address" ref="address1"></property>
<property name="books">
<array>
<value>西游记</value>
<value>红楼梦</value>
<value>三国演义</value>
<value>水浒传</value>
</array>
</property>
<property name="hobbies">
<list>
<value>swimming</value>
<value>painting</value>
<value>playing basketball</value>
</list>
</property>
<property name="card">
<map>
<entry key="Student card" value="000010"></entry>
<entry key="Id card" value="xxxxxxxxxxxx"></entry>
<entry key="Bank card" value="xxxxxxxxxxxxxxxx"></entry>
</map>
</property>
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<property name="wife">
<null></null>
</property>
<property name="info">
<props>
<prop key="username">root</prop>
<prop key="password">123</prop>
</props>
</property>
</bean>
</beans>
标签:基于,set,return,String,void,private,依赖,address,public 来源: https://www.cnblogs.com/Oh-mydream/p/16410773.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。