1、spring复习笔记spring:业务层框架,管理bean的,核心组件是容器。spring方式打印helloworld业务类:GreetingService.javapackage com.spring.demo.spring1;/* * author xuyh * date 2013-6-18 下午11:16:06 * version v1.0 * description 问候服务类 */public class GreetingService private String greeting; public String getGreeting() return greeting; publ
2、ic void setGreeting(String greeting) this.greeting = greeting; public void sayHello() System.out.println(hello: + greeting); maven配置文件: pom.xml 4.0.0 com.spring.demo spring1 0.0.1-SNAPSHOT jar spring1 http:/maven.apache.org UTF-8 junit junit 4.11 org.springframework spring-core 3.1.4.RELEASE org.spr
3、ingframework spring-beans 3.1.4.RELEASE org.springframework spring-aop 3.1.4.RELEASE org.springframework spring-context 3.1.4.RELEASE Spring 配置文件:ApplicationContext.xml 测试类 : GreetingServiceTestpackage com.spring.demo.spring1;import org.junit.Test;import org.springframework.beans.factory.BeanFactory
4、;import org.springframework.context.support.ClassPathXmlApplicationContext;public class GreetingServiceTest /* 传统方式打印helloworld */ Test public void testSayHello() GreetingService gs = new GreetingService(); gs.setGreeting(xuyh); gs.sayHello(); /* spring方式打印helloworld */ Test public void testSayHello
5、Spring() BeanFactory bf = new ClassPathXmlApplicationContext(ApplicationContext.xml); GreetingService gs = (GreetingService) bf.getBean(greetingService); gs.sayHello(); 更多的属性或对象package com.spring.demo.spring1;/* * author xuyh * date 2013-6-18 下午11:16:06 * version v1.0 * description 问候服务类 */public cl
6、ass GreetingService private String greeting; private String greeting2; private ByeService byeService; public String getGreeting() return greeting; public void setGreeting(String greeting) this.greeting = greeting; public void sayHello() System.out.println(hello: + greeting); public String getGreetin
7、g2() return greeting2; public void setGreeting2(String greeting2) this.greeting2 = greeting2; public void sayHello2() System.out.println(hello: + greeting2); public ByeService getByeService() return byeService; public void setByeService(ByeService byeService) this.byeService = byeService; public voi
8、d sayGreetingBye() byeService.sayBye(); package com.spring.demo.spring1;/* * author xuyh * date 2013-6-18 下午11:16:06 * version v1.0 * description 欢送服务类 */public class ByeService private String bye; public String getBye() return bye; public void setBye(String greeting) this.bye = greeting; public voi
9、d sayBye() System.out.println(bye: + bye); package com.spring.demo.spring1;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;public class GreetingServiceTest /* 传统方式打印hello
10、world */ Test public void testSayHello() GreetingService gs = new GreetingService(); gs.setGreeting(xuyh); gs.sayHello(); static BeanFactory bf = null; BeforeClass public static void before() /整个类只运行一次 bf = new ClassPathXmlApplicationContext(ApplicationContext.xml); /* spring方式打印helloworld */ Test p
11、ublic void testSayHelloSpring() GreetingService gs = (GreetingService) bf.getBean(greetingService); gs.sayHello(); gs.sayHello2(); /* spring方式打印第二个类ByeService */ Test public void testSayByeSpring() ByeService bs = (ByeService) bf.getBean(byeService); bs.sayBye(); /* spring类依赖 */ Test public void tes
12、tSayGreetingByeSpring() GreetingService gs = (GreetingService) bf.getBean(greetingService); gs.sayGreetingBye(); spring IOC1ioc:inverse of coutrol,控制反转,获得依赖对象的方式反转了。a.new 由spring完成b.set组装对象由spring完成。并且顺序相反了。即不必先初始化被依赖类。c.DI: dependency injection,依赖注入2. Bean工厂 BeanFactory:创建各种类型的Bean,功能单一,只能组装对象;Bean
13、工厂只把Bean的定义信息载进来,用的时候才去实例化。BeanFactory bf = new XmlBeanFactory(new FileInputStream(“bean.xml”);这样可以节省资源。当资源有限,如手机等,可以用BeanFactory ApplicationContext:应用上下文,除了BeanFactory的DI功能外,还有系统架构的服务。它也不是实例化所有的Bean,它只加载所有的单例Bean。使用bean元素的scope=”singleton”;与之相对的是scope=”prototype”即原型Bean;spring的单例与java设计模式单例是不一样的。Sp
14、ring单例是逻辑上约束的,java设计模式是物理约束的。Application:1、提供文本信息解析工具2、提供载入文件资源的通用方法,如图片3、可以向注册为监听器的Bean发送事件在很少情况下使用BeanFactory如在移动设备。它有三种经常使用的实现:1、ClassPathXmlApplicationContext 类路径中加载2、FileSystemXmlApplicationContext 系统文件3、XmlWebApplicationContext 在web环境中加载将bean从工厂中删掉有两种方法:1)若bean实现了DisposableBean接口,distroy()方法被调
15、用。2)在bean元素中配置 destroy-method= 另外,可以使用获得应用上下文的引用:public class GreetingService implements BeanNameAware,BeanFactoryAware,ApplicationContextAware . Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException System.out.println(setApplicationContext():+applic
16、ationContext); 生命周期图例:其实初始化与销毁有两种方法,一种是配置,一种是实现接口,推荐前一个,更为轻量。InitializingBean/init-methodDisposableBean/destroy-method两者不冲突,共存时接口为先。scope=”singleton|prototype|request|session|global session”前两者常用,后三者为web中的范围。生命周期bean被载入到容器时,他的生命周期就开始了。bean工厂在一个bean可以使用前完成很多工作:1)容器寻找bean的定义信息并实例化。2)使用依赖注入,spring按bean
17、定义信息配置bean的所有属性。3)若bean实现了BeanNameAware接口,工厂调用bean的setBeanName()方法传递bean的ID。4)若Bean实现了BeanFactoryAware接口,工厂调用setBeanFactory()方法传入工厂自身。5)若BeanPostProcessor和bean关联,则它们的postProcessBeforeInitialization()方法被语源。6)若bean指定了init-method方法,则它将被调用。7)最后,若有BeanPostProcessor和bean关联,则它们的postProcessAfterInitializati
18、on()方法被调用。解释:1)实例化,测试在空的构造函数中打印:new GreetingService()2)DI注入:测试在三个set方法中打印:DI: setGreeting():gaopanDI: setGreeting2():tomDI: setByeService():com.spring.demo.spring1.ByeService289d2e3)BeanNameAware:如果想获取bean的id的话:public class GreetingService implements BeanNameAware . Override public void setBeanName(
19、String name) System.out.println(BeanNameAware setBeanName():+name); BeanNameAware setBeanName():greetingService4)BeanFactoryAwar:Bean工厂关注,如果想获取BeanFactory的引用,当使用此接口public class GreetingService implements BeanFactoryAware . Override public void setBeanFactory(BeanFactory beanFactory) throws BeansExce
20、ption System.out.println(BeanFactoryAware setBeanFactory:+beanFactory); BeanFactoryAware setBeanFactory:org.springframework.beans.factory.support.DefaultListableBeanFactory401369: defining beans greetingService,byeService; root of factory hierarchy5)BeanPostProcessor:Bean后处理器,用于对Bean进行批处理。类似于web中的过滤
21、器Filter。/* * author xuyh * date 2013-6-20 下午11:21:39 * version v1.0 * description Bean的后处理器 */public class MyBeanPostProcessor implements BeanPostProcessor /* * 前置后处理器 */ Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException System.out.println(pos
22、tProcessBeforeInitialization():+beanName); return bean; /* * 后置后处理器 */ Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException System.out.println(postProcessAfterInitialization():+beanName); return bean; .postProcessBeforeInitialization():byeServicep
23、ostProcessAfterInitialization():byeService.postProcessBeforeInitialization():greetingServicepostProcessAfterInitialization():greetingService.可见后处理器对所有的Bean进行后处理。使用场景:对于集合的初始化6)调用bean的定制的初始化方法init-method如果bean配置了init-method方法,则会在后处理器的前后之间被调用。 public class GreetingService implements BeanNameAware,BeanFactoryAware . publi
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1