《spring复习笔记.docx》由会员分享,可在线阅读,更多相关《spring复习笔记.docx(16页珍藏版)》请在金锄头文库上搜索。
1、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;public void setG
2、reeting(String greeting) this.greeting = greeting;public void sayHello() System.out.println(hello: + greeting);maven配置文件: pom.xml4.0.0com.spring.demospring10.0.1-SNAPSHOTjarspring1http:/maven.apache.orgUTF-8junitjunit4.11org.springframeworkspring-core3.1.4.RELEASEorg.springframeworkspring-beans3.1.4
3、.RELEASEorg.springframeworkspring-aop3.1.4.RELEASEorg.springframeworkspring-context3.1.4.RELEASESpring 配置文件:ApplicationContext.xml测试类 : GreetingServiceTestpackage com.spring.demo.spring1;import org.junit.Test;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.sup
4、port.ClassPathXmlApplicationContext;public class GreetingServiceTest /* 传统方式打印helloworld */Testpublic void testSayHello() GreetingService gs = new GreetingService();gs.setGreeting(xuyh);gs.sayHello();/* spring方式打印helloworld */Testpublic void testSayHelloSpring() BeanFactory bf = new ClassPathXmlAppl
5、icationContext(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 class GreetingService private String greeting;priva
6、te 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 getGreeting2() return greeting2;public void setGreeting2(String g
7、reeting2) 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 void 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 void sayBye() System.out.println(bye: + bye);