Spring Bean基本管理--bean注入方式汇总
Spring中提供了自动装配依赖对象的机制,但是在实际应用中并不推荐使用自动装配,因为自动装配会产生未知情况,开发人员无法预见最终的装配结果。
自动装配是在配置文件中实现的,如下:
<bean id="***" class="***" autowire="byType">
只需要配置一个autowire属性即可完成自动装配,不用再配置文件中写<property>,但是在类中还是要生成依赖对象的setter方法。
Autowire的属性值有如下几个:
· byType 按类型装配 可以根据属性类型,在容器中寻找该类型匹配的bean,如有多个,则会抛出异常,如果没有找到,则属性值为null;
· byName 按名称装配 可以根据属性的名称在容器中查询与该属性名称相同的bean,如果没有找到,则属性值为null;
· constructor 与byType方式相似,不同之处在与它应用于构造器参数,如果在容器中没有找到与构造器参数类型一致的bean,那么将抛出异常;
· autodetect 通过bean类的自省机制(introspection)来决定是使用constructor还是byType的方式进行自动装配。如果发现默认的构造器,那么将使用byType的方式。
public class HelloBean { private String helloWord; //...省略getter、setter方法 }
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloBean"
class="onlyfun.caterpillar.HelloBean">
<property name="helloWord">
<value>Hello!Justin!</value>
</property>
</bean>
</beans>
java:
public class SpringDemo {
public static void main(String[] args) {
Resource rs = new FileSystemResource("beans-config.xml");
BeanFactory factory = new XmlBeanFactory(rs); HelloBean hello = (HelloBean) factory.getBean("helloBean");
System.out.println(hello.getHelloWord());
}
}
· index是索引,指定注入的属性,从0开始,如:0代表personDao,1代表str属性;
· type是指该属性所对应的类型,如Persondao对应的是com.aptech.dao.PersonDAO;
· ref 是指引用的依赖对象;
· value 当注入的不是依赖对象,而是基本数据类型时,就用value;
public class HelloBean {
private String name;
private String helloWord; // 建议有要无参数建构方法
public HelloBean() {
} public HelloBean(String name, String helloWord) {
this.name = name;
this.helloWord = helloWord;
} //...省略getter、setter方法
}
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloBean"
class="onlyfun.caterpillar.HelloBean">
<constructor-arg index="0">
<value>Justin</value>
</constructor-arg>
<constructor-arg index="1">
<value>Hello</value>
</constructor-arg>
</bean>
</beans>
java代码:
public class SpringDemo {
public static void main(String[] args) {
ApplicationContext context =
new FileSystemXmlApplicationContext("beans-config.xml"); HelloBean hello = (HelloBean) context.getBean("helloBean");
System.out.print("Name: ");
System.out.println(hello.getName());
System.out.print("Word: ");
System.out.println(hello.getHelloWord());
}
}
private String helloWord;
private Date date;
//...省略getter、setter方法
}
<bean id="dateBean" class="java.util.Date"/>
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean">
<property name="helloWord">
<value>Hello!</value>
</property>
<property name="date">
<ref bean="dateBean"/>
</property>
</bean>
</beans>
public static void main(String[] args) {
ApplicationContext context =
new FileSystemXmlApplicationContext("beans-config.xml");
HelloBean hello = (HelloBean) context.getBean("helloBean");
System.out.print(hello.getHelloWord());
System.out.print(" It's ");
System.out.print(hello.getDate());
System.out.println(".");
}
}
<bean id="dateBean" class="java.util.Date"/>
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="byType">
<property name="helloWord">
<value>Hello!</value>
</property>
</bean>
</beans>
<bean id="dateBean" class="java.util.Date"/>
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="byName">
<property name="helloWord">
<value>Hello!</value>
</property>
</bean>
</beans>
<bean id="dateBean" class="java.util.Date"/>
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="constructor">
<property name="helloWord">
<value>Hello!</value>
</property>
</bean>
</beans>
<bean id="dateBean" class="java.util.Date"/>
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="autodetect">
<property name="helloWord">
<value>Hello!</value>
</property>
</bean>
</beans>
<bean id="dateBean" class="java.util.Date"/>
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="autodetect" dependeny-check="all">
<property name="helloWord">
<value>Hello!</value>
</property>
</bean>
</beans>
private String[] someStrArray;
private Some[] someObjArray;
private List someList;
private Map someMap;
public String[] getSomeStrArray() {
return someStrArray;
}
public void setSomeStrArray(String[] someStrArray) {
this.someStrArray = someStrArray;
}
public Some[] getSomeObjArray() {
return someObjArray;
}
public void setSomeObjArray(Some[] someObjArray) {
this.someObjArray = someObjArray;
}
public List getSomeList() {
return someList;
}
public void setSomeList(List someList) {
this.someList = someList;
}
public Map getSomeMap() {
return someMap;
}
public void setSomeMap(Map someMap) {
this.someMap = someMap;
}
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return name;
}
}
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="some1" class="onlyfun.caterpillar.Some">
<property name="name">
<value>Justin</value>
</property>
</bean>
<bean id="some2" class="onlyfun.caterpillar.Some">
<property name="name">
<value>momor</value>
</property>
</bean>
<bean id="someBean" class="onlyfun.caterpillar.SomeBean">
<property name="someStrArray">
<list>
<value>Hello</value>
<value>Welcome</value>
</list>
</property>
<property name="someObjArray">
<list>
<ref bean="some1"/>
<ref bean="some2"/>
</list>
</property>
<property name="someList">
<list>
<value>ListTest</value>
<ref bean="some1"/>
<ref bean="some2"/>
</list>
</property>
<property name="someMap">
<map>
<entry key="MapTest">
<value>Hello!Justin!</value>
</entry>
<entry key="someKey1">
<ref bean="some1"/>
</entry>
</map>
</property>
</bean>
</beans>
public static void main(String[] args) {
ApplicationContext context =
new FileSystemXmlApplicationContext(
"beans-config.xml");
SomeBean someBean =
(SomeBean) context.getBean("someBean");
// 取得数组型态依赖注入对象
String[] strs =
(String[]) someBean.getSomeStrArray();
Some[] somes =
(Some[]) someBean.getSomeObjArray();
for(int i = 0; i < strs.length; i++) {
System.out.println(strs[i] + ","
+ somes[i].getName());
}
// 取得List型态依赖注入对象
System.out.println();
List someList = (List) someBean.getSomeList();
for(int i = 0; i < someList.size(); i++) {
System.out.println(someList.get(i));
}
// 取得Map型态依赖注入对象
System.out.println();
Map someMap = (Map) someBean.getSomeMap();
System.out.println(someMap.get("MapTest"));
System.out.println(someMap.get("someKey1"));
}
}
十、静态工厂的方法注入
十一、实例工厂的方法注入
Spring Bean基本管理--bean注入方式汇总的更多相关文章
- (转)让Spring自动扫描和管理Bean
http://blog.csdn.net/yerenyuan_pku/article/details/52861403 前面的例子我们都是使用XML的bean定义来配置组件.在一个稍大的项目中,通常会 ...
- Spring中bean的四种注入方式
一.前言 最近在复习Spring的相关内容,这篇博客就来记录一下Spring为bean的属性注入值的四种方式.这篇博客主要讲解在xml文件中,如何为bean的属性注入值,最后也会简单提一下使用注解 ...
- spring Bean的三种注入方式
1.构造函数注入: 构造函数的注入方式分为很多种 (1)普通构造函数,空参数的构造函数 <bean id="exampleBean" class="examples ...
- Spring第八发—自动装配及让Spring自动扫描和管理Bean
依赖注入–自动装配依赖对象(了解即可) 对于自动装配,大家了解一下就可以了,实在不推荐大家使用.例子: byName:按名称装配,可以根据属性的名称,在容器中寻找跟该属性名相同的bean,如果没有找到 ...
- Spring是如何管理Bean
容器是什么?spring中是如何体现的?一直有疑惑,这两天看了一下Spring管理bean的Demo,对于Spring中的容器有了简单的认识. 我们知道,容器是一个空间的概念,一般理解为可盛放物体的地 ...
- Spring、Spring自动扫描和管理Bean
Spring2.5为我们引入了组件自动扫描机制,它可以在类路径下寻找标记了@Component.@Service.@Controller.@Repository注解的类,并把这些类纳入到spring容 ...
- Spring IOC以及三种注入方式
IOC是spring的最基础部分,也是核心模块,Spring的其他组件模块和应用开发都是以它为基础的.IOC把spring的面向接口编程和松耦合的思想体现的淋漓尽致. IOC概念 IOC(Invers ...
- Spring中Ioc容器的注入方式
1 通过setter方法注入 bean类: package com.test; public class UserServiceImplement implements IUserService { ...
- Spring常用的三种注入方式
好文要收藏,摘自:https://blog.csdn.net/a909301740/article/details/78379720 Spring通过DI(依赖注入)实现IOC(控制反转),常用的注入 ...
随机推荐
- POJ 3228 Gold Transportation(带权并查集,好题)
参考链接:http://www.cnblogs.com/jiaohuang/archive/2010/11/13/1876418.html 题意:地图上某些点有金子,有些点有房子,还有一些带权路径,问 ...
- Javascript 选项卡
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- HDU 1573 X问题 (中国剩余定理)
题目链接 题意 : 中文题不详述. 思路 : 中国剩余定理.求中国剩余定理中解的个数.看这里看这里 #include <stdio.h> #include <iostream> ...
- N! HDU 1042
N! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- lintcode:anagrams 乱序字符串
题目 乱序字符串 给出一个字符串数组S,找到其中所有的乱序字符串(Anagram).如果一个字符串是乱序字符串,那么他存在一个字母集合相同,但顺序不同的字符串也在S中. 您在真实的面试中是否遇到过这个 ...
- unity3d泰斗破坏神2----课程列表
免费 课时1:泰斗破坏神第一支预告视频 01:32免费 课时2:泰斗破坏神第二支预告视频 01:58第 1 章 : 游戏开始 用户登录 服务器选择课时3:游戏开始 用户登录 服务器选择课时4:素材介绍 ...
- js捕捉浏览器关闭事件-兼容几乎所有浏览器
很多时候我们都在困扰,如何捕获浏览器关闭事件,网上虽然有很多方法,但都不理想,后来终于找到了一个很好地实现方法,大家可以试试哦,支持几乎所有的浏览器 <script type="tex ...
- 关于java -version版本问题
因为安装了Oracle,而Oracel会自带JDK,安装完成后,会自动把自己的JDK设置在最前面(path变量里). 这就是为什么结果与事实不相同的原因. 解决方法: 进入系统环境变量,找到path变 ...
- UPC 2224 / “浪潮杯”山东省第四届ACM大学生程序设计竞赛 1008 Boring Counting 主席树
Problem H:Boring Counting Time Limit : 6000/3000ms (Java/Other) Memory Limit : 65535/32768K (Java/ ...
- nyoj-257 郁闷的C小加(一) 前缀表达式变后缀
郁闷的C小加(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说 ...