1 通过setter方法注入

bean类:

package com.test;   

public class UserServiceImplement implements IUserService   

{
private IUserDao user; public IUserDao getUser() {
return user;
} public void setUser(IUserDao user) {
this.user = user;
} public void saveUser() {
user.addUser();
}
}

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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
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-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean id="userdao" class="com.test.UserDaoImplement"/>
<bean id="userservice" class="com.test.UserServiceImplement">
<property name="user" ref="userdao" />
</bean>
</beans>

注:setter方式注入,对应的注入依靠属性,必须要有setter方法。

2 通过构造方法注入

bean类:

package com.test;   

public class UserServiceImplement implements IUserService {
private IUserDao user;
int age; public UserServiceImplement(IUserDao user, int age) {
this.user = user;
this.age = age;
} public void saveUser() {
user.addUser();
System.out.println(this.age);
}
}

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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
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-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean id="userdao" class="com.test.UserDaoImplement" />
<bean id="userservice" class="com.test.UserServiceImplement">
<constructor-arg index="0" type="com.test.IUserDao" ref="userdao"></constructor-arg>
<constructor-arg index="1" value="24"></constructor-arg>
</bean>
</beans>

注:

    • 对于<construcotr-arg>标签中的index属性,假如构造方法只有一个参数的时候可以不指定;它的下标从0开始,表示构造方法参数的索引;假如构造方法有多个参数必须指定索引。除此之外还有一个type属性,type是一个可选属性,这个属性用来指定被注入的参数的参数类型,一定要跟构造方法中的参数类型一致,假如是一个接口,那么也不应传它的实现类。
    • 假如构造函数的的参数类型是基本数据类型,那么就不用ref属性了,而用value属性设置它的值,而且这些数据类型会自动打包和解包;
    • 要注意bean的作用域问题。
    • 被注入的bean类,一定要有构造方法。

3 通过注解进行注入

  1. @Resource

  2. @Autowired

二者区别:

    • @Resource标注是由JDK提供的,而@Autowired标注是由Spring提供的,因而@Autowired标注会与Spring紧密耦合,所以推荐使用@Resource标注
    • @Resource默认是按照名称来装配注入的,当找不到与名称匹配的bean才会按照类型来装配注入
    • @Autowired默认是按照类型装配注入的,假如想按照名称来转配注入,则需要结合@Qualifier一起使用
    • @Resource和@Autowired都可以用来标注字段或者setter方法

@Resource注入方式代码:

package com.test;   

import javax.annotation.Resource;   

public class UserServiceImplement implements IUserService {   

    @Resource
private IUserDao user; private IUserDao user1; public IUserDao getUser1() {
return user1;
} @Resource
public void setUser1(IUserDao user1) {
this.user1 = user1;
} public void saveUser() {
user.addUser();
System.out.println("user注入成功");
user1.addUser();
System.out.println("user1注入成功");
}
}

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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
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-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config />
<bean id="user" class="com.test.UserDaoImplement"/>
<bean id="user1" class="com.test.UserDaoImplement"/>
<bean id="userservice" class="com.test.UserServiceImplement"/>
</beans>

@Autowired注入方式代码:

package com.test;   

import org.springframework.beans.factory.annotation.Autowired;   

public class UserServiceImplement implements IUserService {   

    @Autowired
private IUserDao user; public IUserDao getUser() {
return user;
} public void setUser(IUserDao user) {
this.user = user;
} public void saveUser() {
user.addUser();
System.out.println("user注入成功");
}
}

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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
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-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config />
<bean id="user" class="com.test.UserDaoImplement"/>
<bean id="userservice" class="com.test.UserServiceImplement"/>
</beans>

Spring中Ioc容器的注入方式的更多相关文章

  1. Spring扩展:Spring的IoC容器(注入对象的方式和编码方式)

    二.Spring的IoC容器 IoC:Inversion of Control(控制反转) DI:Dependency Injection(依赖注入) 三.依赖注入的方式 (1)构造注入 (2)set ...

  2. spring中IOC容器注册和获取bean的实例

    spring中常用的功能主要的是ioc和aop,此处主要说明下,实例注册和使用的方法,此为学习后的笔记记录总结 1.使用xml文件配置 在idea中创建maven工程,然后创建实例Person,然后在 ...

  3. IOC容器和注入方式

    IOC和DI IOC: 反转资源获取的方向 DI: IOC的另一种表述反式,即组件以一些预先定义好的方式(例如:setter方法)接收来自如容器的资源注入 IOC容器对象的关联关系 IOC前生--分离 ...

  4. 【Spring】IoC容器 - 依赖注入

    前言 上一篇文章已经学习了[依赖查找]相关的知识,这里详细的介绍一下[依赖注入]. 依赖注入 - 分类 因为自己是基于小马哥的脉络来学习,并且很认可小马哥梳理的分类方式,下面按照小马哥思想为[依赖注入 ...

  5. Spring 中 IoC 容器简介

    IoC 是一种通过描述来生成或者获取对象的技术,可以说 Spring 是一种基于 IoC 容器编程的框架 在一个系统中可以生成各种对象,并且这些对象都需要进行管理.为了描述这些对象关系,我们需要一个容 ...

  6. Spring 深入——IoC 容器 01

    IoC容器的实现学习--01 目录 IoC容器的实现学习--01 简介 IoC 容器系列的设计与实现:BeanFactory 和 ApplicationContext BeanFactory load ...

  7. 【SSH系列】深入浅出spring IOC中三种依赖注入方式

    spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,控制反转一般分为两种类型,依赖注入和依赖查找,依赖什么?为什么需要依赖?注入什么?控 ...

  8. spring IOC中四种依赖注入方式

    在spring ioc中有三种依赖注入,分别是:https://blog.csdn.net/u010800201/article/details/72674420 a.接口注入:b.setter方法注 ...

  9. 深入浅出spring IOC中三种依赖注入方式

    深入浅出spring IOC中三种依赖注入方式 spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,控制反转一般分为两种类型,依赖注入和 ...

随机推荐

  1. 公共Webservice

    网络上可供测试的Web Service腾讯QQ在线状态 WEB 服务Endpoint: http://www.webxml.com.cn/webservices/qqOnlineWebService. ...

  2. vsftp关于"550 create directory operation failed"问题解决

    前提: 昨天晚上配置好了vsftp, 但登陆后,除了浏览,什么也干不了.(如新建文件/文件夹, 删除文件, 重命名等都不可操作) 都是弹出 "550 create directory ope ...

  3. 用POP动画引擎实现衰减动画(POPDecayAnimation)

    效果图: #import "ViewController.h" #import <POP.h> @interface ViewController () @end @i ...

  4. 洛谷 P1515 旅行

    P1515 旅行 题目描述 你要进行一个行程为7000KM的旅行,现在沿途有些汽车旅馆,为了安全起见,每天晚上都不开车,住在汽车旅馆,你手里现在已经有一个旅馆列表,用离起点的距离来标识,如下: 0, ...

  5. C++ 约瑟夫环

    约瑟夫环: 已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出列:依此规律重复下去,直到圆桌周 ...

  6. [转载] 与WIN不同,linux替换文件夹会删除原文件夹下的全部内容!

    今天差点把源码给覆盖掉了><...555... 虚惊一场!!看了一篇博客分析这种情况.我的环境是CentOS5.5,不会出现文件夹直接被覆盖的情况,但是在Linux下不要用Win下的一些直 ...

  7. Linux 挂载iso,并设置为源

    ubuntu在安装lsb-core时需要从 /media/cdrom中查找源,无奈我机器的光驱被我换为硬盘了,无法安装光盘,只有在网上下载的iso文件在硬盘中,所以把iso挂载到它要查找位置 执行: ...

  8. CentOS+nginx+uwsgi+Python 多站点环境搭建

    转载:http://www.cnblogs.com/xiongpq/p/3381069.html 环境: CentOS X64 6.5 nginx 1.5.6 Python 2.7.5 正文: 一:安 ...

  9. CentOS 7 之几个新特性(转)

    上篇我们讲到默认没有ifconfig是centos7的新特性,所以我特意上网搜索了一下其新特性,找到一篇文章,现转过来. centos最小好化安装没有ifconfig命令 刚安装了centos7.0, ...

  10. Input File 表单上传按钮美化

    HTML <div class="input-file-button"> 上传图片<input type="file" class=" ...