两个基础包
org.springframework.beans
org.springframework.context
applicationContext包含BeanFactory的所有功能
BeanFactory加载后,如果Bean的某一个属性没有注入,第一次调用getBean方法才会抛出异常;
而ApplicationContext则在初始化时,检查所依赖属性是否注入
定义,初始化,使用,销毁
实现org.springframework.beans.factory.InitializingBean接口,覆盖afterPropertiesSet方法
bean标签内init-method
实现org.springframework.beans.factory.DisposableBean接口,覆盖destroy方法
bean标签内destroy-method
xml文件
default-init-method
default-destroy-method
创建顺序
如果三者皆有的时候,它们之间的创建顺序:接口实现>标签内定义,同时全局默认方法没有它的事情
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");
}
}
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.");
}
}
package juedaiyuer;
public class BeanLifeCycle {
public void defautInit() {
System.out.println("Bean defautInit.");
}
public void defaultDestroy() {
System.out.println("Bean defaultDestroy.");
}
}
spring中提供了一些以Aware结尾的接口,实现了Aware接口的bean在被初始化之后,可以获取相应的资源
通过Aware接口,可以对spring相应资源进行操作(慎重)
为对spring进行简单的扩展
ApplicationContextAware
BeanNameAware
...
BeanNameAware可以获取bean的id;
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());
}
}
分类
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);
}
}
针对资源文件的统一接口
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
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中,可以直接写入文件名,而不是全类名