spring 依赖注入DI-个人学习笔记

依赖注入(dependency  injection):

依赖:指bean对象创建依赖于容器,bean对象依赖于资源。注入:bean对象依赖的资源由容器来设置和装配。

1.spring注入-构造注入:请看

spring-ioc 学习笔记2

2.spring注入-setter注入:

Test.java

<span style="font-size:12px;">public class Test {

	public static void main(String[] args) {
	ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
	Student student=(Student)ac.getBean("student");
	student.show();
	}
}</span>
1).常量注入

Student.java

public class Student {

	private String name;

	public void setName(String name) {
		this.name = name;
	}
	
	public void show(){
		System.out.println("name="+name);
	}
}
beans.xml

 <bean name="student" class="cn.xunifeng.vo.Student">
    	<property name="name" value="李四"/>
    </bean>

2).bean注入

beans.xml

<span style="font-size:12px;">  <bean name="addr" class="cn.xunifeng.vo.Address">
     	<property name="address" value="贵州贵阳市"/>
     </bean>
    <bean name="student" class="cn.xunifeng.vo.Student">
    	<property name="name" value="李四"/>
    	<property name="addr" ref="addr"/>
    </bean></span>
Student.java

public class Student {

	private String name;
	private Address addr;
	
	public void setAddr(Address addr) {
		this.addr = addr;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public void show(){
		System.out.println("name="+name +" ;address="+addr.getAddress());
	}
}

Address.java

public class Address {
	private String address;

	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}	
}
3). 数组注入

beans.xml

 <bean name="addr" class="cn.xunifeng.vo.Address">
     	<property name="address" value="贵州贵阳市"/>
     </bean>
    <bean name="student" class="cn.xunifeng.vo.Student">
    	<property name="name" value="李四"/>
    	<property name="addr" ref="addr"/>
    	<property name="books">
    		<array>
    			<value>百年孤独</value>
    			<value>梦的解析</value>
    			<value>武动乾坤</value>
    		</array>
    	</property>
    </bean>
student. java

public class Student {
         ..................................
	private String[] books;
	
	public void setBooks(String[] books) {
		this.books = books;
	}
	............
           ......
       public void show(){
		...............................
		System.out.print("books=");
		for(int i=0;i<books.length;i++){
			System.out.print(books[i].toString()+"  ");
		}
		System.out.println();
	}
}
4).List注入

beans.xml

	<property name="hobbies">
    			<list>
    				<value>篮球</value>
    				<value>足球</value>
    				<value>排球</value>
    			</list>
    	</property>
5). Map注入
beans.xml

<span style="font-size:12px;"><property name="cards">
    		<map>
    			<!-- 第一种方法 -->
    			<entry key="建设银行" value="5552345565465431"/>
    			<!-- 第二种方法 -->
    			<entry>
    				<key><value>农业银行</value></key>
    				<value>522224151215121</value>
    			</entry>
    		</map>
    	</property></span>
6)Set注入

beans.xml

<property name="games">
    		<set>
    			<value>LOL</value>
    			<value>CS</value>
    			<value>DNF</value>
    		</set>
    	</property>
7).Null注入

beans.xml

	<property name="email"><null/></property>
8).Properties注入
beans.xml

<property name="info">
    		<props>
    			<prop key="学号">2012040569</prop>
    			<prop key="性别">男</prop>
    			<prop key="姓名">张三</prop>
    		</props>
    	</property>

9).P命名空间注入(属性依然要设置set方法)

<span style="font-size:12px;"> <?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">
</span>
<pre class="programlisting"><span style="font-size:12px;"><span class="hl-tag"><bean name="user" class="cn.xunifeng.vo.User" p:name="李雪红" p:age="25"/></span></span><span style="font-size:12px;">

</span>

 
user.java 

<span style="font-size:12px;">package cn.xunifeng.vo;

public class User {

	private String name;
	
	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

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

	@Override
	public String toString() {
		return "User [age=" + age + ", name=" + name + "]";
	}
	
	
}
</span>


10).C命名空间注入(要求有对应参数的构造方法  spring4.0版本才出现)

<span style="font-size:12px;"><beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

   <!-- C命名空间注入  需要有对应参数的构造方法 (前提需要引入头文件)-->
    <bean name="user1" class="cn.xunifeng.vo.User" c:name="傅红雪" c:age="30"/>

</beans></span>
user. java

package cn.xunifeng.vo;

public class User {

	private String name;
	
	private int age;

	public User() {
		
	}
	
	public User(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

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

	@Override
	public String toString() {
		return "User [age=" + age + ", name=" + name + "]";
	}
	
	
}










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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值