spring的第一个例子

左手的ㄟ右手 2022-09-25 01:20 300阅读 0赞

java

Performer.java

  1. package com.springinaction.springidol;
  2. /**
  3. * 表演者抽象方法
  4. * @author WuJieJecket
  5. *
  6. */
  7. public interface Performer {
  8. public void perform();
  9. }

Juggler.java

  1. package com.springinaction.springidol;
  2. /**
  3. * idol:偶像
  4. * juggler:杂技师
  5. * @author WuJieJecket
  6. *
  7. */
  8. public class Juggler implements Performer {
  9. private int bearBags=3;
  10. //无参构造方法
  11. public Juggler(){
  12. }
  13. //传入球数
  14. public Juggler(int beanBags){
  15. this.bearBags=beanBags;
  16. }
  17. public void perform() {
  18. // TODO Auto-generated method stub
  19. System.out.println("JUGGLING(杂耍) "+bearBags+" BEANBAGS(豆袋子)");
  20. }
  21. }

配置文件

spring-idol.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  7. <bean id="duke" class="com.springinaction.springidol.Juggler"></bean>
  8. </beans>

文件结构

Center

测试方法

  1. /**
  2. * spring-idol.xml
  3. */
  4. @Test
  5. public void testJuggler(){
  6. //从类路径
  7. ApplicationContext ctx=new ClassPathXmlApplicationContext("com/springinaction/springidol/spring-idol.xml");
  8. Performer performer=(Performer) ctx.getBean("duke");
  9. performer.perform();
  10. }

发表评论

表情:
评论列表 (有 0 条评论,300人围观)

还没有评论,来说两句吧...

相关阅读

    相关 dubbo第一入门例子

    最近刚刚接触 Dubbo这个  框架,遇到了挺多的麻烦,网上搜索来的入门demo也是有挺多问题,百般周折终于弄出来了一个可以使用的小demo,与大家分享。 Zookeeper