Spring中Ioc容器的注入方式
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 通过注解进行注入
- @Resource
- @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容器的注入方式的更多相关文章
- Spring扩展:Spring的IoC容器(注入对象的方式和编码方式)
二.Spring的IoC容器 IoC:Inversion of Control(控制反转) DI:Dependency Injection(依赖注入) 三.依赖注入的方式 (1)构造注入 (2)set ...
- spring中IOC容器注册和获取bean的实例
spring中常用的功能主要的是ioc和aop,此处主要说明下,实例注册和使用的方法,此为学习后的笔记记录总结 1.使用xml文件配置 在idea中创建maven工程,然后创建实例Person,然后在 ...
- IOC容器和注入方式
IOC和DI IOC: 反转资源获取的方向 DI: IOC的另一种表述反式,即组件以一些预先定义好的方式(例如:setter方法)接收来自如容器的资源注入 IOC容器对象的关联关系 IOC前生--分离 ...
- 【Spring】IoC容器 - 依赖注入
前言 上一篇文章已经学习了[依赖查找]相关的知识,这里详细的介绍一下[依赖注入]. 依赖注入 - 分类 因为自己是基于小马哥的脉络来学习,并且很认可小马哥梳理的分类方式,下面按照小马哥思想为[依赖注入 ...
- Spring 中 IoC 容器简介
IoC 是一种通过描述来生成或者获取对象的技术,可以说 Spring 是一种基于 IoC 容器编程的框架 在一个系统中可以生成各种对象,并且这些对象都需要进行管理.为了描述这些对象关系,我们需要一个容 ...
- Spring 深入——IoC 容器 01
IoC容器的实现学习--01 目录 IoC容器的实现学习--01 简介 IoC 容器系列的设计与实现:BeanFactory 和 ApplicationContext BeanFactory load ...
- 【SSH系列】深入浅出spring IOC中三种依赖注入方式
spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,控制反转一般分为两种类型,依赖注入和依赖查找,依赖什么?为什么需要依赖?注入什么?控 ...
- spring IOC中四种依赖注入方式
在spring ioc中有三种依赖注入,分别是:https://blog.csdn.net/u010800201/article/details/72674420 a.接口注入:b.setter方法注 ...
- 深入浅出spring IOC中三种依赖注入方式
深入浅出spring IOC中三种依赖注入方式 spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,控制反转一般分为两种类型,依赖注入和 ...
随机推荐
- C++之static
一.静态全局变量和非静态全局变量 1. 隐藏作用 比较非静态全局变量和静态(static)全局变量: 对于多个文件的代码,非静态全局变量和函数都是全局可见的.举例如下: a.c中: #include& ...
- 如何实现数字lcd显示效果(原创)
如题,我最先想到的是找一种字体,然后来显示lcd的效果,但是字体又无法满足有空位的时候那个暗灰色的文字的效果,如下所示 就是前三位那些灰色的888,因为你设置数值的时候只能是从0-9的数字,而这灰色的 ...
- python Sina微博自动转发带抽奖字样的微博,添加关注,取消关注
项目地址:https://github.com/chengshuyi/SinaWeibo 具有的功能 转发带抽奖字样的微博并可以@相应数量的好友 提取关注并添加关注 取消关注 获取粉丝列表
- NYOJ 116士兵杀敌(二) 树状数组
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=116 士兵杀敌(一) 数组是固定的,所以可以用一个sum数组来保存每个元素的和就行,但是不 ...
- Gamit的安装
--2017-1-6修正 我这里用的是Gamit10.5,系统是Ubunt 14. 1:获取root权限(仅针对装好Ubunt后没有修改过root密码的用户,如果修改过密码,但是忘掉了,则请进入单用户 ...
- 把数据库中的null作为条件查询应该用is
如select * from mbXX where tuijian is null 而不是select * from mbXX where tuijian=null
- XFire构建服务端Service的两种方式
1.原声构建: 2.集成spring构建 http://blog.csdn.net/carefree31441/article/details/4000436XFire构建服务端Service的两种方 ...
- MongoDB数据库导出导入迁移
在server 1导出 /usr/local/mongodb/bin/mongorestore -d adv_new /tmp/mongodb/ 然后会在/tmp/mongodb/目录下生成一个adv ...
- 关于Adobe Flash 11.3 引起的火狐使用问题
Adobe Flash 更新到11.3之后,为火狐引入Flash沙盒安全模式,但同时,又造成了部分兼容性问题,导致 Windows vista及 Windows 7上部分火狐崩溃,并致使一些使用Fla ...
- CSS布局注意(纯属个人总结)
和CSS样式有关多用class,和后台数据有关多用id. 1.使用绝对定位时(偏移量如:top,left...),如果父类没有相对定位,使用绝对定位的元素是相对根元素(即<html>标签) ...