a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 406|回复: 0

[专业语言] Java认证辅导之关于Spring使用入门示例3

[复制链接]
发表于 2012-8-4 12:44:44 | 显示全部楼层 |阅读模式
Java认证辅导之关于Spring使用入门示例35 W+ G# c4 ~& p
3.5.添加引用/ B% ?& {& M  m# t1 A5 N+ a, |* O9 f9 c
org.springframework.core-3.0.2.RELEASE.jar
4 J8 e2 D* a1 s6 |. T2 [org.springframework.beans-3.0.2.RELEASE.jar
( y/ _0 R& M# \/ |com.springsource.org.apache.commons.logging-1.1.1.jar  {. f/ e' i. V6 H: g( Z5 `
3.6.hellospringworld.xml
, k8 |: ^: c3 _/ V% o1 V: Q) J代码% f" R. l+ v  S
《?xml version=“1.0” encoding=“UTF-8”?》* K5 Z' ~; x6 u% I
《!DOCTYPE beans PUBLIC “-//SPRING/DTD BEAN/EN” “http://www.springframework.org/dtd/spring-beans.dtd”》: Q1 t7 U: w0 b( j6 X+ O+ h
《beans》
$ F/ h- g5 c7 [; H《bean id=“LanguageBean” class=“example.PerlLanguage”》
2 ]* ~# p9 L$ U- A( t$ p《/bean》! K! v. e0 H4 F6 U$ C8 P
《bean id=“DeveloperBean” class=“example.Developer”》
& i5 _/ }) g6 _《property name=“language” ref=“LanguageBean”/》
9 v* O( t: f- `+ h6 [' D《/bean》6 ]% a4 B6 M2 H1 w3 ?
《/beans》
" D4 A/ @6 ^/ j. H( }* Y0 }3.7.Main.java
1 H( @8 b) y/ q  z代码6 R5 U* d! J5 F
package example;
2 {& N8 ^  r" k1 T4 U& vimport org.springframework.beans.factory.BeanFactory;
8 }: j, M) o% {3 pimport org.springframework.beans.factory.xml.XmlBeanFactory;
0 f2 Y0 ?$ h9 c* N& L# b2 o. z' zimport org.springframework.core.io.FileSystemResource;6 G. q8 ?; A/ o! i3 l( u4 J3 g
import org.springframework.core.io.Resource;. q% L) s4 i! W" d" q
public class Main {. ^  l! I' H+ ~
public static void main(String[] args) {3 M+ @+ u8 d* Y1 {5 o$ n" d
// TODO Auto-generated method stub
( b) v) N7 a) L3 F* P2 `Resource r = new FileSystemResource(“hellospringworld.xml”);
$ \- N" ?7 M) f8 y$ C4 d& K9 A" sBeanFactory f = new XmlBeanFactory(r);
0 M0 V# m' J0 ]* ?& W$ y1 FDeveloper d = (Developer) f.getBean(“DeveloperBean”);
! m, }# V# z8 M9 C& j2 JString s = d.getCurrentUsingLanguage();
7 X1 q! F( Y  f# s/ }' dSystem.out.println(“The developer is currently using: ”+s);: m, I5 Z4 D6 Z
}, r) X: S( A0 ]# @  H7 N; z
}, S" }, L6 a# b+ o- v* ~' t
运行此程序可以看到输出是:3 N4 n( b- r& v3 ?( L( a3 w
The developer is currently using: Perl2 y; N6 G. y( i
现在,假设有一天这个Developer不使用Perl而改用Java了,我们需要做什么?我们并不需要改程序,只需要修改xml文件就行了。
1 S. I/ Y7 E3 {& }《bean id=“LanguageBean” class=“example.PerlLanguage”》《/bean》, {; V* E4 ~7 e7 p1 S
改成
. O6 E: s  G, B8 g8 T0 ~《bean id=“LanguageBean” class=“example.JavaLanguage”》《/bean》# R, g. Y, A$ K' z7 z4 v: e
这就是我目前所理解的使用Spring的好处,即解耦,减少不同的类之间的耦合性。
  _) b) M, M! h/ l6 i" }( ^, F当然,现在的Spring已经日渐复杂跟强大,其中很多特性我并没有使用过,上面所说都是些粗浅的理解,等待以后使用的过程中有更加深入的认识。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-6-3 00:32 , Processed in 0.183064 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表