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. java枚举使用

    1.开发中如何使用枚举,一般在开发中使用消息提示.枚举可以继承,实现接口等.public enum Result { SUCCESS(1,"201 ok") { @Override ...

  2. window8.1中用户的管理员权限的提升方法

    1.使用命令netplwiz 2.点击确定后出现如下所示的内容,选择待修改的用户 3.然后点击属性,出现如图的内容 在上图中选中管理员左侧的单选按钮便可以了,将当前用户提升为管理员账户.

  3. 关于scrollview监听的一些方法

    一 package cn.testscrollview; import android.os.Bundle; import android.view.MotionEvent; import andro ...

  4. 6-10k招几个.NET开发工程师(工作地点:成都)

    目前工作的公司是一家做新加坡公司在成都的研发中心,目前有个项目组在做电子医疗记录(EMR)软件系统,在新加坡卖得还不错,由于以前版本的技术有障碍(主要采用WPF技术),目前老板决定投资用比较主流的技术 ...

  5. Eclipse插件开发之基础篇(4) OSGi框架

    转载出处:http://www.cnblogs.com/liuzhuo. 1. 什么是OSGi框架 OSGi(Open Service Gateway Initiative)框架是运行在JavaVM环 ...

  6. Android开发-API指南-<meta-data>

    <meta-data> 英文原文:http://developer.android.com/guide/topics/manifest/meta-data-element.html 采集( ...

  7. 学习总结 HTML简单应用

    <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  8. phonegap ios默认启动页

    phonegap创建的项目默认的启动界面是phonegap的图标,想去掉这个图标,有两个方法,第一就是将resourece下面的splash文件下面的图片改成自己想要的启动页面,名字要相同,替换掉它默 ...

  9. 下雪了-js下雪效果

    Jingle Bells,Jingle Bells,圣诞来临,做了一个下雪的小程序玩.有大雪花和小雪花. <!DOCTYPE html PUBliC "-//W3C//DTD XHTM ...

  10. AX 获得当前Grid的数据源的记录行数

    sysQuery::CountTotal();方法 eg: int lines = sysQuery::CountTotal( SalesTable_ds.QueryRun());