编号
|
名称
|
出处
|
作者
|
1
|
IoC 容器和Dependency Injection 模式
|
DependencyInjection.pdf
|
Martin Fowler
|
2
|
Spring开发指南
|
Spring开发指南.pdf
|
夏昕
|
3
|
Spring技术手册
|
《Spring技术手册》
|
林信良
|
4
|
Expert One-on-One J2EE Development Without EJB
|
《Expert One-on-One J2EE Development Without EJB》
|
Rod Johnson
Juergen Hoeller
|
5
|
文中示例项目源代码
|
http://zlsunnan.edudisk.cn
|
林信良
|
public interface
Action {
public
String execute(String str);
}
|
public class
UpperAction
implements
Action {
private
String message;
public
String getMessage() {
return
message;
}
public void
setMessage(String string) {
message = string;
}
public
String execute(String str) {
return
(getMessage() + str).toUpperCase();
}
}
|
public class
LowerAction
implements
Action {
private
String message;
public
String getMessage() {
return
message;
}
public void
setMessage(String string) {
message = string;
}
public
String execute(String str) {
return
(getMessage()+str).toLowerCase();
}
}
|
<
beans
>
<
description
>
Spring Quick Start
description>
<
bean
id
=
"TheAction"
class
=
"com.raykey.spring.qs.UpperAction"
>
<
property
name
=
"message"
>
<
value
>
HeLLo
value>
property>
bean>
beans>
|
public void
testQuickStart() {
ApplicationContext ctx=
new
FileSystemXmlApplicationContext(
"bean.xml"
);
Action action = (Action) ctx.getBean(
"TheAction"
);
System.out.println(action.execute(
"Rod Johnson"
));
}
|
class MovieLister...
public MovieLister(MovieFinder finder) {
this.finder = finder;
}
class ColonMovieFinder...
public ColonMovieFinder(String filename) {
this.filename = filename;
}
//
通过另一个类对其关系进行配置
private MutablePicoContainer configureContainer() {
MutablePicoContainer pico = new DefaultPicoContainer();
Parameter[] finderParams = {new ConstantParameter("movies1.txt")};
pico.registerComponentImplementation(MovieFinder.class,ColonMovieFinder.class, finderParams);
pico.registerComponentImplementation(MovieLister.class);
return pico;
}
|
public void testWithPico() {
MutablePicoContainer pico = configureContainer();
MovieLister lister = (MovieLister)
pico.getComponentInstance(MovieLister.class);
Movie[] movies = lister.moviesDirectedBy("Sergio Leone");
assertEquals("Once Upon a Time in the West",movies[0].getTitle());
}
|
class MovieLister...
private MovieFinder finder;
public void setFinder(MovieFinder finder) {
this.finder = finder;
}
class ColonMovieFinder...
public void setFilename(String filename) {
this.filename = filename;
}
//
通过配置文件将其关联起来
<beans></beans>
<bean class="spring.MovieLister" id="MovieLister"></bean>
<property name="finder"></property>
<ref local="MovieFinder"></ref>
<bean class="spring.ColonMovieFinder" id="MovieFinder"></bean>
<property name="filename"></property>
<value></value>movies1.txt
|
public void testWithSpring() throws Exception {
ApplicationContext ctx = new FileSystemXmlApplicationContext("spring.xml");
MovieLister lister = (MovieLister) ctx.getBean("MovieLister");
Movie[] movies = lister.moviesDirectedBy("Sergio Leone");
assertEquals("Once Upon a Time in the West",movies[0].getTitle());
}
|