Java 第五天 Spring IOC 配置文件
Spring xml结构定义文件:
http://www.springframework.org/schema/beans/spring-beans.xsd
可用xsd列表: http://www.springframework.org/schema/beans/
代码使用:
public void testAddUser() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] { "beans.xml" });
UserService us = (UserService) ctx.getBean("userService");
/*
* Operations
*/
}
构造函数注入:
1.自动判断类型
<beans>
<bean id="foo" class="x.y.Foo">
<constructor-arg ref="bar" />
<constructor-arg ref="baz" />
</bean>
<bean id="bar" class="x.y.Bar" />
<bean id="baz" class="x.y.Baz" />
</beans>
2.指定参数类型和值
<bean id="examplebean" class="examples.Examplebean">
<constructor-arg type="int" value="7500000" />
<constructor-arg type="java.lang.String" value="42" />
</bean>
3.指定参数名称和值 (需要1)Spring 3.0以上;2)调试模式下编译/在构造函数上指定@ConstructorProperties)
<bean id="examplebean" class="examples.Examplebean">
<constructor-arg name="years" value="7500000" />
<constructor-arg name="ultimateanswer" value="42" />
</bean>
4.指定参数顺序和值
<bean id="examplebean" class="examples.Examplebean">
<constructor-arg index="0" value="7500000" />
<constructor-arg index="1" value="42" />
</bean>
属性(setter)注入:
配置片断:
<bean name="uf" class="xpp.dao.fileimpl.UserDAOFileImpl" />
<bean id="UserService" class="xpp.service.UserService">
<property name="userDAO" ref="uf" />
</bean>
其目的是在运行时将UserDAOFileImpl的对象设置为UserService 类型中userDAO的属性成员。
引用对象用ref, 简单对象用value直接给定值。可以是属性,也可以是子结点。
集合注入:
<bean id="moreComplexObject" class="example.ComplexObject">
<!-- results in a setAdminEmails(java.util.Properties) call -->
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.org</prop>
<prop key="support">support@example.org</prop>
<prop key="development">development@example.org</prop>
</props>
</property>
<!-- results in a setSomeList(java.util.List) call -->
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource" />
</list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="someMap">
<map>
<entry key="an entry" value="just some string"/>
<entry key ="a ref" value-ref="myDataSource"/>
</map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource" />
</set>
</property>
</bean>
Scope:
bean的scope有singleton, prototype, request, session, global session。前2者较为常用,后3者用于web,较少用(struct2中有相关设置,可能用于Spring MVC)
singleton: 每次获取该bean均获得同一对象。
prototype: 每次获取该bean将创建新对象。
自动装配(Auto Wire):
较少使用,设置bean的autowire属性可使bean在获取时自动装配所需对象
byName: 通过查找其它bean的名称来自动装配(setter)
byType: 通过查找其它bean的类型来自动装配(setter)
constructor: 通过查找其它bean的类型来自动调用构造函数
default: 根据全局的默认设置
生命周期:
默认情况下,ApplicationContext初始化时实例化所有配置文件中的singleton的bean。在需要的情况下,个别bean需要延迟加载,此时可制定bean的属性lazy-init="true"
需要注意init-method 和 destroy-method 属性(bean初始化和销毁时的调用方法)不要和prototype的Scope一起使用,因为ClassPathXmlApplicationContext不会监控prototype的生存和销毁,在运行至其desotry()方法时不会触发destroy-method方法。
Java 第五天 Spring IOC 配置文件的更多相关文章
- Java反射机制在Spring IOC中的应用
反射的定义: 反射是java语言的一个特性,它允程序在运行时(注意不是编译的时候)来进行自我检查并且对内部的成员进行操作.例如它允许一个java的类获取它所有的成员变量和方法并且显示出来. 反射机制的 ...
- java web路径和spring读取配置文件
此篇博客缘起:部署java web系统到阿里云服务器(ubuntu14.04)的时候,有以下两个问题 找不到自定义的property配置文件 上传图片的时候找不到路径 开发的时候是在windows上的 ...
- Java中如何获取spring中配置文件.properties中属性值
通过spring配置properties文件 1 2 3 4 5 6 7 8 9 <bean id="propertyConfigurer" class="co ...
- spring-从普通java类取得注入spring Ioc容器的对象的方案
1.启动服务时通过spring容器的监听器(继承ContextLoaderListener 监听器的方法) public class ListenerSpringContext extends Con ...
- Spring IOC 概念及作用
一:程序之间的耦合及解决 耦合性(Coupling):也叫耦合度,是对模块间关联程度的度量.耦合的强弱取决于模块间接口的复杂性.调用模块的方式以及通过界面传送数据的多少.模块间的耦合度是指模块之间的依 ...
- Spring IOC 为什么能降低耦合
有同学在学习 Spring 框架中可能会问这样的问题,为什么通过依赖注入就可以降低代码间的耦合呢?我通过 new 生产对象不也可以吗,不就是一行代码的不同,一个是 @Resource 注入,一个是 n ...
- Spring IoC Container and Spring Bean Example Tutorial
Spring Framework is built on the Inversion of Control (IOC) principle. Dependency injection is the t ...
- Spring IOC初始化深度解析
1.前言 本文是基于JAVA配置方法对Spring IOC进行分析,掌握Spring IOC初始化流程对于我们更好的使用Spring.学习Spring还是很有帮助的,本文所使用的的Spring版本为5 ...
- [Java面试五]Spring总结以及在面试中的一些问题.
1.谈谈你对spring IOC和DI的理解,它们有什么区别? IoC Inverse of Control 反转控制的概念,就是将原本在程序中手动创建UserService对象的控制权,交由Spri ...
随机推荐
- getdata
public partial class GetData : System.Web.UI.Page { protected void Page_Load(object sender, EventArg ...
- struts2下实现的json传递list,object。
必须的jar: java bean: package upload.progress.action; public class music { private String name; private ...
- 医失眠灵验方--五味子50g 茯神50g 合欢花15g 法半夏15g
方药:五味子50g 茯神50g 合欢花15g 法半夏15g 水煎服 主治:失眠健忘 此方为已故名老中医李培生之验方,用于临床治疗失眠健忘症,疗效显著,其主药为五味子,滋阴和阳,敛阳 ...
- (Loadrunner)Abnormal termination, caused by mdrv process termination.(转)
Load generator跑了太多用户导致CPU和内存爆满,进程无法处理请求 确认自定义的代码是否释放内存 合理调整或增加思考时间 关闭extended log 尽量避免使用Load generat ...
- Flash图表控件FusionCharts如何在图表标绘非连续数据
你可能经常要以不完整的数据点绘制图表.例如,当绘制每月的销售图表时,你可能没有所有的月数据.所以,你可能只想以一个空白的区域来显示缺失的数据,不在这个区域中绘制任何东西.FusionCharts可以让 ...
- Eclipse 工作目录被破坏,导致Eclipse 打不开
由于之前一直使用的的是 visual studio 的开发工具,对 java 的 Eclipse 工具比较陌生,在使用 eclipse 的过程中误删了工作目录的部分文件,导致在在下次启动 eclips ...
- NSBundle 的使用
NSBundle 读取图片 plist text NSBundle *mainbundle=[NSBundle mainBundle]; //使mainBundle 对象获取图片的路径 NSStrin ...
- CLRS: online maximum (n,k)algorithm
//the first k elements interviewed and rejected, //for the latter n-k elements ,if value >max,re ...
- Windows Socket网络编程-2016.01.07
在使用WSAEventSelect的套接字模型中,遇到了WSAEventSelect返回10038的错误,在定位解决的过程中,简单记录一些定位解决的手段摘要. 使用windows的错误帮助信息,使用命 ...
- 【MySQL】触发器学习
MySQL手册中对触发器的定义是: 触发程序是与表有关的命名数据库对象,当表上出现特定事件时,将激活该对象.表必须是永久性表,不能将触发程序与临时表与视图关联起来. 相同触发程序动作时间和事件的给定表 ...