属性注入(二)

注入对象类型属性

1.创建service类和dao类

(1)在service得到dao对象

 

2.具体实现过程

(1)创建dao类

package cn.itcast.ioc;

public class UserDao {

	public void add() {
		System.out.println("dao.........");
	}
}

(2)在service里面把dao作为类型属性

(3)生成dao类型属性的set方法

package cn.itcast.ioc;

public class UserService {

	//1定义dao类型属性
	private UserDao userDao;
	//2生成set方法
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public void add() {
		System.out.println("service.........");
		//在service里面得到dao类对象,才能调用dao里面的方法
//		UserDao dao = new UserDao();
//		dao.add();
		userDao.add();
	}

	
}

 

(4)配置文件中注入关系

  a.  配置service和dao对象

  b.  注入dao对象

<?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">

		<!-- 注入对象类型属性 -->
		<!-- 1配置service和dao对象 -->
		<bean id="userDao" class="cn.itcast.ioc.UserDao"></bean>
	
		<bean id="userService" class="cn.itcast.ioc.UserService">
			<!-- 注入dao对象 
				name属性值,service类里面属性的名称
				不能写value属性,value用于传递字符串
				ref属性传递对象:dao配置bean标签中的id值(也就是要传递的对象对应在bean中的id值)
			-->
			<property name="userDao" ref="userDao"></property>
		</bean>
</beans>

 (5)测试

package cn.itcast.ioc;

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得到配置创建的对象
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}
测试结果:
service.........
dao.........

以上测试结果说明测试类在获得UserService类后,将userDao类的对象注入到UserService类中的userDao属性中,并在UserService类中的add方法中成功调用userDao类的方法add。

P名称空间

p名称空间注入方法,即通过命名空间来为类中的属性注入相应的值。

(1)首先在beans中引入下列代码

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

得到最终的beans如下所示: 

<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"
       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 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">

	<!-- p名称空间注入 -->
	<bean id="person" class="cn.itcast.proper ty.Person" p:pname="Lucy"></bean>
</beans>

在上面一段代码中,我们可以看到 p:name 的属性,就是通过使用p名称空间指定该类空间下的pname属性,通过p:name 就可以对其进行属性注入。

(2)类似注入对象类型属性中所述方法,创建person类,并在person类中 写test1方法用于测试调用结果。并通过测试类来测试调用结果,测试代码如下:

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得到配置创建的对象
        Person person = (Person) context.getBean("person");
        person.test1();
    }
}

最终得到测试的结果: 

测试结果:
person.....Lucy

注入复杂类型属性

在实际开发过程中,我们需要注入的属性不仅仅是string类型的属性,有时候还需要对一些复杂属性进行属性注入。在这里我们介绍四种复杂类型的注入方法,包括数组,list,map以及properties类型的属性注入方法。

(1)首先我们先按照老规矩,创建一个类,在类中我们给出数组,list,map以及properties类型的属性定义、set方法以及测试调用的方法。

package cn.itcast.property;

import java.util.Map;
import java.util.List;
import java.util.Properties;

public class Person {

	private String pname;
	private String[] arrs;
	private List<String> list;
	private Map<String, String> map;
	private Properties properties;
	
	public void setPname(String pname) {
		this.pname = pname;
	}
	public void setArrs(String[] arrs) {
		this.arrs = arrs;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	public  void  test1() {
		System.out.println("arrs:"+arrs);
		System.out.println("list:"+list);
		System.out.println("map:"+map);
		System.out.println("properties:"+properties);
	}
}

(2)完成类的定义后,我们需要对bean.xml配置文件进行修改。每种属性的注入方式如下。

  1. 数组

<!-- 数组 -->
<property name="arrs">
    <list>
        <value>小王</value>
        <value>小马</value>
        <value>小宋</value>
    </list>
</property>

  2. list 集合

<!-- list -->
<property name="list">
    <list>
        <value>小奥</value>
        <value>小金</value>
        <value>小普</value>
    </list>
</property>

  3.map 集合

<!-- map -->
<property name="map">
    <map>
        <entry key="aa" value="lucy"></entry>
        <entry key="bb" value="mary"></entry>
        <entry key="cc" value="tomy"></entry>
    </map>
</property>

  4.properties 类型

<!-- properties -->
<property name="properties">
    <props>
        <prop key="driverclass">com.mysql.jdbc.Diver</prop>
        <prop key="username">root</prop>
    </props>
</property>

最终整合后的bean.xml 文件如下所示:

<?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"
       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 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="person" class="cn.itcast.property.Person">
		<!-- 数组 -->
		<property name="arrs">
			<list>
				<value>小王</value>
				<value>小马</value>
				<value>小宋</value>
			</list>
		</property>
		
		<!-- list -->
		<property name="list">
			<list>
				<value>小奥</value>
				<value>小金</value>
				<value>小普</value>
			</list>
		</property>
		
		<!-- map -->
		<property name="map">
			<map>
				<entry key="aa" value="lucy"></entry>
				<entry key="bb" value="mary"></entry>
				<entry key="cc" value="tomy"></entry>
			</map>
		</property>
		
		<!-- properties -->
		<property name="properties">
			<props>
				<prop key="driverclass">com.mysql.jdbc.Diver</prop>
				<prop key="username">root</prop>
			</props>
		</property>
		</bean>
		
</beans>

(3)同样的,我们使用p命名空间中所用的测试代码进行测试,得到最终的测试结果:

测试结果:
arrs:[Ljava.lang.String;@638ef7ed
list:[小奥, 小金, 小普]
map:{aa=lucy, bb=mary, cc=tomy}
properties:{driverclass=com.mysql.jdbc.Diver, username=root}

 

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