Spring In Action (1) -- HelloWorld
来源:互联网 发布:知敬畏明底线 编辑:程序博客网 时间:2024/04/25 21:05
Spring版Hello World示例的第一个类是一个服务类,它的作用是打印出那句声名狼藉的问候。如下程序清单显示了GreetingService.java接口,它定义了我们的服务类需要提供的服务。GreetingServiceImpl.java是接口的实现。
- package com.springinaction.chapter01.hello;
- public interface GreetingService {
- public void sayGreeting();
- }
- package com.springinaction.chapter01.hello;
- public class GreetingServiceImpl implements GreetingService {
- private String greeting;
- public void sayGreeting() {
- System.out.println(this.greeting);
- }
- public GreetingServiceImpl(String greeting) {
- super();
- this.greeting = greeting;
- }
- public GreetingServiceImpl() {
- super();
- }
- public String getGreeting() {
- return greeting;
- }
- public void setGreeting(String greeting) {
- this.greeting = greeting;
- }
- }
Spring配置文件hello.xml告诉容器如何配置服务
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <bean id="greetingService" class="com.springinaction.chapter01.hello.GreetingServiceImpl">
- <property name="greeting">
- <value>I Love Aneya!</value>
- </property>
- </bean>
- </beans>
剩下的任务就是建立一个类来载入Spring容器并且利用它来获取服务:
- package com.springinaction.chapter01.hello;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.FileSystemXmlApplicationContext;
- public class HelloApp {
- /**
- * @param args
- */
- public static void main(String[] args) {
- ApplicationContext applicationContext = new FileSystemXmlApplicationContext("hello.xml");
- GreetingServiceImpl greetingServiceImpl = (GreetingServiceImpl)applicationContext.getBean("greetingService");
- greetingServiceImpl.sayGreeting();
- }
- }
最后,需要指出的是,为了让这段代码能够顺利执行,有两个包是必须引用的:spring.jar和commons-logging-1.1.1.jar
- Spring In Action (1) -- HelloWorld
- 《Spring In Action》学习笔记之HelloWorld
- Spring in action 学习之HelloWorld程序
- Spring-boot IN Action(1)
- Spring in Action 学习笔记(1)
- Spring In Action笔记(1)
- Spring in action 读书笔记
- Spring in action
- Spring in Action
- Spring in Action
- Spring in action笔记
- 读书笔记:spring in action
- Spring in Action问题
- Spring in Action(摘抄)
- Spring in action Third
- Spring in action 笔记
- spring in action
- Spring In Action
- linux firefox flash没有声音的解决办法及原因
- [分享]Lc.exe 已退出,代码 -1
- Fedora 9 安装OMNet++
- 求问delete和delete[] 的区别
- FCKeditor在线编辑器的使用
- Spring In Action (1) -- HelloWorld
- 衣服很好看
- 获得本月最后一天
- 今天开博客啦,博客定性
- ERP的管理思想
- 郁闷,怎么发的文没了?
- JDK源代码分析聚集篇-------Set分析(我们大家都是第一无二的)
- 马云经典语录
- JUnit 学习文档