spring 的配置文件

什么鬼。。。。。。spring学习笔记整理

1.配置spring的头头文件


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
   
beans>

2.bean 的相关属性

id="exampleBean" class="examples.ExampleBean"/>



<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>


<beans default-lazy-init="true">
    
beans>

2.9. initialization method:初始化回调
实现org.springframework.beans.factory.InitializingBean接口允许容器在设置好bean的所有必要属性后,执行初始化事宜。InitializingBean接口仅指定了一个方法:


"exampleInitBean" class="examples.ExampleBean" init-method="init"/>

//相关的java代码
public class ExampleBean {
    public void init() {
        // do some initialization work
    }
}

2.10. destruction method:析构回调


"exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/>

//相关的java代码
public class ExampleBean {
    public void cleanup() {
        // do some destruction work (like releasing pooled connections)
    }
}

缺省的初始化和析构方法

public class DefaultBlogService implements BlogService {

    private BlogDao blogDao;

    public void setBlogDao(BlogDao blogDao) {
        this.blogDao = blogDao;
    }

    // this is (unsurprisingly) the initialization callback method
    public void init() {
        if (this.blogDao == null) {
            throw new IllegalStateException("The [blogDao] property must be set.");
        }
    }
}

default-init-method="init">
    "blogService" class="com.foo.DefaultBlogService">
        <property name="blogDao" ref="blogDao" />
    bean>

beans>

你可能感兴趣的:(spring学习笔记)