码迷,mamicode.com
首页 > 编程语言 > 详细

Spring特性--DI

时间:2016-07-30 15:00:13      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

DI:Dependency Injection(依赖注入),通俗的讲就是一种通过xml配置文件,为交给sping容器的对象初始化参数。又称做控制反转:Inversion of Control(IoC)

 依赖注入主要分为两种形式:

    |-:基于构造方法的依赖注入

    |-:基于setter方法的依赖注入

 

 基于构造方法的依赖注入又可以分为以下几种:

·复杂数据类型:

·简单数据类型:

    |- 基于属性类型(type)

    |-基于索引(index)

    |-基于参数名称(name)

 

复杂数据类型实例

 

 

package com.fuwh.spring;

 

/*
 * POJO类
 */
public class Clazz {
private String name;
private int grade;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
package com.fuwh.spring;
/*
 * POJO类
 */
public class Lesson {
private String name;
private int score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
package com.fuwh.spring;
public class Student {
private Clazz clazz;
private Lesson lesson;
public Student(Clazz clazz, Lesson lesson) {
this.clazz = clazz;
this.lesson = lesson;
}
@Override
public String toString() {
return "Student [clazz=" + clazz.getGrade()+clazz.getName() + ", lesson=" + lesson.getName()+","+lesson.getScore() + "学分]";
}
}

 

<?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.xsd">
    <!-- 配置文件 -->
<bean id="student" class="com.fuwh.spring.Student" lazy-init="default">
<constructor-arg ref="clazz"/>
<constructor-arg ref="lesson"/>
</bean>
<bean id="clazz" class="com.fuwh.spring.Clazz">
<property name="name" value="信本"/>
<property name="grade" value="08"/>
</bean>
<bean id="lesson" class="com.fuwh.spring.Lesson">
<property name="name" value="java"/>
<property name="score" value="4"/>
</bean>
</beans>

 

package com.fuwh.spring;

 

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Spring01 {
public static void main(String[] args) {
/*
* 测试类
*/
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
System.out.println(ac.getBean("student",Student.class));
}
}

基于属性类型(type) 实例: 

 

package com.fuwh.spring;
/*
 * POJO类
 */
public class Clazz {
private String name;
private int grade;
public Clazz(String name, int grade) {
super();
this.name = name;
this.grade = grade;
}
@Override
public String toString() {
return "Student [name=" + name + ", grade=" + grade + "]";
}
}
<?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.xsd">
    <!-- spring配置文件 -->
<bean id="clazz" class="com.fuwh.spring.Clazz" lazy-init="default">
<constructor-arg type="String" value="信息与计算科学"/>
<constructor-arg type="int" value="08"/>
</bean>
</beans>

 

package com.fuwh.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Spring01 {
public static void main(String[] args) {
/*
* 测试类
*/
// ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
// ApplicationContext ac=new ClassPathXmlApplicationContext(new String[]{"beanStudent.xml","beanClazz.xml"});
// ApplicationContext ac=new ClassPathXmlApplicationContext("beanStudent.xml");
// System.out.println(ac.getBean("student"));
ApplicationContext ac=new ClassPathXmlApplicationContext("beanClazz.xml");
System.out.println(ac.getBean("clazz",Clazz.class));
}
}

 基于索引(index)实例

※需要注意的是,索引是从“0”开始
<?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.xsd">
    <!-- spring配置文件 -->
<bean id="clazz" class="com.fuwh.spring.Clazz" lazy-init="default">
<constructor-arg index="0" value="信息与计算科学"/>
<constructor-arg index="1" value="08"/>
</bean>
</beans>

 

基于参数名称(name)实例:

<?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.xsd">
    <!-- spring配置文件 -->
<bean id="clazz" class="com.fuwh.spring.Clazz" lazy-init="default">
<constructor-arg name="name" value="信息与计算科学"/>
<constructor-arg name="grade" index="1" value="08"/>
</bean>
</beans>

 

 基于setter方法的依赖注入

 

package com.fuwh.spring;

 

/*
 * POJO类
 */
public class Lesson {
private String name;
private int score;
public void setName(String name) {
System.out.println("name parameter is injected");
this.name = name;
}
public void setScore(int score) {
System.out.println("score parameter is injected");
this.score = score;
}
@Override
public String toString() {
return "Lesson [name=" + name + ", score=" + score + "学分]";
}
}

 

<?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">
    <!-- spring配置文件 -->
<bean id="lesson" class="com.fuwh.spring.Lesson" lazy-init="default"
p:name="php"
p:score="2"/>
</beans>

 

 

package com.fuwh.spring;

 

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Spring01 {
public static void main(String[] args) {
/*
* 测试类
*/
ApplicationContext ac=new ClassPathXmlApplicationContext("beanLesson.xml");
System.out.println(ac.getBean("lesson",Lesson.class));
}
}

 在注入的时候,使用以上两种方式都是可以的,但是在以下一种情况下,只能使用setter的方式注入

Class A的构造方法中需要Class B的实例, Class B的构造方法中又需要Class A的实例,

这时候就会报BeanCurrentlyInCreationException的exception.

 

 

Spring特性--DI

标签:

原文地址:http://www.cnblogs.com/zerotomax/p/5720601.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!