springNote2 - juedaiyuer/researchNote GitHub Wiki

#spring笔记(二)---基于XML的Bean装配#

##Bean容器初始化##

两个基础包

org.springframework.beans
org.springframework.context
  1. BeanFactory提供配置结构和基本功能,加载并初始化Bean.
  2. ApplicationContext保存了Bean对象,可以在Spring中广泛使用.

applicationContext包含BeanFactory的所有功能
BeanFactory加载后,如果Bean的某一个属性没有注入,第一次调用getBean方法才会抛出异常;
而ApplicationContext则在初始化时,检查所依赖属性是否注入

##生命周期##

定义,初始化,使用,销毁

###初始化###

  1. 实现org.springframework.beans.factory.InitializingBean接口,覆盖afterPropertiesSet方法

  2. bean标签内init-method

###销毁###

  1. 实现org.springframework.beans.factory.DisposableBean接口,覆盖destroy方法

  2. bean标签内destroy-method

###配置全局默认初始化,销毁方法###

xml文件

default-init-method
default-destroy-method

创建顺序

如果三者皆有的时候,它们之间的创建顺序:接口实现>标签内定义,同时全局默认方法没有它的事情

##bean标签内定义初始化,销毁bean程序示例##

<bean id="beanLifeCycle" class="juedaiyuer.BeanLifeCycle"  init-method="start" destroy-method="stop"></bean>

package juedaiyuer;

public class BeanLifeCycle{


	public void start() {
		System.out.println("Bean start .");
	}

	public void stop() {
		System.out.println("Bean stop.");
	}

}

单元测试

package test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanLifecycle extends UnitTestBase {

	public TestBeanLifecycle() {
		super("classpath:spring-lifecycle.xml");
	}

	@Test
	public void test1() {
		super.getBean("beanLifeCycle");
	}

}	

##接口实现初始化,销毁bean程序##

package juedaiyuer;


import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;


public class BeanLifeCycle  implements InitializingBean, DisposableBean{

	@Override
	public void destroy() throws Exception {
		System.out.println("Bean destroy.");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("Bean afterPropertiesSet.");
	}

}

##默认初始化,销毁bean程序##

<default-init-method="defautInit" default-destroy-method="defaultDestroy"> 
   
<bean id="beanLifeCycle" class="juedaiyuer.BeanLifeCycle"  ></bean>



package juedaiyuer;

public class BeanLifeCycle {


	public void defautInit() {
		System.out.println("Bean defautInit.");
	}

	public void defaultDestroy() {
		System.out.println("Bean defaultDestroy.");
	}
}

##Aware接口##

spring中提供了一些以Aware结尾的接口,实现了Aware接口的bean在被初始化之后,可以获取相应的资源

通过Aware接口,可以对spring相应资源进行操作(慎重)

为对spring进行简单的扩展

ApplicationContextAware
BeanNameAware
...

BeanNameAware可以获取bean的id;

###ApplicationContextAware接口的一个简单程序###

<bean id="myApplicationContextAware" class="juedaiyuer.Aware.myApplicationContextAware" ></bean>




package test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;


package juedaiyuer.Aware;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class myApplicationContextAware implements ApplicationContextAware {

	private ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext = arg0;
		System.out.println("myApplicationContextAware:"+arg0.getBean("myApplicationContextAware").hashCode());
	}

	public void a()
	{
		//获取应用上下文的信息
	}

}



@RunWith(BlockJUnit4ClassRunner.class)
public class TestAware extends UnitTestBase {

	public TestAware() {
		super("classpath:spring-aware.xml");
	}

	@Test
	public void testmyApplicationContextAware() {
//		System.out.println("testMoocApplicationContext : " + super.getBean("moocApplicationContext").hashCode());
	}
	
}

##Bean的自动装配autowiring##

分类

  1. NO

  2. byname 根据属性名自动装配

  3. byType

  4. constructor

    package juedaiyuer.autowiring;

    public class AutoWiringDAO {

     public void say(String word) {
     	System.out.println("AutoWiringDAO : " + word);
     }
    

    }

    //Service类依赖于DAO //依赖成员需要设置set方法,用于byname,bytype //实例对象需要构造函数,用于constructor自动装配

    package juedaiyuer.autowiring;

    public class AutoWiringService {

     private AutoWiringDAO autoWiringDAO;
    
     public AutoWiringService(AutoWiringDAO autoWiringDAO) {
     	System.out.println("AutoWiringService");
     	this.autoWiringDAO = autoWiringDAO;
     }
    
     public void setAutoWiringDAO(AutoWiringDAO autoWiringDAO) {
     	System.out.println("setAutoWiringDAO");
     	this.autoWiringDAO = autoWiringDAO;
     }
    
     public void say(String word) {
     	this.autoWiringDAO.say(word);
     }
    

    }

    package test;

    import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner;

    import juedaiyuer.autowiring.AutoWiringService; import test.UnitTestBase;

    @RunWith(BlockJUnit4ClassRunner.class) public class TestAutoWiring extends UnitTestBase {

     public TestAutoWiring() {
     	super("classpath:spring-autowiring.xml");
     }
    
     @Test
     public void testSay() {
     	AutoWiringService service = super.getBean("autoWiringService");
     	service.say(" this is a test");
     }
    

    }

byname

<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.xsd" 
    default-autowire="byname">


<bean id="autoWiringService" class="juedaiyuer.autowiring.AutoWiringService" ></bean>
    
<bean id="autoWiringDAO" class="juedaiyuer.autowiring.AutoWiringDAO" ></bean>	

使用自动装配,可以减少下面的代码配置

<property name="autoWiringDAO" ref="autoWiringDAO"> </property>

##Resource##

针对资源文件的统一接口

UrlResource
ClassPathResource
FileSystemResource
ServletContextResource
InputStreamResource
ByteArrayResource

ResourceLoader

All application contexts implement the ResourceLoader interface,and therefore all application contexts may be used to obtain Resource instances

<bean  id="myResource" class="juedaiyuer.resource.myResource" ></bean>



package juedaiyuer.resource;

import java.io.IOException;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;


public class myResource implements ApplicationContextAware  {

	private ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}

	public void resource() throws IOException {
		Resource resource = applicationContext.getResource("config.txt");
		System.out.println(resource.getFilename());
		System.out.println(resource.contentLength());
	}

}


package test;

import java.io.IOException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import juedaiyuer.resource.myResource;
import test.UnitTestBase;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestResource extends UnitTestBase {

	public TestResource() {
		super("classpath:spring-resource.xml");
	}

	@Test
	public void testResource() {
		myResource rs = super.getBean("myResource");
		try {
			rs.resource();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

将一个目录配置到工程的Path中,可以直接写入文件名,而不是全类名


##source##

  1. evernote-Spring IOC容器中实例化bean
⚠️ **GitHub.com Fallback** ⚠️