【Spring 2】spring的属性注入形式
一、注入简介
spring是一个java bean的容器,它摒弃了过去通过new关键字调用类再调用类的实例的形式,通过依赖注入维护者一系列的java bean的示例。通过spring所提供的依赖注入的方法,我们可以管理这些java bean之间的引用关系。
spring的注入形式,主要是有4种:setter方法、构造器方法、静态 / 实例工厂方法、注解,这里主要介绍属性注入的:setter方法和构造器方法。
二、实例解析
2.1,setter方法属性注入
首先,在applicationContext里面进行配置
<pre name="code" class="html"><span style="font-family:KaiTi_GB2312;font-size:18px;"><?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="userDao4Mysql" class="com.angel.spring.dao.UserDao4MySqlImpl"></bean>
<span> </span><bean id="userDao4Oracle" class="com.angel.spring.dao.UserDao4OracleImpl"></bean>
<span> </span><bean id="userManager" class="com.angel.spring.manager.UserManagerImpl">
<span> </span><property name="userDao" ref="userDao4Oracle" />
<span> </span></bean> </beans></span>
最后,测试类
<span style="font-family:KaiTi_GB2312;font-size:18px;"></span><span style="font-family:KaiTi_GB2312;font-size:18px;">package com.angel.spring.manager;
import com.angel.spring.dao.UserDao;
public class UserManagerImpl implements UserManager {
private UserDao userDao;
public void addUser(String username, String password) {
userDao.addUser(username, password);
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
} </span>
2.2,构造器方法属性注入
首先,在application中的配置
<pre name="code" class="html"><span style="font-family:KaiTi_GB2312;font-size:18px;"><?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
<span style="color:#ff0000;">default-autowire="byType"</span>> <bean id="bean2" class="com.angel.spring.Bean2"></bean> <bean id="beantest3" class="com.angel.spring.Bean3">
<constructor-arg index="0" value="1002" />
<constructor-arg index="1" value="Angel" />
<constructor-arg index="2" value="23" />
</bean>
</beans></span>
其次,在实体类中提供构造方法
<pre name="code" class="java"><span style="font-family:KaiTi_GB2312;font-size:18px;">public Bean3(int id,String name,String sex){
this.id=id;
this.name=name;
this.sex=sex;
}</span>
注意:spring 提供了Resource、Autowired这两个注解用于注入,另外在xml配置文件中,beans标签下有一个参数default-autowire用来设置默认的注入类型。这里我先说明一下default-autowire的用途。首先default-autowire参数有几个可选值:
default:实际上是会优先执行constructor然后是byType
byType:这个是会根据类型去注入,当找到了会直接注入,没有找到不会报错,但是找到多个会报No unique bean of type的错误
byName:这个会根据name注入
constructor:这个是通过构造注入
no:不启用自动注入
三、总结
以上就介绍了属性注入的两种形式:setter和构造器注入。spring同样还支持注解注入,但是今天例子没有做完,等做完了之后,再次进行总结!以前就知道个依赖注入,可是到底是怎么注入的,都没有认真了解过,学习还是需要踏踏实实的!
【Spring 2】spring的属性注入形式的更多相关文章
- spring练习,使用Eclipse搭建的Spring开发环境,属性注入通过构造方法方式实现,模拟用户的正常登录。
相关 知识 >>> 相关 练习 >>> 实现要求: 使用Eclipse搭建的Spring开发环境,属性注入通过构造方法方式实现,模拟用户的正常登录.要求如下: 通过 ...
- Spring学习日记02_IOC_属性注入_其他类型属性
ICO操作Bean管理(xml注入其它类型属性) 字面量 null值 <property name="address"> <null></null&g ...
- Spring中集合类型属性注入
我们都知道如何去注入普通属性的值,非常简单,那么我们如何去注入开发中常见的集合类型的属性了,别急,往下看. 这里将介绍如何给Map list set Array Properties 这些属性注入值. ...
- Spring boot将配置属性注入到bean类中
一.@ConfigurationProperties注解的使用 看配置文件,我的是yaml格式的配置: // file application.yml my: servers: - dev.bar.c ...
- Spring boot将配置属性注入到bean 专题
https://blog.csdn.net/wangmx1993328/article/details/81002901 Error starting ApplicationContext. To d ...
- Spring中@value以及属性注入的学习
1.简单的Java配置 配置文件(jdbc.properties) jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://1 ...
- Spring学习日记03_IOC_属性注入_集合类型属性
Ioc操作Bean管理(xml注入集合属性) 注入数组类型属性 注入List集合类型属性 注入Map集合类型属性 Stu类 public class Stu { //1. 数组类型属性 private ...
- 【坑】Spring中抽象父类属性注入,子类调用父类方法使用父类注入属性
运行环境 idea 2017.1.1 spring 3.2.9.RELEASE 需求背景 需要实现一个功能,该功能有2个场景A.B,大同小异 抽象一个抽象基类Base,实现了基本相同的方法BaseMe ...
- spring的bean的属性注入
一.设置注入 设置注入要求: 要求属性在实体类中必须有getter 和setter方法,然后在spring的工厂中就可以使用property标签进行设值注入. 二.构造注入 通过类的构造方法的方式注入 ...
随机推荐
- jQuery插件:用于获取元素自身的HTML内容
jQuery.fn.outerHTML = function(s) { return (s) ? this.before(s).remove() : $("<Hill_man>& ...
- 精妙SQL语句收集(转)
SQL语句先前写的时候,很容易把一些特殊的用法忘记,我特此整理了一下SQL语句操作. 一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 ...
- mysql多表字段名重复的情况
CREATE TABLE `card` ( `id` ) unsigned NOT NULL AUTO_INCREMENT, `json_str` ) NOT NULL, `f` ,) unsigne ...
- JavaScript: bind apply call
var foo = function(age,sex){ console.log(this.name,age,sex); }; //call将改变函数运行的context foo.call({name ...
- Introduction to ASP.NET Web Programming Using the Razor Syntax (C#)
1, http://www.asp.net/web-pages/overview/getting-started/introducing-razor-syntax-c 2, Introduction ...
- ylbtech-dbs:ylbtech-4,PurpleHouse(房地产楼盘销售系统)
ylbtech-dbs:ylbtech-4,PurpleHouse(房地产楼盘销售系统) -- =============================================-- Crea ...
- js跳转页面方法整理
1.window.location.href方式 window.location.href="http://www.zgw8.com"; 2.window.navigate方式跳转 ...
- Spring Bean 生命周期2
在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一个实例,而不是每次都产生一个新的对象使用Sin ...
- htmlparser日记
myParser = Parser.createParser(response, "utf-8");NodeFilter tableFilter = new NodeClassFi ...
- ubuntu13.10下安装samba
1.现在的ubuntu系统都默认安装了samba服务,如果是精简系统的话,可以通过apt-get进行安装 sudo apt-get install samba sudo apt-get cifs-ut ...