spring复习笔记.docx
- 文档编号:3377493
- 上传时间:2022-11-22
- 格式:DOCX
- 页数:20
- 大小:67.79KB
spring复习笔记.docx
《spring复习笔记.docx》由会员分享,可在线阅读,更多相关《spring复习笔记.docx(20页珍藏版)》请在冰豆网上搜索。
spring复习笔记
spring:
业务层框架,管理bean的,核心组件是容器。
spring方式打印helloworld
业务类:
GreetingService.java
packagecom.spring.demo.spring1;
/**
*@authorxuyh
*@date2013-6-18下午11:
16:
06
*@versionv1.0
*@description问候服务类
*/
publicclassGreetingService{
privateStringgreeting;
publicStringgetGreeting(){
returngreeting;
}
publicvoidsetGreeting(Stringgreeting){
this.greeting=greeting;
}
publicvoidsayHello(){
System.out.println("hello:
"+greeting);
}
}
maven配置文件:
pom.xml
//maven.apache.org/POM/4.0.0"xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation="http: //maven.apache.org/POM/4.0.0http: //maven.apache.org/xsd/maven-4.0.0.xsd"> //maven.apache.org Spring配置文件: ApplicationContext.xml xmlversion="1.0"encoding="UTF-8"? > //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-3.1.xsd"> 测试类: GreetingServiceTest packagecom.spring.demo.spring1; importorg.junit.Test; importorg.springframework.beans.factory.BeanFactory; importorg.springframework.context.support.ClassPathXmlApplicationContext; publicclassGreetingServiceTest{ /*传统方式打印helloworld*/ @Test publicvoidtestSayHello(){ GreetingServicegs=newGreetingService(); gs.setGreeting("xuyh"); gs.sayHello(); } /*spring方式打印helloworld*/ @Test publicvoidtestSayHelloSpring(){ BeanFactorybf=newClassPathXmlApplicationContext("ApplicationContext.xml"); GreetingServicegs=(GreetingService)bf.getBean("greetingService"); gs.sayHello(); } } 更多的属性或对象 packagecom.spring.demo.spring1; /** *@authorxuyh *@date2013-6-18下午11: 16: 06 *@versionv1.0 *@description问候服务类 */ publicclassGreetingService{ privateStringgreeting; privateStringgreeting2; privateByeServicebyeService; publicStringgetGreeting(){ returngreeting; } publicvoidsetGreeting(Stringgreeting){ this.greeting=greeting; } publicvoidsayHello(){ System.out.println("hello: "+greeting); } publicStringgetGreeting2(){ returngreeting2; } publicvoidsetGreeting2(Stringgreeting2){ this.greeting2=greeting2; } publicvoidsayHello2(){ System.out.println("hello: "+greeting2); } publicByeServicegetByeService(){ returnbyeService; } publicvoidsetByeService(ByeServicebyeService){ this.byeService=byeService; } publicvoidsayGreetingBye(){ byeService.sayBye(); } } packagecom.spring.demo.spring1; /** *@authorxuyh *@date2013-6-18下午11: 16: 06 *@versionv1.0 *@description欢送服务类 */ publicclassByeService{ privateStringbye; publicStringgetBye(){ returnbye; } publicvoidsetBye(Stringgreeting){ this.bye=greeting; } publicvoidsayBye(){ System.out.println("bye: "+bye); } } xmlversion="1.0"encoding="UTF-8"? > //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-3.1.xsd"> packagecom.spring.demo.spring1; importorg.junit.BeforeClass; importorg.junit.Test; importorg.springframework.beans.factory.BeanFactory; importorg.springframework.context.support.ClassPathXmlApplicationContext; publicclassGreetingServiceTest{ /*传统方式打印helloworld*/ @Test publicvoidtestSayHello(){ GreetingServicegs=newGreetingService(); gs.setGreeting("xuyh"); gs.sayHello(); } staticBeanFactorybf=null; @BeforeClass publicstaticvoidbefore(){//整个类只运行一次 bf=newClassPathXmlApplicationContext("ApplicationContext.xml"); } /*spring方式打印helloworld*/ @Test publicvoidtestSayHelloSpring(){ GreetingServicegs=(GreetingService)bf.getBean("greetingService"); gs.sayHello(); gs.sayHello2(); } /*spring方式打印第二个类ByeService*/ @Test publicvoidtestSayByeSpring(){ ByeServicebs=(ByeService)bf.getBean("byeService"); bs.sayBye(); } /*spring类依赖*/ @Test publicvoidtestSayGreetingByeSpring(){ GreetingServicegs=(GreetingService)bf.getBean("greetingService"); gs.sayGreetingBye(); } } springIOC 1.ioc: inverseofcoutrol,控制反转,获得依赖对象的方式反转了。 a.new由spring完成 b.set组装对象由spring完成。 并且顺序相反了。 即不必先初始化被依赖类。 c.DI: dependencyinjection,依赖注入 2.Bean工厂BeanFactory: 创建各种类型的Bean,功能单一,只能组装对象;Bean工厂只把Bean的定义信息载进来,用的时候才去实例化。 BeanFactorybf=newXmlBeanFactory(newFileInputStream(“bean.xml”));这样可以节省资源。 当资源有限,如手机等,可以用BeanFactory ApplicationContext: 应用上下文,除了BeanFactory的DI功能外,还有系统架构的服务。 它也不是实例化所有的Bean,它只加载所有的单例Bean。 使用bean元素的scope=”singleton”; 与之相对的是scope=”prototype”即原型Bean;spring的单例与java设计模式单例是不一样的。 Spring单例是逻辑上约束的,java设计模式是物理约束的。 Application: 1、提供文本信息解析工具 2、提供载入文件资源的通用方法,如图片 3、可以向注册为监听器的Bean发送事件 在很少情况下使用BeanFactory如在移动设备。 它有三种经常使用的实现: 1、ClassPathXmlApplicationContext类路径中加载 2、FileSystemXmlApplicationContext系统文件 3、XmlWebApplicationContext在web环境中加载 将bean从工厂中删掉有两种方法: 1)若bean实现了DisposableBean接口,distroy()方法被调用。 2)在bean元素中配置destroy-method= 另外,可以使用获得应用上下文的引用: publicclassGreetingServiceimplementsBeanNameAware,BeanFactoryAware,ApplicationContextAware{ ... @Override publicvoidsetApplicationContext(ApplicationContextapplicationContext)throwsBeansException{ System.out.println("setApplicationContext(): "+applicationContext); } 生命周期图例: 其实初始化与销毁有两种方法,一种是配置,一种是实现接口,推荐前一个,更为轻量。 InitializingBean/init-method DisposableBean/destroy-method 两者不冲突,共存时接口为先。 scope=”singleton|prototype|request|session|globalsession” 前两者常用,后三者为web中的范围。 生命周期 bean被载入到容器时,他的生命周期就开始了。 bean工厂在一个bean可以使用前完成很多工作: 1)容器寻找bean的定义信息并实例化。 2)使用依赖注入,spring按bean定义信息配置bean的所有属性。 3)若bean实现了BeanNameAware接口,工厂调用bean的setBeanName()方法传递bean的ID。 4)若Bean实现了BeanFactoryAware接口,工厂调用setBeanFactory()方法传入工厂自身。 5)若BeanPostProcessor和bean关联,则它们的postProcessBeforeInitialization()方法被语源。 6)若bean指定了init-method方法,则它将被调用。 7)最后,若有BeanPostProcessor和bean关联,则它们的postProcessAfterInitialization()方法被调用。 解释: 1)实例化,测试在空的构造函数中打印: newGreetingService() 2)DI注入: 测试在三个set方法中打印: DI: setGreeting(): gaopan DI: setGreeting2(): tom DI: setByeService(): com.spring.demo.spring1.ByeService@289d2e 3)BeanNameAware: 如果想获取bean的id的话: publicclassGreetingServiceimplementsBeanNameAware{ ... @Override publicvoidsetBeanName(Stringname){ System.out.println("BeanNameAwaresetBeanName(): "+name); } } BeanNameAwaresetBeanName(): greetingService 4)BeanFactoryAwar: Bean工厂关注,如果想获取BeanFactory的引用,当使用此接口 publicclassGreetingServiceimplementsBeanFactoryAware{ ... @Override publicvoidsetBeanFactory(BeanFactorybeanFactory)throwsBeansException{ System.out.println("BeanFactoryAwaresetBeanFactory: "+beanFactory); } } BeanFactoryAwaresetBeanFactory: org.springframework.beans.factory.support.DefaultListableBeanFactory@401369: definingbeans[greetingService,byeService];rootoffactoryhierarchy 5)BeanPostProcessor: Bean后处理器,用于对Bean进行批处理。 类似于web中的过滤器Filter。 /** *@authorxuyh *@date2013-6-20下午11: 21: 39 *@versionv1.0 *@descriptionBean的后处理器 */ publicclassMyBeanPostProcessorimplementsBeanPostProcessor{ /** *前置后处理器 */ @Override publicObjectpostProcessBeforeInitialization(Objectbean,StringbeanName)throwsBeansException{ System.out.println("postProcessBeforeInitialization(): "+beanName); returnbean; } /** *后置后处理器 */ @Override publicObjectpostProcessAfterInitialization(Objectbean,StringbeanName)throwsBeansException{ System.out.println("postProcessAfterInitialization(): "+beanName); returnbean; } } --后处理器配置,只需配置一个普通的Bean即可--> ... postProcessBeforeInitialization(): byeService postProcessAfterInitialization(): byeService ... postProcessBeforeInitialization(): greetingService postProcessAfterInitialization(): greetingService ... 可见后处理器对所有的Bean进行后处理。 使用场景: 对于集合的初始化…… 6)调用bean的定制的初始化方法init-method 如果bean配置了init-method方法,则会在后处理器的前后之间被调用。 publicclassGreetingServiceimplementsBeanNameAware,BeanFactoryAware{ ... publi
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- spring 复习 笔记
![提示](https://static.bdocx.com/images/bang_tan.gif)