spring学习笔记1——开始spring之旅
2009-07-04 21:38
417 查看
开始学spring之前编写一个简单的HelloWorld程序,对spring有个感性的理解。
写一个服务类,作用是打出那熟悉的问候,下面显示GreetingService接口,它定义了服务。
GreetingServiceImpl是GreetingService的实现:我们发现此类用两种方式初始化,一个是直接用GreetingServiceImpl(String greeting),另一类是用GreetingServiceImpl()然后调用setGreeting方法来初始化,这两种办法在spring里也可以以配置来实现。
在spring中配置HelloWorld,相当与:
GreetingServiceImpl greetingService = new GreetingServiceImpl();
greetingService.setGreeting("Buenos Dias!");
在spring中配置HelloWorld,相当与:
GreetingServiceImpl greetingService = new GreetingServiceImpl("Buenos Dias!");
BeanFactory是spring的容器,将把hello.xml文件加载之后main()方法调用BeanFactory的getBean()方法来得到问候服务的引用。
写一个服务类,作用是打出那熟悉的问候,下面显示GreetingService接口,它定义了服务。
public interface GreetingService { void sayGreeting(); } |
public class GreetingServiceImpl implements GreetingService { private String greeting; public GreetingServiceImpl(){} public GreetingServiceImpl(String greeting){ this.greeting = greeting; } public void sayGreeting() { System.out.println(greeting); } public void setGreeting(String greeting){ this.greeting = greeting; } } |
GreetingServiceImpl greetingService = new GreetingServiceImpl();
greetingService.setGreeting("Buenos Dias!");
<?xml version="1.0" encoding="UTF-8" ?> <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-2.0.xsd"> <bean id="greetingService" class="org.spring.helloworld.GreetingServiceImpl"> <property name="greeting" value="Buenos Dias!" /> </bean> </beans> |
GreetingServiceImpl greetingService = new GreetingServiceImpl("Buenos Dias!");
<bean id="greetingService" class="org.spring.helloworld.GreetingServiceImpl"> <constructor-arg value="Buenos Dias!" /> </bean> |
public class HelloApp { /** * 通过BeanFactory容器载入xml文件 */ public void BeanFactoryTest(){ BeanFactory factory = new XmlBeanFactory(new FileSystemResource("xmls/hello.xml")); GreetingService greetingService = (GreetingService)factory.getBean("greetingService"); greetingService.sayGreeting(); } /** * 通过ApplicationContext容器载入xml文件 */ public void ApplicationContextTest(){ ApplicationContext atx = new ClassPathXmlApplicationContext("org/spring/helloworld/hello.xml"); GreetingService greetingService = (GreetingService)atx.getBean("greetingService"); greetingService.sayGreeting(); } public static void main(String[] args){ HelloApp ha = new HelloApp(); ha.BeanFactoryTest(); ha.ApplicationContextTest(); } } |
相关文章推荐
- Spring源码学习笔记之一:从BeanPostProcesser开始
- Spring in Action 学习笔记—第一章 开始Spring之旅
- (原)spring学习笔记2.简单的开始
- Spring 学习笔记1——开始
- [原]spring学习笔记3.简单的开始2
- Spring in Action 学习笔记—开始Spring之旅
- Spring in Action 学习笔记—第一章开始Spring之旅
- Spring in Action 学习笔记—第一章 开始Spring之旅(转贴)
- Spring学习笔记——Sprin管理Bean的原理
- Java学习笔记37:Spring 使用 @ResponseBody 返回中文乱码问题解决
- springMVC学习笔记---day02 springMVC+spring+mybatis整合开发框架搭建
- SpringInAction 学习笔记:高级装配
- spring学习笔记13(注解@Autowired方式注入)
- spring学习笔记
- Spring MVC 学习笔记2 - 利用Spring Tool Suite创建一个web 项目
- Spring学习笔记之Spring MVC体系学习(一)
- Spring学习笔记 Spring gets started
- spring web flow学习笔记一
- Spring 学习笔记之 Bean 定义的继承
- Spring学习笔记:关于Interceptor