• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
XPP的Blog
博客园    首页    新随笔    联系   管理    订阅  订阅

Java 第五天 Spring IOC 配置文件

简单学习了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方法。

posted @ 2013-12-26 03:26  Peter_xpp  阅读(280)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3