一 setter方法注入

配置文件如下:

<bean id="helloAction" class="org.yoo.action.SpringSetterHelloAction">
<!-- setter injection using the nested <ref/> element -->
<property name="helloservice"><ref bean="helloService"/></property>
<!--可以不设置类型 -->
<property name="name" value="yoo"></property>
</bean>

action实现类中代码:

private IHelloService helloservice;
private String name ;
public void sayHello(){
helloservice.sayHello();
System.out.println(this.name);
}

public void setHelloservice(IHelloService helloservice) {
this.helloservice = helloservice;
}

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

这里的name和helloservice都采用属性的setter方法注入。即类中设置一个全局属性,并对属性有setter方法,以供容器注入。

二 构造器注入

spring也支持构造器注入,也即有些资料中的构造子或者构造函数注入。

先看配置文件:

<!--普通构造器注入-->
<bean id="helloAction" class="org.yoo.action.ConstructorHelloAction">
<!--type 必须为Java.lang.String 因为是按类型匹配的,不是按顺序匹配-->

<constructor-arg type="java.lang.String" value="yoo"/>
<!-- 也可以使用index来匹配-->
<!--<constructor-arg index="1" value="42"/>-->
<constructor-arg><ref bean="helloService"/></constructor-arg>
</bean>

action实现类中代码:

private HelloServiceImpl helloservice;
private String name ;

public SpringConstructorHelloAction(HelloServiceImpl helloservice,String name){
this.helloservice = helloservice;
this.name = name ;
}

@Override
public void sayHello() {
helloservice.sayHello();
System.out.println(this.name);
}

同样设置2个属性,然后在构造函数中注入。

三静态工厂注入

配置文件如下:

<!--静态工厂构造注入-->

<!--注意这里的class 带factory-method参数-->

<!--factory-method的值是FactoryHelloAction中的工厂方法名createInstance-->
<bean id="helloAction" class="org.yoo.di.FactoryHelloAction" factory-method="createInstance">
<constructor-arg type="java.lang.String" value="yoo"/>
<constructor-arg><ref bean="helloService"/></constructor-arg>
</bean>

action实现类:

private HelloServiceImpl helloservice;
private String name = null;

private SpringFactoryHelloAction(String name ,HelloServiceImpl helloservice){
this.helloservice = helloservice ;
this.name = name ;
}

public static SpringFactoryHelloAction createInstance(String name ,HelloServiceImpl helloservice) {
SpringFactoryHelloAction fha = new SpringFactoryHelloAction (name,helloservice);
// some other operations
return fha;
}

@Override
public void sayHello() {
helloservice.sayHello();
System.out.println(this.name);
}
四 无配置文件注入(自动注入)

上面三种方法都需要编写配置文件,在spring2.5中还提供了不编写配置文件的ioc实现。需要注意的是,无配置文件指bean之间依赖,不基于配置文件,而不是指没有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">
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="helloService" class="org.yoo.service.HelloServiceImpl">
</bean>
<bean id="helloAction" class="org.yoo.action.AutowiredHelloAction">
</bean>
</beans>

可见上面只是设置了helloService和helloAction两个bean,并没有设置helloAction对helloService的依赖。

另外<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>是必须的,有此才支持自动注入。

action实现类:

/*
* 属性自动装载
*/
/*
@Autowired
private HelloService helloservice;

@Override
public void sayHello() {
helloservice.sayHello();

上面代码很简单,在属性上加上 @Autowired注释,spring会自动注入HelloService 。

同样也支持构造器和setteer方法的注入,如下:

构造器自动注入:

/*
* 还可以使用构造函数设置
*/
@Autowired
public SpringAutowiredHelloAction(HelloServiceImpl helloservice){
this.helloservice = helloservice;
}

setter方法自动注入:

/*
* 也可以使用set方法设置
*/
/*
@Autowired
public void setHelloservice(HelloService helloservice) {
this.helloservice = helloservice;
}

最后在spring的reference文档中有提到,如果不使用自动注入,尽量使用setter方法,一般通用的也是使用setter方法。

而使用自动注入还是配置文件方式,如果jdk低于1.5或者spring不是2.5,就只能够使用配置文件方式了。其它的就看实际项目选择了。

 

Spring Ioc-依赖注入的几种方式的更多相关文章

  1. Spring IOC 依赖注入的两种方式XML和注解

    依赖注入的原理 依赖注入的方式---XML配置 依赖注入的方式---注解的方式 Spring 它的核心就是IOC和AOP.而IOC中实现Bean注入的实现方式之一就是DI(依赖注入). 一 DI的原理 ...

  2. Spring中依赖注入的四种方式

    在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入  这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的sett ...

  3. Spring注解依赖注入的三种方式的优缺点以及优先选择

    当我们在使用依赖注入的时候,通常有三种方式: 1.通过构造器来注入: 2.通过setter方法来注入: 3.通过filed变量来注入: 那么他们有什么区别吗?应该选择哪种方式更好? 三种方式的区别小结 ...

  4. spring的依赖注入的四种方式,数组与集合注入;引用注入;内部bean注入

    三种注入方式 第一种: 基于构造函数 hi.java (bean) package test_one; public class hi { private String name; public hi ...

  5. Spring的依赖注入的2种方式(1天时间)

    今天花了一天的时间才调试出来 private      接口   实现类的那个bean; 最后面的那个名字不能随便的写,必须是配置文件中,实现类的那个bean 就是后面的那个名字写错了,花了整整一天 ...

  6. ASP.NET MVC中使用Unity进行依赖注入的三种方式

    在ASP.NET MVC中使用Unity进行依赖注入的三种方式 2013-12-15 21:07 by 小白哥哥, 146 阅读, 0 评论, 收藏, 编辑 在ASP.NET MVC4中,为了在解开C ...

  7. 峰Spring4学习(2)依赖注入的几种方式

    一.装配一个bean 二.依赖注入的几种方式 com.cy.entity   People.java: package com.cy.entity; public class People { pri ...

  8. SSH深度历险记(八) 剖析SSH核心原则+Spring依赖注入的三种方式

           于java发育.一类程序猿必须依靠类的其他方法,它是通常new依赖类的方法,然后调用类的实例,这样的发展问题new良好的班统一管理的例子.spring提出了依赖注入的思想,即依赖类不由程 ...

  9. SSH深度历险(八) 剖析SSH核心原理+Spring依赖注入的三种方式

           在java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依 ...

  10. Spring:DI依赖注入的几种方式

    据我所学,spring实现依赖注入(DI)的方式分为三大类:基于构造器(构造方法)的依赖注入.基于setter的依赖注入.其他方式(c命名空间.p命名空间等).其中推荐使用setter方法注入,这种注 ...

随机推荐

  1. webuploader插件,我踩得坑

    我在目前的公司做的项目要么是原生写法去做项目,要么就是vue+webpack做项目,但是vue这部分只是用了模板template,vue其他的都没用. 有一个项目需要做上传图片的功能,老大扔给我一个插 ...

  2. 用户权限模块之spring security

    准备工作:数据库采用mysql(5.6及以上) CREATE TABLE `auth_system` ( `ID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'I ...

  3. 破解 Adobe 系列的最佳方法,手把手教

    此方法是个人认为最方便的而且最安全的方法(可以避免下载到可能捆绑病毒的破解版本) 1.首先到Adobe的官网上下载 Creative Cloud: 打开官网上creative cloud 的下载页面: ...

  4. 第2章 系统用户/组管理(2) su和sudo

    本文目录: 2.1 su 2.2 sudo 2.2.1 /etc/sudoers文件 2.2.2 sudo和sudoedit命令 2.1 su 切换用户或以指定用户运行命令. 使用su可以指定运行命令 ...

  5. 开始学习机器学习,从Ng的视频开始

    时隔开5个月,忙完了考研和毕设后终于有时间搞自己想搞得,研究生导师方向是图像处理与机器学习结合,重新开工 何为机器学习? 对于机器学习(Machine Learning)的定义大体上有两种,第一种是美 ...

  6. jquery中div悬浮嵌套按钮效果

    <div class="btn_sure_cai" style="margin-left: 0px;" onmouseover="show_hi ...

  7. 在CentOS7上使用systemctl配置tomcat

    最近买了一台阿里云,现在阿里云CentOS都已经支持7了(前面都是使用CentOS6),索性直接使用CentOS7,7上面目前最大区别就是service变成了现在的systemctl,简单的查了一下并 ...

  8. [leetcode-565-Array Nesting]

    A zero-indexed array A consisting of N different integers is given. The array contains all integers ...

  9. 【Android Developers Training】 79. 连接到网络

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  10. JQuery事件机制笔记

    一.事件绑定及移除 1.bind() bind()为每个匹配的元素绑定一个或多个事件处理函数: 语法:bind(event,fn)//不能给未来元素添加事件: bind(event,fn); bind ...