一个简单的Spring程序
Posted 你是那天边突然划过的一道闪电
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个简单的Spring程序相关的知识,希望对你有一定的参考价值。
首先,要用Eclipse开发Spring程序,需要安装Spring插件并重启Eclipse
具体操作:
新建java Project后,导入需要使用的包(提前下好Spring),Spring包包含6个部分的jar,我们只要导入用的即可
程序开发场景准备:
角色 -
执行动作的人群:proformer,接口
实现的类:Juggler,杂技师
发布指令的主类:Main
舞台准备:spring-idol.xml,配置bean,谁具备上场表现的机会
具体代码:
Proformer接口:
package com.springinaction.springidol; public interface Proformer { void perform() throws Exception; }
Juggler类:
package com.springinaction.springidol; public class Juggler implements Proformer { private int beanBags = 3; //默认杂技师可以抛3个豆子 public Juggler(){ } @Override public void perform() throws Exception { System.out.println("JUGGLING " + beanBags + " BEANBAGS"); } }
Main:
package com.springinaction.springidol; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext( "com/springinaction/springidol/spring-idol.xml"); Proformer performer = (Proformer)ctx.getBean("duke"); try { performer.perform(); } catch (Exception e) { e.printStackTrace(); } } }
spring-idol.xml:
<?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.xsd"> <bean id="duke" class="com.springinaction.springidol.Juggler" > </bean> </beans>
执行结果:JUGGLING 3 BEANBAGS
现在,组委会要搞比赛,杂技师一次抛3个豆子不能获得冠军,必须要能抛更多的豆子。我们修改下杂技师的能力(构造方法)
package com.springinaction.springidol;
public class Juggler implements Proformer {
private int beanBags = 3; //默认杂技师可以抛3个豆子
public Juggler(){
}
Juggler(int beanBags){ //增加一个构造方法,组委会提供多少豆子,杂技师都能玩得转
this.beanBags = beanBags;
}
@Override
public void perform() throws Exception {
System.out.println("JUGGLING " + beanBags + " BEANBAGS");
}
}
然后组委会可以提要求了,要求在舞台准备中做:
修改spring-idol.xml:
<?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.xsd">
<bean id="duke" class="com.springinaction.springidol.Juggler" >
<constructor-arg value="15"></constructor-arg> <!-- 组委会说,抛15个豆子看看吧 -->
</bean>
</beans>
跑Main表演:JUGGLING 15 BEANBAGS,瓦萨!
问题来了,组委会说,光抛个豆子有啥,你要能抛豆子的同时又能吟诗那冠军就是你的了!杂技师一想,这不是我该干的事啊,不行,得拉技能,找谁拉,肯定是诗人啊。
怎么拉呢?为了不破坏我杂技师本身的纯洁性,学女娲造个娃去拉技能吧:
增加一个Juggler子类poeticJuggler:
package com.springinaction.springidol; public class PoeticJuggler extends Juggler{ private Poem poem; PoeticJuggler(Poem poem){ this.poem = poem; } public PoeticJuggler(int beanBags, Poem poem) { //通过构造方法学技能 super(beanBags); this.poem = poem; } public void perform() throws Exception{ super.perform(); //爹地的杂技本事得保留 System.out.println("while recite..."); poem.recite(); //学了诗人的技能就用出来吧 } }
诗人还是隐身的,造吧
增加poem接口
package com.springinaction.springidol; public interface Poem { void recite(); }
增加诗人实现类:
package com.springinaction.springidol; public class Sonnet29 implements Poem {
//会朗诵莎士比亚的诗歌 private static String[] LINES = { "When in disgrace with fortune and men\'s eyes,", "I all alone beweep my outcast state,", "And trouble deaf heaven with my bootless cries,", "And look upon myself, and curse my fate,", "Wishing me like to one more rich in hope,", "Featured like him, like him with friends posses\'d,", "Desiring this man\'s art, and that man\'s scope,", "With what I most enjoy contented least;", "Yet in these thoughts myself almost despising,", "Haply I think on thee,-and then my state", "(Like to the lark at break of day arising From sullen earth) sings hymns at heaven\'s gate;", "For thy sweet love remember\'d , such wealth brings,", "That then I scorn to change my state with kings.!" }; Sonnet29(){ } @Override public void recite() { for(int i=0;i<LINES.length;i++) System.out.println(LINES[i]); } }
好啦,技能准备好,该用了,舞台准备,换人,杂技师儿子上
修改spring-idol.xml:
<?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.xsd"> <bean id="duke" class="com.springinaction.springidol.Juggler" > <constructor-arg value="15"></constructor-arg> </bean> <bean id="sonnect29" class="com.springinaction.springidol.Sonnet29"></bean> <bean id="poeticDuck" class="com.springinaction.springidol.PoeticJuggler"> <!-- 一个会朗诵的杂技师 --> <constructor-arg value="15"></constructor-arg> <constructor-arg ref="sonnect29"></constructor-arg> </bean> </beans>
换人表演了
package com.springinaction.springidol; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext( "com/springinaction/springidol/spring-idol.xml"); // Proformer performer = (Proformer)ctx.getBean("duke"); Proformer performer = (Proformer)ctx.getBean("poeticDuck"); //儿子替老子争光了 try { performer.perform(); } catch (Exception e) { e.printStackTrace(); } } }
结果:
JUGGLING 15 BEANBAGS
while recite...
When in disgrace with fortune and men\'s eyes,
I all alone beweep my outcast state,
And trouble deaf heaven with my bootless cries,
And look upon myself, and curse my fate,
Wishing me like to one more rich in hope,
Featured like him, like him with friends posses\'d,
Desiring this man\'s art, and that man\'s scope,
With what I most enjoy contented least;
Yet in these thoughts myself almost despising,
Haply I think on thee,-and then my state
(Like to the lark at break of day arising From sullen earth) sings hymns at heaven\'s gate;
For thy sweet love remember\'d , such wealth brings,
That then I scorn to change my state with kings.!
组委会满意了,不错不错,冠军是你的了!
以上是关于一个简单的Spring程序的主要内容,如果未能解决你的问题,请参考以下文章
初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段
初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段