By : icess ,我的部落格 http://blog.matrix.org.cn/page/icess
在考试前匆匆看了一遍Spring in Action 也没有记录什么,考试结束了,要抓紧时间整理一下.要不然就忘了.^_^:
整理一下我目前可能会用到的模块, 对于那些现在根本用不到的冬冬还是等有时间再研究吧!
第一个当然是最经典的HelloWorld 了 , 呵呵,简单,但是说明了原理.
定义一个服务接口
package
test.helloworld;
public interface
GreetingService {
public void
sayGreeting();
}
下面是其实现:
package
test.helloworld;
public class
GreetingServiceImpl
implements
GreetingService {
private
String greeting;
public
GreetingServiceImpl() {}
public
GreetingServiceImpl(String greeting) {
this
.greeting = greeting;
}
public void
sayGreeting() {
// Auto-generated method stub
System.out.println(greeting);
}
public void
setGreeting(String greeting) {
this
.greeting = greeting;
}
}
然后就是测试 IoC 的测试代码:
package
test.helloworld;
import
org.springframework.beans.factory.BeanFactory;
import
org.springframework.beans.factory.xml.XmlBeanFactory;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import
org.springframework.core.io.FileSystemResource;
public class
HelloApp {
/**
*
@param
args
*/
public static void
main(String[] args) {
// TODO Auto-generated method stub
// BeanFactory factory;
// factory = new XmlBeanFactory(new FileSystemResource("src/test/helloworld/hello.xml"));
// 使用不同的方法得到bean. (BeanFactory or ApplicationContext)
ApplicationContext context =
new
ClassPathXmlApplicationContext(
"test/helloworld/hello.xml"
);
GreetingService greetingService = (GreetingService) context.getBean(
"greetingService"
);
// GreetingService greetingService = (GreetingService) factory.getBean("greetingService");
greetingService.sayGreeting();
}
}
还有重要的配置文件如下: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
=
"test.helloworld.GreetingServiceImpl"
>
<
property
name
=
"greeting"
>
<
value
>
ice rain !
</
value
>
</
property
>
</
bean
>
</
beans
>
呵呵就这么简单,实现了 greeting 属性的 Ioc.
这是Spring 两大基本支柱其一的工作原理, 关于AoP的内容,在这里不作讨论,因为现在我使用AoP的地方不是很多,简单的应用是很简单的啦.^_^.
下面一篇我们来看看 在spring包装下的jdbc访问. 详细情况也可以参考这里
分享到:
相关推荐
Spring in Action中文清晰版(带阅读笔记). Spring in Action中文清晰版(带阅读笔记).
Spring In Action中的笔记总结哦 Spring In Action中的笔记总结哦
Spring in Action中文清晰版(带阅读笔记)
Spring in Action 中文 清晰版 (带阅读笔记)(共压缩4分卷) 此第3分卷 是扫描版的 感谢分享的兄弟
Spring in action的学习笔记,里面有许多代码示例,部分内容是自己根据多方面资料综合而成
Spring in Action 中文 清晰版 (带阅读笔记)
Spring In Action(第二版)中文版_笔记 by uuwoxin 1.1 Spring是什么 如果说BeanFactory使Spring成为容器的话,那么上下文环境就是使Spring成为框架的原因。 上下文环境扩展了BeanFactory,添加了对I18N(国际化)...
Spring in Action 中间带有读书笔记, 第2部分共4部分
《Spring+in+action+中文版(第4版)》读书笔记带图片
Spring in Action 中间带有读书笔记 第4部分共4部分
Spring in Action 中间带有读书笔记,第3部分共4部分
spring_in_action_学习笔记 希望对各位学习spring的同学们 有帮助
Spring in Action 中文 清晰版 (带阅读笔记).part2
Spring in Action 中文 清晰版 (带阅读笔记).part1
本人阅读 《Spring in Action》一书笔记,点点滴滴,从博文中连缀成册,不多不少,共列下了 100 条。
spring in action 中文版,可能大家都知道了这本书吧,spring的入门书籍,介绍全面。我找了好久才好不容易找到一个是[完全]的中文版,不过是带有人为笔记的,版式上有点花,但总聊胜于无吧.当时是很高兴的.(一共34个包,...
Spring in Action 中间带有读书笔记, 第1部分共4部分
SpringInAction4 《Spring In Action 4th》学习笔记 第一部分 Spring的核心 1. Spring之旅 依赖注入 AOP bean的初始化过程 spring容器 2. 装配Bean “initialization on demand holder”创建单例模式的理解,参考 ...