屬性注入(一)

1.屬性注入的概念:創建對象的時候,爲類中屬性設置屬性值

 

2.屬性注入的方式介紹(三種方式)

(1)使用set方法注入

(2)使用有參數構造注入

(3)使用接口注入

 

3.在spring框架裏,支持前兩種方式

(1)set方法注入(重點)

(2)有參構造注入

 

a.   使用有參數構造注入屬性

我們先創建一個類,並在類中給出相應的需要被注入的屬性、有參的構造方法。

package cn.itcast.property;

public class PropertyDemo1 {
	
	private String username;

	public PropertyDemo1(String username) {
		this.username = username;
	}
	
	public void test1() {
		System.out.println("demo1........."+username);
	}
	
}

在xml配置文件中使用constructor-arg注入屬性值,用name指定注入的屬性,value指定注入的屬性值。

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

		<!-- 使用有參數的構造注入屬性 -->
		<bean id="demo" class="cn.itcast.property.PropertyDemo1">
			<!-- 使用有參構造注入 -->
			<constructor-arg name="username" value="jack"></constructor-arg>
		</bean>
</beans>

最後,我們在測試類中,加載配置文件並創建對象,驗證是否成功注入屬性。

package cn.itcast.property;

import  org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public  class TestIOC{
	
    @Test
    public void testUser(){
        //1加載spring配置文件,根據創建對象
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        //2得到配置創建的對象
        PropertyDemo1 demo1 = (PropertyDemo1) context.getBean("demo");
        demo1.test1();
    }
}
測試結果:
demo1.........jack

b.   使用set方式注入屬性

我們先創建一個類,並在類中給出相應的需要被注入的屬性以及set方法。

package cn.itcast.property;

public class Book {

	private String bookname;
	//set方法

	public void setBookname(String bookname) {
		this.bookname = bookname;
	}
	
	public void demobook() {
		System.out.println("book........."+bookname);
	}
}

在xml配置文件中使用property注入屬性值,用name指定注入的屬性,value指定注入的屬性值。

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

		<!-- 使用set方法注入屬性 -->
		<bean id="book" class="cn.itcast.property.Book">
			<!-- 注入屬性值 
				name屬性值,類裏面定義的屬性值名稱
				value屬性,設置具體的值
			-->
			<property name="bookname" value="書名"></property>
		</bean>
</beans>

同樣的,我們最後使用測試類來測試一下屬性注入後的結果。

package cn.itcast.property;

import  org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public  class TestIOC{
	
    @Test
    public void testUser(){
        //1加載spring配置文件,根據創建對象
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        //2得到配置創建的對象
        Book book = (Book) context.getBean("book");
        book.demobook();
    }
}
測試結果:
book.........書名

值得一提的是,我們在開發過程中更多的使用set方法注入,需要重點掌握。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章