使用Spring实现依赖注入

基本条件:所有的类要被spring所管理才能进行依赖注入

A. 依赖注入

我们以下面这个例子说明如何进行依赖注入:


代码准备:
interfaces:

/**
 * 有机箱
 */
public interface IComputerCase {
    public String sysComputerCaseName();
}

/**
 * 键盘鼠标
 */
public interface IKeyboard {
    public String sysKeyboardName();
}

/**
 * 显示器
 */
public interface IMonitor {
    public String sysMonitorName();
}

entity:

public class Acer implements IMonitor {
    public String sysMonitorName() {
        return "宏基";
    }
}

public class BenQ implements IMonitor {
    public String sysMonitorName() {
        return "明基";
    }
}

public class Bubalus implements IComputerCase {
    public String sysComputerCaseName() {
        return "大水牛";
    }
}

public class GoldenField implements IComputerCase {
    public String sysComputerCaseName() {
        return "金和田";
    }
}

public class Logitech implements IKeyboard {
    public String sysKeyboardName() {
        return "罗技";
    }
}

public class Rapoo implements IKeyboard {
    public String sysKeyboardName() {
        return "雷柏";
    }
}

1.使用set注入(最常用的方式)

通过setxx方法注入:property

1.1 提供相应要注入的类的setter

public class Computer1 {

    private IComputerCase computerCase;
    private IKeyboard keyboard;
    private IMonitor monitor;

    public IComputerCase getComputerCase() {
        return computerCase;
    }

    //提供要注入的类的set方法
    public void setComputerCase(IComputerCase computerCase) {
        this.computerCase = computerCase;
    }

    public IKeyboard getKeyboard() {
        return keyboard;
    }

    //提供要注入的类的set方法
    public void setKeyboard(IKeyboard keyboard) {
        this.keyboard = keyboard;
    }

    public IMonitor getMonitor() {
        return monitor;
    }

    //提供要注入的类的set方法
    public void setMonitor(IMonitor monitor) {
        this.monitor = monitor;
    }
}

1.2 在配置文件中注入
  name中的值会在Computer对象中调用setXX方法来注入(所以方法名称不能变)。比如注入name="computerCase"在具体注入时会回调用setComputerCase来完成注入。ref="ComputerCase1"表示是配置文件中的bean中所创建的ComputerCase1的id。

<?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-3.1.xsd">

    <bean id="ComputerCase1" class="com.IOCExercise.entity.Bubalus"/>
    <bean id="ComputerCase2" class="com.IOCExercise.entity.GoldenField"/>

    <bean id="Keyboard1" class="com.IOCExercise.entity.Logitech"/>
    <bean id="Keyboard2" class="com.IOCExercise.entity.Rapoo"/>

    <bean id="Monitor1" class="com.IOCExercise.entity.Acer"/>
    <bean id="Monitor2" class="com.IOCExercise.entity.BenQ"/>

    <bean id="Computer1" class="com.IOCExercise.test.Computer1">
       <!--向Computer1注入computerCase-->
       <property name="computerCase" ref="ComputerCase1" />
       <!--向Computer1注入keyboard-->
       <property name="keyboard" ref="Keyboard1" />
       <!--向Computer1注入monitor-->
       <property name="monitor" ref="Monitor1" />
     </bean>
</beans>

1.3 测试

public class Computer1SetterTest {

    public static void main(String[] args) {
        //以ApplicationContext方式
        ApplicationContext context=
                new ClassPathXmlApplicationContext("bean1.xml");
        Computer1 aComputer1=(Computer1)context.getBean("Computer1");
        System.out.println("computer1's computercase:"+aComputer1.getComputerCase().sysComputerCaseName());
        System.out.println("computer1's keyboard:"+aComputer1.getKeyboard().sysKeyboardName());
        System.out.println("computer1's monitor:"+aComputer1.getMonitor().sysMonitorName());
}

2. 基于构造函数的注入(了解)

通过构造函数注入:constructor-arg

这种方式的注入是指带有参数的构造函数注入,如果未设置对象的set方法,所以就不能支持第一种注入方式。这里的注入方式是在构造函数中注入,也就是说在创建Computer2 对象时要将computerCase、keyboard和monitor这三个参数值传进来。

2.1 提供相应要注入的类的构造方法

public class Computer2 {

    public IComputerCase computerCase;

    public IKeyboard keyboard;

    public IMonitor monitor;

    public Computer2(IComputerCase computerCase,IKeyboard keyboard,IMonitor monitor)
    {
        this.computerCase = computerCase;
        this.keyboard = keyboard;
        this.monitor = monitor;
    }
}

2.2 在配置文件中注入
  在XML文件中同样不用<property>的形式,而是使用<constructor-arg>标签,ref属性同样指向其它<bean>标签的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" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <bean id="ComputerCase1" class="com.IOCExercise.entity.Bubalus"/>
    <bean id="ComputerCase2" class="com.IOCExercise.entity.GoldenField"/>

    <bean id="Keyboard1" class="com.IOCExercise.entity.Logitech"/>
    <bean id="Keyboard2" class="com.IOCExercise.entity.Rapoo"/>

    <bean id="Monitor1" class="com.IOCExercise.entity.Acer"/>
    <bean id="Monitor2" class="com.IOCExercise.entity.BenQ"/>

    <bean id="Computer2" class="com.IOCExercise.test.Computer2">
        <constructor-arg ref="ComputerCase2"/>
        <constructor-arg ref="Keyboard2"/>
        <constructor-arg ref="Monitor2"/>
    </bean>
</beans>

当构造函数有多个参数时,可以使用constructor-arg标签的index属性,index属性的值从0开始。下面是设置index,就是参数位置:

    <bean id="Computer2" class="com.IOCExercise.test.Computer2">
        <constructor-arg index="0" ref="ComputerCase2"/>
        <constructor-arg index="1" ref="Keyboard2"/>
        <constructor-arg index="2" ref="Monitor2"/>
    </bean>

2.3 测试

public class Computer2ConsTest {

    public static void main(String[] args) {
        //以ApplicationContext方式
        ApplicationContext context=
                new ClassPathXmlApplicationContext("bean1.xml");
        Computer2 aComputer2=(Computer2)context.getBean("Computer2");
        System.out.println("computer2's computercase:"+aComputer2.computerCase.sysComputerCaseName());
        System.out.println("computer2's keyboard:"+aComputer2.keyboard.sysKeyboardName());
        System.out.println("computer2's monitor:"+aComputer2.monitor.sysMonitorName());
}

B. 属性注入和自动注入

1. 属性注入
以下面的例子说明如何进行属性注入:

![Upload Paste_Image.png failed. Please try again.]

Classes类:

public class Classes {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Student类:

public class Student {
    //姓名
    private String name;
    //学号
    private int sno;
    //班级
    private Classes classes;
    //课程分数
    private double[] score;
   //课程列表
    private List<String> course;
    private Set<String> hobby;
    private Map other;

    //setter、getter
}

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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <bean id="Classes3" class="com.IOCExercise.entity.Classes">
        <property name="name" value="软件R3班" />
    </bean>

    <bean id="Student" class="com.IOCExercise.entity.Student">
        <!--通过value可以注入一个基本数据类型-->
        <property name="name" value="zoyoto" />
        <property name="sno" value="26" />
        <!--引用Spring中定义的classes对象-->
        <property name="classes" ref="Classes3" />
        <!--注入数组-->
        <property name="score">
            <array>
                <value>91</value>
                <value>88</value>
            </array>
        </property>
        <!--注入list-->
        <property name="course">
            <list>
                <value>语文</value>
                <value>数学</value>
            </list>
        </property>
        <!--注入Set-->
        <property name="hobby">
            <set>
                <value>画画</value>
                <value>羽毛球</value>
            </set>
        </property>
        <!--注入Map-->
        <property name="other">
            <map>
                <entry key="beijing" value="北京"></entry>
                <entry key="guangzhou" value="广州" ></entry>
            </map>
        </property>
    </bean>
</beans>

2. 自动注入(一般不使用)

  • byName是根据set的名称注入的,如果名称不对就无法注入(默认情况)
  • byType是根据类型来注入的,和名称无关,如果一个类中有两个相同类型的对象则无法注入。

如果要使用一般使用byName:


C. 基于annotation的注入

使用@Resource、@Autowired、@Repository、@Service、@Controller 和 @Component可以将类标识为 Bean。

annotation介绍

@Resource
  @Resource就相当于application.getBean()方法。@Resource有两个中重要的属性:name和type ,而Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用 byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。

@Autowired
  @Autowired作用和@Resource一样,但@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用,如下:

@Autowired() 
@Qualifier("baseDao")     
private BaseDao baseDao;   

@Component
  是一个泛华的概念,仅仅表示一个组件,可以作用于任何层次。

@Repository
  只能标注在DAO层,这是因为该注解的作用不只是将类识别为Bean,同时它还能将所标注的类中抛出的数据访问异常封装为Spring的数据访问异常类型

@Service
  通常作用在业务层,但是目前该功能与@Component相同。

@Controller
  通常作用在控制层,但是目前该功能与@Component相同。

1.设置bean.xml的schema


2.在类上面设置


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容