问题?Spring的DI讲解、DI有几种注入方式、

一、spring的DI:依赖注入给属性赋值
DI定义:一个对象类的属性可以使用springDI(依赖注入)来进行赋值,但是并不是所有的类属性都适合springDI来赋值.,
一共有两种方式进行属性赋值:set方法和带参构造方法

案例1:setting方法为对象类属性赋值
Person类

package cn.itcast.sh.spring.di;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Person {
	private Long pid;
	private String pname;
	private Student student;
	private List lists;
	private Map map;
	private Properties properties;
	private Set sets;

	public Long getPid() {
		return pid;
	}
	public void setPid(Long pid) {
		this.pid = pid;
	}
	public String getPname() {
		return pname;
	}
	public void setPname(String pname) {
		this.pname = pname;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public List getLists() {
		return lists;
	}
	public void setLists(List lists) {
		this.lists = lists;
	}
	public Map getMap() {
		return map;
	}
	public void setMap(Map map) {
		this.map = map;
	}
	public Properties getProperties() {
		return properties;
	}
	public void setProperties(Properties properties) {
		this.properties = properties;
	}
	public Set getSets() {
		return sets;
	}
	public void setSets(Set sets) {
		this.sets = sets;
	}
}

Student类、

package cn.itcast.sh.spring.di;
public class Student {
}

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

			<!-- 先把person和student放入到spring容器中 -->
           <!-- SpringDI:依赖注入,本质就是向对象类的属性赋值 -->

           <!-- 用setting方式来为属性赋值 -->
           <bean id="person" class="cn.itcast.sh.spring.di.Person">
           	 <!--
	 			 property是用来描述一个类的属性
	 			   基本类型的封装类、String等需要值的类型用value赋值
				引用类型用ref赋值(一般是对象的注入)
			  -->
           		<property name="pid" value="2"></property>
           		<property name="pname" value="lijun"></property>
           		<property name="lists">
           			<list>
           				<value>list1</value>
           				<value>list2</value>
           				<ref bean="student"/>
           			</list>
           		</property>
           		<property name="map">
           			<map>
           				<entry key="m1">
           					<value>map1</value>
           				</entry>
           				<entry key="map2">
           					<ref bean="student"/>
           				</entry>
           			</map>
           		</property>
           		<!-- properties:只有String类型 -->
           		<property name="properties">
           			<props>
           				<prop key="p1">p1的值</prop>
           				<prop key="p2">p2的值</prop>
           			</props>
           		</property>
           		<property name="sets">
           			<set>
           				<value>set第一个值</value>
           				<value>set第二值</value>
           				<ref bean="student"/>
           			</set>
           		</property>
           		<property name="student">
           			<ref bean="student"/>
           		</property>
           </bean>

           <bean id="student" class="cn.itcast.sh.spring.di.Student"></bean>
</beans>

测试类:

package cn.itcast.sh.spring.ioc.test;

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

import cn.itcast.sh.spring.di.Person;
import cn.itcast.sh.spring.di.Student;

public class IDTest {
	@Test
	public void A(){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person = (Person)applicationContext.getBean("person");
		System.out.println(person.getPid());
		System.err.println(person.getStudent());
		System.out.println("--------------");
		System.out.println(person.getLists());
		System.out.println("--------------");
		System.out.println(person.getMap());
		System.out.println("--------------");
		System.out.println(person.getProperties());
		System.out.println("--------------");
		System.out.println(person.getSets());

	}
}

结果图片处:

案例2:带参构造器为对象类属性赋值

Person类

package cn.itcast.sh.spring.di.constructor;

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

public class Person {
	private String pname;
	private Student student;

	private void person() {}//如果写的有带参构造方法,一定要写默认的构造方法

	public Person(String pname,Student student){
		this.pname = pname;
		this.student = student;
	}

	public String getPname() {
		return pname;
	}

	public Student getStudent() {
		return student;
	}

}

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

			<!-- 先把person和student放入到spring容器中 -->
           <!-- SpringDI:依赖注入,本质就是向对象类的属性赋值 -->

           <!-- 用构造函数的方式为属性赋值(
           		如果没有constructor-arg:默认情况下是调用的无参构造函数,
           		如果有constructor-arg:则调用带参数的构造函数) 

           		注:一个bean只能确认一个构造函数,因此在对象类中不能出现两个构造函数
           	-->

           <bean id="personConstructor" class="cn.itcast.sh.spring.di.constructor.Person">
           <!--
           	index:表示构造函数参数的位置
           	type:表示参数的类型
           	value:表示给基本类型赋值
           	ref:表示给引用类型赋值
            -->
           	<constructor-arg index="0" type="java.lang.String" value="aaa"></constructor-arg>
           	<constructor-arg index="1" type="cn.itcast.sh.spring.di.constructor.Student" ref="studentConstructor"></constructor-arg>
           </bean>

           <bean id="studentConstructor" class="cn.itcast.sh.spring.di.constructor.Student"></bean>
</beans>

测试类:

package cn.itcast.sh.spring.ioc.test;

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

import cn.itcast.sh.spring.di.constructor.Person;

public class DIConstructorTest {
	@Test
	public void A(){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person = (Person)applicationContext.getBean("personConstructor");
		System.out.println(person.getPname());
		person.getStudent().B();
	}
}

结果图片处:


案例写到这里;我们就可能要提一个问题了,那我们用这个方式来干甚呢?

用spring这种赋值方式,我们可以实现属性的赋值,还可以实现对象的注入,在我们进行对象的实现的时候,就不需要我们来做了,全部交给spring容器来做。

二、springIOC和DI的意义

意义:不完全面接口编程----->变成完全的接口编程(也就是将对象的产生给了spring产生,我们不需要关心是谁来实现的接口)

案例:spring+MVC模式

action层

package cn.itcast.sh.spring.mvc;
public class PersonAction {
	private PersonService personService;

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public void savePerson1(){
		this.personService.savePerson();
	}
}

service接口层及实现

package cn.itcast.sh.spring.mvc;

public interface PersonService {
	public void savePerson();
}
package cn.itcast.sh.spring.mvc;

public class PersonServiceImp implements PersonService {
	private PersonDao personDao;

	public PersonDao getPersonDao() {
		return personDao;
	}
	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	public void savePerson() {
		this.personDao.savePerson();
	}

}

Dao接口层及实现

package cn.itcast.sh.spring.mvc;

public interface PersonDao {
	public void savePerson();
}
package cn.itcast.sh.spring.mvc;

public class PersonDaoImp implements PersonDao {

	public void savePerson() {
		System.out.println("sava Person Dao!!");
	}

}

spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
           <!-- Spring模拟MVC -->
           <bean id="personDao" class="cn.itcast.sh.spring.mvc.PersonDaoImp"></bean>
           <!-- 这里采用的是setting方法进行注入的 -->
           <bean id="personService" class="cn.itcast.sh.spring.mvc.PersonServiceImp">
           		<property name="personDao">
           			<ref bean="personDao"/>
           		</property>
           </bean>

           <bean id="personAction" class="cn.itcast.sh.spring.mvc.PersonAction">
           	  <property name="personService">
           	  		<ref bean="personService"/>
           	  </property>
           </bean>
</beans>

测试类:

package cn.itcast.sh.spring.ioc.test;

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

import cn.itcast.sh.spring.mvc.PersonAction;
import cn.itcast.sh.spring.util.SpringUtil;

public class MVCTest  {
	@Test
	public void A(){
		ApplicationContext applicationContext = new  ClassPathXmlApplicationContext("applicationContext.xml");
		PersonAction personAction = (PersonAction)applicationContext.getBean("personAction");
		personAction.savePerson1();
	}
}

结果图片处:

spring容器IOC创建对象<三>的更多相关文章

  1. spring容器IOC创建对象<二>

    问题?spring是如何创建对象的?什么时候创建对象?有几种创建方式?测试对象是单例的还是多例的 ?对象的初始化和销毁? 下面的四大模块IOC的内容了!需要深刻理解 SpringIOC定义:把对象的创 ...

  2. spring容器IOC原理解析

    原理简单介绍: Spring容器的原理,其实就是通过解析xml文件,或取到用户配置的bean,然后通过反射将这些bean挨个放到集合中,然后对外提供一个getBean()方法,以便我们获得这些bean ...

  3. 菜鸟学SSH(十三)——Spring容器IOC解析及简单实现

    最近一段时间,“容器”两个字一直萦绕在我的耳边,甚至是吃饭.睡觉的时候都在我脑子里蹦来蹦去的.随着这些天一次次的交流.讨论,对于容器的理解也逐渐加深.理论上的东西终归要落实到实践,今天就借助Sprin ...

  4. Spring容器IOC解析及简单实现(转)

    文章转自http://blog.csdn.net/liushuijinger/article/details/35978965

  5. Spring详解(二)------IOC控制反转

    我相信提到 Spring,很多人会脱口而出IOC(控制反转).DI(依赖注入).AOP等等概念,这些概念也是面试官经常问到的知识点.那么这篇博客我们就来详细的讲解 IOC控制反转. ps:本篇博客源码 ...

  6. Spring_01 spring容器、控制反转(IOC)、依赖注入(DI)

    目录 1 什么是spring框架 2 spring框架的特点 3 spring容器 3.1 什么是spring容器 3.2 spring容器创建对象的编程步骤 3.4 spring容器创建对象的方式 ...

  7. Spring实战(二)Spring容器和bean的生命周期

    引入问题: 在XML配置文件中配置bean后,这些文件又是如何被加载的?它们被加载到哪里去了? Spring容器——框架核心 1.什么是Spring容器?它的功能是什么? 在基于Spring的应用中, ...

  8. 菜鸟学SSH(十四)——Spring容器AOP的实现原理——动态代理

    之前写了一篇关于IOC的博客——<Spring容器IOC解析及简单实现>,今天再来聊聊AOP.大家都知道Spring的两大特性是IOC和AOP,换句话说,容器的两大特性就是IOC和AOP. ...

  9. spring容器和上下文的理解

    spring容器和上下文的理解 spring框架现在使用的很多,这说明有其独特之处----依赖注入,很简单的四个字,在我们需要对象的时候,spring就可以给我们提供所需要的对象,对象的创建.销毁.管 ...

随机推荐

  1. Cygwin: connection closed by ::1

    问题描述:在Win7下Cygwin中,使用ssh localhost命令, 出现Connectionclosedby::1的问题. 解决方案:1.我的电脑-管理-找到CYGWINsshd. 2.右键 ...

  2. 【Linux程序设计】之Linux库函数的使用,多文件程序开发,静态与共享函数

    这个系列的博客贴的都是我大二的时候学习Linux系统高级编程时的一些实验程序,都挺简单的.贴出来纯粹是聊胜于无. 实验题目:Linux基础程序设计综合实验 实验目的:熟悉并掌握Linux库函数的使用, ...

  3. 打包apk

    apk 配置环境变量 打开"终端",输入"pico .bash_profile"命令 export ANDROID_SDK_ROOT=/Users/sun/Do ...

  4. MySQL安装问题:Unable to update security settings解决方案

    主要问题还是之前装过,卸载的时候卸载不干净导致的. 如下: 安装到最后出现: Unable to update security settings. Access denied for user 'r ...

  5. 一人一python挑战题解

    题目id: 1 just print a+b give you two var a and b, print the value of a+b, just do it!! print a+b 题目id ...

  6. TYVJ P1022 进制转换 Label:坑

    背景 太原成成中学第3次模拟赛 第2道 描述 对于十进制整数N,试求其-2进制表示.例如,因为 1*1 + 1*-2 + 1*4 + 0*-8 +1*16 + 1*-32 = -13 ,所以(-13) ...

  7. POJ 2342 Label:树形dp

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6955   Accepted: 4003 ...

  8. diff和patch配合使用(转载备用)

    Linux下diff与patch命令的配合使用 在Linux下,diff与patch命令配合使用可以进行简单的代码维护工作. [A] diffdiff命令用于比较文件的差异,可以用于制作patch文件 ...

  9. 中文编码、字符集,GBK, UTF-8的概念

    字符集指的是什么? 字符集是一个人为的规定,人们用一个小册子规定好"文字字符"与"数字"的对应关系. 其中,每一个字符对应的数组也称其为编码. 例如,ASCII ...

  10. Codeforeces 617E XOR and Favorite Number(莫队+小技巧)

    E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...