[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的 ...
随机推荐
- 如何理解PHP的单例模式
单例模式就是让类的一个对象成为系统中的唯一实例,避免大量的 new 操作消耗的资源. PHP的单例模式实现要求: 1.一个private的__construct是必须的,单例类不能在其它类中实例化,只 ...
- 11g ASM新特性
Oracle 11g的ASM有两个有意思的特性,我们看看他们能带给我们什么? 1.Fast mirror resync 原来当diskgroup中的盘发生故障时,Oracle会将这个盘标记为offli ...
- Selenium-Grid2 配置RemoteWebDriver
为什么要使用Selenium Grid ? 分布式运行大规模的Test 能够通过一个中央点,很容易的运行不同OS上的不同browser 最小化对Grid的维护时间,并能充分利用虚拟设备 Seleniu ...
- 64. Minimum Path Sum(最小走棋盘 动态规划)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- ASP.NET Core EF 查询获取导航属性值,使用Include封装
// 引用 using Microsoft.EntityFrameworkCore; // 摘要: // Specifies related entities to include in the qu ...
- 页面渲染是否结束 与 jquery插件方法是否可以应用
只有页面全部 渲染结束,才可以调用 插件的方法. 正确写法: $(function(){ 插件调用方法. })
- 使用svn的过程中check out的文件路径中的文件图标全都加上了“蓝色问号”的解决方案
(1)你在对同一层目录下创建一个记事本文件,然后把下面这句话复制进去 for /r . %%a in (.) do @if exist "%%a\.svn" rd /s /q &q ...
- Django学习笔记之CBV和FBV
FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV CBV(class base v ...
- Kali视频学习1-5
Kali视频学习1-5 安装 安装Kali虚拟机 设置网络更新,使用了163的源 deb http://mirrors.163.com/debian wheezy main non-free cont ...
- [翻译]纠正PostCSS的4大认识误区
市面上已经有很多的前端工具,再来引入新的前端工具,价值大不大?这主要取决于,它是否给开发人员提供了新的功能,是否值得花时间和精力去学习和使用? PostCSS出现时有一个很有趣的现象.像sass和le ...