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 ...
随机推荐
- 【转】Bash脚本实现批量作业并行化
首先附上自己常用的代码 ---------------------------------------------------------------------------------------- ...
- xml规范及xml解析
http://www.cnblogs.com/wang-meng/p/5374498.html 1,XML基础介绍 xml的概念: XML 指可扩展标记语言(EXtensible Markup Lan ...
- java语言实现的短信接入实例,各公司大同小异
和几家短信平台接触过,都进行了接入测试.总体来说短信发送又快,覆盖率又全的,价格相对贵些.简易选两家分开使用,短信验证码的用一家贵的快的,普通的推广群发短信就用一个便宜的. 下面显示下测试代码 pub ...
- 【Unity Shaders】学习笔记——SurfaceShader(四)用纹理改善漫反射
[Unity Shaders]学习笔记——SurfaceShader(四)用纹理改善漫反射 转载请注明出处:http://www.cnblogs.com/-867259206/p/5603368.ht ...
- Indian Scientists Design Device to Collect Solar Energy 印度科学家设计太阳能收集设备
Indian scientists have designed a new device they hope will solve one of the biggest problems with ...
- gis 导出 dwg,shp
当我们在webgis 想要把某个地块或者多个地块导出dwg或者shp文件的时候怎么办?这个时候最好就是用后台的方式.首先把web gis上的graphic 的polygon提取为坐标的形式(类似于x, ...
- Windows Azure - App Services
1. 需要了解的概念:App Service Plan, Resource Group 2. Create an ASP.NET web app in Azure App Services 3. Cr ...
- Eclipse 安装反编译插件jadclipse
下载jadClipse地址: 链接: http://pan.baidu.com/s/1kTN4TPd 提取码: 3fvd 将net.sf.jadclipse_3.3.0.jar拷贝到eclipse的 ...
- 学习记录 java 链表知识
01.import java.util.HashMap; 02.import java.util.Scanner; 03.import java.util.Stack; 04. 05./** 06. ...
- bzoj1003 [ZJOI2006]物流运输
1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6300 Solved: 2597[Submit][Stat ...