[spring]Bean注入——在XML中配置
Bean注入的方式有两种:
一、在XML中配置
- 属性注入
- 构造函数注入
- 工厂方法注入
二、使用注解的方式注入@Autowired,@Resource,@Required
本文首先讲解在XML中配置的注入方式
1.属性注入
属性注入即通过setXxx()方法注入Bean的属性值或依赖对象,由于属性注入方式具有可选择性和灵活性高的优点,因此属性注入是实际应用中最常采用的注入方式。
属性注入要求Bean提供一个默认的构造函数,并为需要注入的属性提供对应的Setter方法。Spring先调用Bean的默认构造函数实例化Bean对象,然后通过反射的方式调用Setter方法注入属性值。
java代码:
public class LogonService implements BeanNameAware{
private UserDao userDao; public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} public UserDao getUserDao() {
return userDao;
}
}
bean.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-autowire="byName"
>
<bean id="userDao" class="com.baobaotao.anno.UserDao"/>
<bean id="propertyId" class="com.baobaotao.anno.LogonService">
//ref中的"userDao"为前一行的bean id
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
2.构造方法注入(使用构造函数注入的前提是Bean必须提供带参数的构造函数)
java代码:
public class LogonService implements BeanNameAware{ public LogonService(){}
//使用构造函数注入的前提是Bean必须提供带参数的构造函数
public LogonService(LogDao logDao, UserDao userDao) {
this.logDao = logDao;
this.userDao = userDao;
} private LogDao logDao; private UserDao userDao; public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} public void setLogDao(LogDao logDao) {
this.logDao = logDao;
} public LogDao getLogDao() {
return logDao;
}
public UserDao getUserDao() {
return userDao;
} }
bean.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-autowire="byName"> <bean id="logDao" class="com.baobaotao.anno.LogDao"/>
<bean id="userDao" class="com.baobaotao.anno.UserDao"/>
<bean class="com.baobaotao.anno.LogonService">
<constructor-arg ref="logDao"></constructor-arg>
<constructor-arg ref="userDao"></constructor-arg>
</bean> </beans>
3.工厂方法注入
factory-bean:用于实例化工厂类;
factory-method:用于调用工厂类方法;
java代码:
public class CarFactory {
//非静态方法
public Car createCar(){
Car car = new Car();
car.setBrand("BMW");
return car;
} //静态方法
public static Car createStaticCar(){
Car car = new Car();
return car;
}
}
bean.xml配置:
对于非静态方法createCar(必须实例化工厂类(factory-bean)后才能调用工厂方法)的注入方式:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 工厂方法-->
<bean id="carFactory" class="com.baobaotao.ditype.CarFactory" />
<bean id="car5" factory-bean="carFactory" factory-method="createCar">
</bean> </beans>
对于静态方法createStaticCar(无须创建工厂类实例的情况下就可以调用工厂类方法)的注入方式:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="car6" class="com.baobaotao.ditype.CarFactory"
factory-method="createStaticCar"></bean> </beans>
[spring]Bean注入——在XML中配置的更多相关文章
- 源码跟读,Spring是如何解析和加载xml中配置的beans
Spring版本基于: 跟踪代码源码基于: https://github.com/deng-cc/KeepLearning commit id:c009ce47bd19e1faf9e07f12086c ...
- web.xml中配置Spring中applicationContext.xml的方式
2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...
- Spring bean注入方式
版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5619525.html Spring bean提供了3中注入方式:属 ...
- Spring整合Hibernate的XML文件配置,以及web.xml文件配置
利用Spring整合Hibernate时的XML文件配置 applicationContext.xml <?xml version="1.0" encoding=" ...
- SSH整合,applicationContext.xml中配置hibernate映射文件问题
今天在applicationContext.xml中配置sessionFactory时遇到了各种头疼的问题,现在总结一下: 1.<property name="mappingDirec ...
- struts2 在 Action 或 Interceptor 中获取 web.xml 中配置的 <context-param> 参数 (这是我的第一篇博文,哈哈。)
最近为了改一个问题,想加一个控制开关,就在web.xml 中配置了一个 <context-param> 参数,并在 Action 或 Interceptor 中获取参数值. 1.在 web ...
- 在web.xml中配置监听器来控制ioc容器生命周期
5.整合关键-在web.xml中配置监听器来控制ioc容器生命周期 原因: 1.配置的组件太多,需保障单实例 2.项目停止后,ioc容器也需要关掉,降低对内存资源的占用. 项目启动创建容器,项目停止销 ...
- 在web.xml中配置error-page
在web.xml中配置error-page 在web.xml中有两种配置error-page的方法,一是通过错误码来配置,而是通过异常的类型来配置,分别举例如下: 一. 通过错误码来配置error ...
- web.xml中配置log4j
1.将 commons-logging.jar和 log4j.jar加入你的项目中:2.在src/下创建log4j.properties|log4j.xml文件:3.在web.xml中配置log4j的 ...
随机推荐
- Windows服务的安装、卸载
创建一个Windows服务 http://jingyan.baidu.com/article/fa4125acb71a8628ac709226.html 安装服务 使用FramWork框架自带的Ins ...
- java-mybaits-00601-查询缓存-一级缓存、二级缓存
1.什么是查询缓存 mybatis提供查询缓存,用于减轻数据压力,提高数据库性能. mybaits提供一级缓存,和二级缓存. 一级缓存是SqlSession级别的缓存. 在操作数据库时需要构造 sql ...
- 更改vim高亮括号匹配颜色
vim括号匹配高亮显示在vim7.4版本, 默认就是开启的. 但是默认的括号匹配 高亮的颜色是浅蓝色, 在亮瞎眼的同时, 严重影响我们写代码, 最明显的感受 就是, 连续打出一对括号, 接下来不仔细看 ...
- TensorFlow学习笔记(四)图像识别与卷积神经网络
一.卷积神经网络简介 卷积神经网络(Convolutional Neural Network,CNN)是一种前馈神经网络,它的人工神经元可以响应一部分覆盖范围内的周围单元,对于大型图像处理有出色表现. ...
- By.Xpath快速定位页面元素常用方法
先看一看xpath的语法 我们将在下面的例子中使用这个 XML 文档. <?xml version="1.0" encoding="ISO-8859-1" ...
- 为什么mysql innodb索引是B+树数据结构
1.文件很大,不可能全部存储在内存中,所以要存在磁盘上 2.索引的组织结构要尽量减少查找过程中磁盘I/O的存取次数(为什么用B-/+Tree,还跟磁盘存取原理有关) 3.B+树所有的data域在叶子节 ...
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C Andryusha and Colored Balloons
地址:http://codeforces.com/contest/782/problem/C 题目: C. Andryusha and Colored Balloons time limit per ...
- [C#]浮点数除零无法捕获异常的解决办法
解决方法: //运算前先检查被除数是否为零,为零则手动抛出除零异常 if (numberB == 0.0) { throw new DivideByZeroException(); } Result ...
- selenium+python学习总结
学习了一个月的selenium+python,终于学有所成,下面以一个简单的项目来总结学习所得. 1. 项目结构 在项目结构中,大家要注意到:每一个源文件夹中都要有一个__init__ ...
- cisco anyconnect linux
cisco anyconnect linux 官方的下载需要登录验证,比较麻烦,可以从这个地方直接下载使用.支持ubuntu,centos. cisco anyconnect vpn client我本 ...