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 配置文件的更多相关文章

  1. Java反射机制在Spring IOC中的应用

    反射的定义: 反射是java语言的一个特性,它允程序在运行时(注意不是编译的时候)来进行自我检查并且对内部的成员进行操作.例如它允许一个java的类获取它所有的成员变量和方法并且显示出来. 反射机制的 ...

  2. java web路径和spring读取配置文件

    此篇博客缘起:部署java web系统到阿里云服务器(ubuntu14.04)的时候,有以下两个问题 找不到自定义的property配置文件 上传图片的时候找不到路径 开发的时候是在windows上的 ...

  3. Java中如何获取spring中配置文件.properties中属性值

    通过spring配置properties文件 1 2 3 4 5 6 7 8 9 <bean id="propertyConfigurer"   class="co ...

  4. spring-从普通java类取得注入spring Ioc容器的对象的方案

    1.启动服务时通过spring容器的监听器(继承ContextLoaderListener 监听器的方法) public class ListenerSpringContext extends Con ...

  5. Spring IOC 概念及作用

    一:程序之间的耦合及解决 耦合性(Coupling):也叫耦合度,是对模块间关联程度的度量.耦合的强弱取决于模块间接口的复杂性.调用模块的方式以及通过界面传送数据的多少.模块间的耦合度是指模块之间的依 ...

  6. Spring IOC 为什么能降低耦合

    有同学在学习 Spring 框架中可能会问这样的问题,为什么通过依赖注入就可以降低代码间的耦合呢?我通过 new 生产对象不也可以吗,不就是一行代码的不同,一个是 @Resource 注入,一个是 n ...

  7. Spring IoC Container and Spring Bean Example Tutorial

    Spring Framework is built on the Inversion of Control (IOC) principle. Dependency injection is the t ...

  8. Spring IOC初始化深度解析

    1.前言 本文是基于JAVA配置方法对Spring IOC进行分析,掌握Spring IOC初始化流程对于我们更好的使用Spring.学习Spring还是很有帮助的,本文所使用的的Spring版本为5 ...

  9. [Java面试五]Spring总结以及在面试中的一些问题.

    1.谈谈你对spring IOC和DI的理解,它们有什么区别? IoC Inverse of Control 反转控制的概念,就是将原本在程序中手动创建UserService对象的控制权,交由Spri ...

随机推荐

  1. JQuery 插件FlexiGrid 之完全配置与使用

    博客分类: Java综合 jQuery配置管理jsonServlet数据结构  自己再做这个的时候.也是找了很多资料..但网上搜索了很多资料. 没有介绍的很全的. 鄙人就在此献丑一下. 来全面的介绍一 ...

  2. Redis附加功能之Redis流水线pipeline

    流水线功能的目的:通过减少客户端与服务器之间的通信次数来提高程序的执行效率. 一.通信 在一般情况下, 用户每执行一个 Redis 命令,客户端与服务器都需要进行一次通信:客户端会将命令请求发送给服务 ...

  3. ASP.NET MVC 之 View 测试

    项目又出问题了!手贱了一下,使用某个工具整理了一下 View 中的内容,不经意之间,将 View 的输出中大小写不小心搞错了,导致输出的内容没有办法正常解析. 这种问题太隐蔽了,下次再遇到怎么办呢? ...

  4. Java后台工程师面试杂记——不跳不涨工资星人跳槽经历

    经过接近一个月的时间,完成换工作这件“小事”,前后总计面试了多家公司,最后也没接到几个offer,不过最终总算尘埃落定,就对这个过程进行一个总结吧. 在某互联网公司工作了近一年的时间,但是频繁的业务需 ...

  5. JDK源码分析之集合03LinkedList

    一.前言 LinkedList是双向列表,实现方式是使用链表的方式保存元素:除了第一个和最后一个元素外,每一个节点都包含一个指向前一个和指向后一个节点的指针和元素的值.其特点是插入删除效率高,而随机访 ...

  6. Centos7 install Openstack - (第三节)添加镜像服务(Glance)

    Centos7 install Openstack - (第三节)添加镜像服务(Glance) 我的blog地址:http://www.cnblogs.com/caoguo 该文根据openstack ...

  7. 实验室中搭建Spark集群和PyCUDA开发环境

    1.安装CUDA 1.1安装前工作 1.1.1选取实验器材 实验中的每台计算机均装有双系统.选择其中一台计算机作为master节点,配置有GeForce GTX 650显卡,拥有384个CUDA核心. ...

  8. 获取oracle 里的表名与字段

    --数据库表名 SELECT distinct A.OBJECT_NAME as TAB_NAME,B.comments as DESCR FROM USER_OBJECTS A , USER_TAB ...

  9. ORA-12518,TNS:listener could not hand off client connection

    前几天在启动应用的时候,在控制台抛出了此异常信息!很明显是数据库方面的问题,不过具体是什么问题哪?百度了一下,网上关于此问题的信息还是有比较多,从异常的提示中我们也能看到是具体是和客户端的连接相关的问 ...

  10. java.lang.Exception: Socket bind failed: [730013] An attempt was made to acc

    在CMD命令行中启动运行startup.bat,启运程序总是闪退,查看日志发现如下错误: 26-Jan-2016 18:12:34.463 SEVERE [main] org.apache.coyot ...