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 ...
随机推荐
- MySQL数据的主从复制、半同步复制和主主复制详解-转
一.MySQL复制概述 ⑴.MySQL数据的复制的基本介绍 目前MySQL数据库已经占去数据库市场上很大的份额,其一是由于MySQL数据的开源性和高性能,当然还有重要的一条就是免费~不过不知道还能免费 ...
- keypress
onKeyUp="keypress2(this,140)" onblur="keypress2(this,140)"<span>你还可以输入140个 ...
- JavaScript自己模仿jQuery的一点小代码
function seter(sId) { var obj = document.getElementById(sId); return new function () { ...
- 关于 LimitedConcurrencyLevelTaskScheduler 的疑惑
1. LimitedConcurrencyLevelTaskScheduler 介绍 这个TaskScheduler用过的应该都知道,微软开源的一个任务调度器,它的代码很简单, 也很好懂,但是我没有明 ...
- SQL基本语句(2)
使用Insert语句插入新数据 语法:INSERT [INTO] tbl_name [(col_name,...)] VALUES (pression,...),… INSERT [INTO] tbl ...
- Android基础总结(10)——手机多媒体的运用:通知、短信、相机、视频播放
Android提供了一系列的API,是我们可以在程序中调用很多手机的多媒体资源,从而编写出更加丰富的应用程序. 1.通知的使用 通知(Notification)是Android中比较有特色的一个功能, ...
- leetcode279. Perfect Squares
learn from DP class Solution { public: int numSquares(int n) { if(n<=0)return 0; int * dp = new i ...
- procfs
https://www.kernel.org/doc/Documentation/filesystems/proc.txt /proc/stat cpu 493610 1050 955506 6140 ...
- WWF3入门<第一篇>
工作流是什么东西?暂时还不是很弄得清除. 工作流是用来解决什么问题的?暂时只是形成了一个很模糊的概念,还没办法用语言描述出来. 一.入门范例 以VS2008为例,先来创建一个WWF程序. 在工具箱中, ...
- jquery 相关class属性的操作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...