【Spring】DI的各种类型变量注入方法

首先创建Student类

student.java
public class Student {
	private String name;
	private Address address;
	private String[] books;
	private List<String> hobbies;
	private Set<String> games;
	private Map<String,Long> cards;
	private String wife;
	private Properties info;
	
	public void show() {
		System.out.println
			("Student's name = " + name + ",address = " + address.getAddress());
		
		System.out.print("books = ");
		
		for(String string:books) {
			System.out.print(string + "  ");
		}
		System.out.println();
		
		System.out.println("hobbies = " + hobbies);
		
		System.out.println("games = " + games);
		
		System.out.println("bank cards = " + cards);
		
		System.out.println("wife = " + wife);
		
		System.out.println("info = " + info);
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setAddress(Address address) {
		this.address = address;
	}
	
	public void setBooks(String[] books) {
		this.books = books;
	}
	
	public void setHobbies(List<String> hobbies) {
		this.hobbies = hobbies;
	}
	
	public void setGames(Set<String> games) {
		this.games = games;
	}
	
	public void setCards(Map<String, Long> cards) {
		this.cards = cards;
	}
	
	public void setWife(String wife) {
		this.wife = wife;
	}
	
	public void setInfo(Properties info) {
		this.info = info;
	}
}

Test.java
public class Test {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("Beans.xml");
		Student student = (Student) ac.getBean("student");
		student.show();
	}
}

1.常量注入

Beans.xml
    <bean id="student" class="com.duxd.vo.Student">
    	<!-- 常量注入 -->
		<property name="name" value="名字"/>
    </bean>

2.类注入

Address.java
public class Address {
	private String address;
	
	public String getAddress() {
		return address;
	}
	
	public void setAddress(String address) {
		this.address = address;
	}
}

Beans.xml
    <bean id="address" class="com.duxd.vo.Address">
    	<property name="address" value="上海"/>
    </bean>
    
    <bean id="student" class="com.duxd.vo.Student">
    	<!-- 类注入 -->
		<property name="address" ref="address"/>
    </bean>

3.数组注入

Beans.xml
    <bean id="student" class="com.duxd.vo.Student">
    	<!-- 数组注入 -->
		<property name="books">
			<array>
				<value>傲慢与偏见</value>
				<value>基督山伯爵</value>
				<value>钢铁是怎样炼成的</value>
			</array>
		</property>
    </bean>

4.List注入

Beans.xml
    <bean id="student" class="com.duxd.vo.Student">
    	<!-- list注入 -->
		<property name="hobbies">
			<list>
				<value>看书</value>
				<value>学习</value>
				<value>上网</value>
				<value>写作</value>
			</list>
		</property>
    </bean>

5.Set注入

Beans.xml
    <bean id="student" class="com.duxd.vo.Student">
    	<!-- Set注入 -->
		<property name="games">
			<set>
				<value>使命召唤</value>
				<value>少女卷轴</value>
				<value>我的世界</value>
			</set>
		</property>
    </bean>

6.Map注入

Beans.xml
    <bean id="student" class="com.duxd.vo.Student">
    	<!-- Map注入 -->
		<property name="cards">
			<map>
				<entry key="招商银行" value="6462131654486413213"/>
				<entry>
					<key><value>工商银行</value></key>
					<value>132164612231684684</value>
				</entry>
			</map>
		</property>
    </bean>

7.Null注入

Beans.xml
    <bean id="student" class="com.duxd.vo.Student">
    	<!-- Null注入 -->
		<property name="wife">
			<null/>
		</property>
    </bean>

8.Properties注入

Beans.xml
    <bean id="student" class="com.duxd.vo.Student">
    	<!-- Properties注入 -->
		<property name="info">
			<props>
				<prop key="学号">2017000001</prop>
				<prop key="sex">男</prop>
				<prop key="name">名字</prop>
			</props>
		</property>
    </bean>

9.结果

Student's name = 名字,address = 上海
books = 傲慢与偏见  基督山伯爵  钢铁是怎样炼成的  
hobbies = [看书, 学习, 上网, 写作]
games = [使命召唤, 少女卷轴, 我的世界]
bank cards = {招商银行=6462131654486413213, 工商银行=132164612231684684}
wife = null
info = {学号=2017000001, name=名字, sex=男}

10.P命名空间注入

xmlns:p="http://www.springframework.org/schema/p"

User.java  需要Set方法
public class User {
	private String name;
	private int age;
	
	public void show() {
		System.out.println("name = " + name + ", age = " + age);
	}
	
	public void setName(String name) {
		this.name = name;
	}

	public void setAge(int age) {
		this.age = age;
	}
}

Test.java
public class Test {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("Beans.xml");
		User user = (User) ac.getBean("user");
		user.show();
	}
}

Beans.xml
	<!-- P命名空间注入需要set方法 -->
	<bean id="user" class="com.duxd.vo.User" p:name="名字" p:age="20"/>

结果
name = 名字, age = 20

11.C命名空间注入

xmlns:c="http://www.springframework.org/schema/c"

User.java  需要带参构造方法
public class User {
	private String name;
	private int age;
	
	public User(){
		super();
	}
	
	public User(String name, int age) {
		this();
		this.name = name;
		this.age = age;
	}
	
	public void show() {
		System.out.println("name = " + name + ", age = " + age);
	}
	
}

Test.java
public class Test {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("Beans.xml");
		User user = (User) ac.getBean("user");
		user.show();
	}
}

Beans.xml
	<!-- C命名空间注入需要带参构造方法 -->
	<bean id="user" class="com.duxd.vo.User" c:name="名字" c:age="20"/>

结果
name = 名字, age = 20

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值