属性注入(一)

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方法注入,需要重点掌握。

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