Spring bean注入方式
版权声明:本文为博主原创文章,如需转载请标注转载地址。
博客地址:http://www.cnblogs.com/caoyc/p/5619525.html
Spring bean提供了3中注入方式:属性注入和构造方法注入
1、属性注入:
<bean id="dept" class="com.proc.bean.Dept">
<property name="id" value="2"/>
<property name="name" value="信息部"></property>
</bean>
属性注入方式,要求属性提供呢setXxx方法。上面提供的是普通属性注入,如果要注入对象属性,可以这样
<bean id="user" class="com.proc.bean.User">
<property name="id" value="1" />
<property name="username" value="caoyc"></property>
<property name="dept" ref="dept"></property>
</bean>
我们看到第三个属性dept,是一个Dept类型的属性,可以通过ref来引用一个已定义的Dept类型的dept对象。
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
User user=context.getBean("user", User.class);
System.out.println(user.getUsername());
System.out.println(user.getDept().getName());
结果我们可以看到
caoyc
信息部
除了可以使用ref来引用外部对象外,我们也可以在user对象内部声明一个Dept对象
<bean id="user" class="com.proc.bean.User">
<property name="id" value="1" />
<property name="username" value="caoyc"></property>
<property name="dept">
<bean class="com.proc.bean.Dept">
<property name="id" value="2"/>
<property name="name" value="信息部"></property>
</bean>
</property>
</bean>
2、使用构造器注入
假如,有一个User类
package com.proc.bean;
public class User {
private int id;
private String username;
private int age;
private double slary;
public User() {
}
public User(int id, String username) {
this.id = id;
this.username = username;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", age=" + age
+ ", slary=" + slary + "]";
}
}
<bean id="user" class="com.proc.bean.User">
<constructor-arg value="1"/>
<constructor-arg value="caoyc"/>
</bean>
这里使用的是: public User(int id, String username)构造方式
这里使用的是下标方式,这里省略了index属性。index属性从0开始。上面的代码相当于
<bean id="user" class="com.proc.bean.User">
<constructor-arg index="0" value="1"/>
<constructor-arg index="1" value="caoyc"/>
</bean>
假设有这样的两个构造方式
public User(int id, String username, int age) {
this.id = id;
this.username = username;
this.age = age;
}
public User(int id, String username, double slary) {
this.id = id;
this.username = username;
this.slary = slary;
}
配置bean
<bean id="user" class="com.proc.bean.User">
<constructor-arg index="0" value="1" />
<constructor-arg index="1" value="caoyc"/>
<constructor-arg index="2" value="18"/>
</bean> <bean id="user2" class="com.proc.bean.User">
<constructor-arg index="0" value="1" />
<constructor-arg index="1" value="caoyc"/>
<constructor-arg index="2" value="1800"/>
</bean>
测试代码
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
User user=context.getBean("user",User.class);
System.out.println(user);
User user2=context.getBean("user2",User.class);
System.out.println(user2);
输出结果
User [id=1, username=caoyc, age=0, slary=18.0]
User [id=1, username=caoyc, age=0, slary=1800.0]
这里都是调用的public User(int id, String username, double slary)这个构造函数。那么怎么调用public User(int id, String username, int age)这个构造函数呢?
方法是,在这里我们需要使用到type属性,来指定参数的具体类型
<bean id="user" class="com.proc.bean.User">
<constructor-arg index="0" value="1" />
<constructor-arg index="1" value="caoyc"/>
<constructor-arg index="2" value="18" type="int"/>
</bean>
输出结果
User [id=1, username=caoyc, age=18, slary=0.0]
User [id=1, username=caoyc, age=0, slary=1800.0]
【其它说明】
1、如果value属性是基本属性直接使用
2、如果valeu属性是其它类型,需要使用ref引用外部类型或使用内部定义方式
3、如果value属性中包含了xml特殊字符,需要使用CDATA来。例如:
<constructor-arg index="1">
<value><![CDATA[<caoyc>]]></value>
</constructor-arg>
Spring bean注入方式的更多相关文章
- Spring 依赖注入方式详解
平常的Java开发中,程序员在某个类中需要依赖其它类的方法. 通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理. Spring提出了依赖注入的思想,即依赖类不由 ...
- Spring 依赖注入方式详解(四)
IoC 简介 平常的Java开发中,程序员在某个类中需要依赖其它类的方法. 通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理. Spring提出了依赖注入的思想 ...
- Spring Bean基本管理--bean注入方式汇总
依赖注入方式:Spring支持两种依赖注入方式,分别是属性注入和构造函数注入.还有工厂方法注入方式. 依赖注入还分为:注入依赖对象可以采用手工装配或自动装配,在实际应用开发中建议使用手工装配,因为自动 ...
- Spring IOC 注入方式
依赖注入通常有如下两种方式: ①设值注入:IOC容器使用属性的Setter方法来注入被依赖的实例. 设值注入是指IOC容器使用属性的Setter方法来注入被依赖的实例.这种注入方式简单.直观,因而在S ...
- 关于Spring的注入方式
spring的三种注入方式: 接口注入(不推荐) getter,setter方式注入(比较常用) 构造器注入(死的应用) 关于getter和setter方式的注入: autowire=" ...
- Spring IOC 注入方式详解 附代码
引言 Spring框架作为优秀的开源框架之一,深受各大Java开发者的追捧,相信对于大家来说并不陌生,Spring之所以这么流行,少不了他的两大核心技术IOC和IOP.我们这里重点讲述Spring框架 ...
- [spring]Bean注入——在XML中配置
Bean注入的方式有两种: 一.在XML中配置 属性注入 构造函数注入 工厂方法注入 二.使用注解的方式注入@Autowired,@Resource,@Required 本文首先讲解在XML中配置的注 ...
- [spring]Bean注入——使用注解代替xml配置
使用注解编程,主要是为了替代xml文件,使开发更加快速. 一.使用注解前提: <?xml version="1.0" encoding="UTF-8"?& ...
- spring+cxf 开发webService(主要是记录遇到spring bean注入不进来的解决方法)
这里不介绍原理,只是记录自己spring+cxf的开发过程和遇到的问题 场景:第三方公司需要调用我们的业务系统,以xml报文的形式传递数据,之后我们解析报文存储到我们数据库生成业务单据: WebSer ...
随机推荐
- 【Floyd】文化之旅
[NOIP2012]文化之旅 题目描述 有一位使者要游历各国,他每到一个国家,都能学到一种文化,但他不愿意学习任何一 种文化超过一次(即如果他学习了某种文化,则他就不能到达其他有这种文化的国家).不 ...
- 【高精度】POJ1001-Exponentiation
整个题库的第二题,原本都没有屑于去做,突发奇想抱着秒杀的心态去写了代码,却硬生生地吃了4个WA.. [思路]先去除掉小数点,进行最基本的高精度乘法运算,再在运算得到的结果中添加小数点输出. [前铺]让 ...
- bzoj 1433: [ZJOI2009]假期的宿舍
1433: [ZJOI2009]假期的宿舍 Description Input Output Sample Input 1 3 1 1 0 0 1 0 0 1 1 1 0 0 1 0 0 Sample ...
- KVM使用virsh console无法连接的解决办法(转)
一.问题描述: KVM中宿主机通过console无法连接客户机,卡在这里不动. # virsh console vm01 Connected to domain vm01 Escape charact ...
- 为什么fis没有freemarker的解决方案啊?_前端吧_百度贴吧
为什么fis没有freemarker的解决方案啊?_前端吧_百度贴吧 fis-plus:适用于PHP+Smarty后端选型jello:适用于Java+Velocity后端选型goiz:适用于go+ma ...
- 微信小程序,开发者工具更新以后wxss编译错误
出现上述错误,解决方法如下: 1.在控制台输入openVendor() : 2.清除里面的wcsc wcsc.exe 3.重启开发者工具 搞定!
- sql server阻塞(block)处理
sp_who2 ACTIVE --从下图可知spid = 65进程被76阻塞 --或 * FROM sys.sysprocesses WHERE blocked <> 0 ) --查看阻塞 ...
- Storm常见模式——分布式RPC
Storm常见模式——分布式RPC 本文翻译自:https://github.com/nathanmarz/storm/wiki/Distributed-RPC,作为学习Storm DRPC的资料,转 ...
- xmodmap使用指南
什么是 xmodmap 改变按键的行为 修改修饰键的行为 修改鼠标按键行为 Fvwm中的修辞键使用 1. 什么是 xmodmap xmodmap 是一个在 X 图形环境下用于修改键盘和鼠标按钮映射的工 ...
- TensorFlow------单层(全连接层)实现手写数字识别训练及测试实例
TensorFlow之单层(全连接层)实现手写数字识别训练及测试实例: import tensorflow as tf from tensorflow.examples.tutorials.mnist ...