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-控制反转,是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,控制反转一般分为两种类型,依赖注入和 ...
随机推荐
- SlidingMenu导入编译用法--Eclipse和IDEA
非常多側滑的应用都用的是开源库SlidingMenu, 效果不错,下面是我用上的效果图,因为近期换成了IDEA(IntelliJ)编辑器,昨天上网找了全部的教程都是关于在Eclipse导入的方法,摸索 ...
- 自定义控件(视图)2期笔记05:自定义控件之继承自View(滑动开关)
1. 开关按钮点击效果,如下: 2. 继承已有View实现自定义View 3. 下面通过一个案例实现滑动开关的案例: (1)新建一个新的Android工程,命名为" 开关按钮", ...
- VS2013配置文件常见问题解决方法
1.C++ VS2013出现LINK : fatal error LNK1104: 无法打开文件“kernel32.lib,”错误,什么原因啊?原因:项目->XX(项目名称)属性->链接器 ...
- HDu -2844 Coins多重背包
这道题是典型的多重背包的题目,也是最基础的多重背包的题目 题目大意:给定n和m, 其中n为有多少中钱币, m为背包的容量,让你求出在1 - m 之间有多少种价钱的组合,由于这道题价值和重量相等,所以就 ...
- 使用SQL Server CONVERT() 函数
语法 CONVERT(data_type(length),data_to_be_converted,style) data_type(length) 规定目标数据类型(带有可选的长度).data_to ...
- C++ for 循环
C++的另一种for循环写法,和C#的foreach语法很类似,不需要知道数组的类型: C++:for(auto& item:items) C#:foreach(var item in ite ...
- JavaScript 客户端JavaScript之Document对象中的表单和表单元素
Form对象 代表一个HTML表单(document可以有多个表单元素) 表单访问 document.form[document.forms.length-1] 访问表单元素 document.for ...
- JavaScript不一样的语法
JavaScript除了面向对象的部分,基本语法和C语言类似,但是也有一些自己的特别之处,现总结如下: (1)break和continue后面可以跟label 语法: break labelname; ...
- phpmyadmin密码字段加密方法
UPDATE member SET password=md5('password')
- C语言刷新缓冲区(转载)
C语言中有几个基本输入函数: //获取字符系列 int fgetc(FILE *stream); int getc(FILE *stream); int getchar(void); //获取行系列 ...