Spring入门Demo

Sprng入门Demo笔记:

人拿着斧子砍东西

 

package  com.gxd.authdemo.springdemo;

// 定义一个Person接口
public   interface  Person {    
    
public   void  useAxe() ;
    
}

 

package  com.gxd.authdemo.springdemo;

public   interface  Axe {
    
// 斧子有个砍的方法
     public   void  chop() ;

}

 

package  com.gxd.authdemo.springdemo;

public   class  Chinese  implements  Person  {

    
private Axe axe ;
    
public void useAxe() {
        System.out.println(
"中国人");
        axe.chop() ;

    }

    
    
//设值的注入方式
    public Axe getAxe() {
        
return axe;
    }

    
public void setAxe(Axe axe) {
        
this.axe = axe;
    }


}

 

package  com.gxd.authdemo.springdemo;

public   class  American  implements  Person  {
    
    
private Axe axe ;
    
    
public American() {}
    
//构造的注入方式
    public American(Axe axe) {
        
this.axe = axe ;
    }

    
public void useAxe() {
        System.out.println(
"美国人");
        axe.chop() ;
    }


}

 

package  com.gxd.authdemo.springdemo;

public   class  SteelAxe  implements  Axe {

    
public   void  chop() {
        System.out.println(
" 真是一把好钢斧 " );
    }

}

 

package  com.gxd.authdemo.springdemo;

public   class  StoneAxe  implements  Axe {

    
public   void  chop() {
        System.out.println(
" 原始社会用的斧子哈,用石头做的 " );
    }

}

 

package  com.gxd.authdemo.springdemo;

import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;

public   class  Test {

    
public   static   void  main(String[] args) {
        ApplicationContext ctx 
=   new  ClassPathXmlApplicationContext( " applicationContext.xml " ) ;
        
// Person p = (Person)ctx.getBean("chinese") ;
        Person p  =  (Person)ctx.getBean( " american " ) ;
        p.useAxe() ;
    }
}

 

<? 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 ="chinese"  class ="com.gxd.authdemo.springdemo.Chinese" >
        
< property  name ="axe" >
            
< ref  local ="stoneAxe" />
        
</ property >
    
</ bean >
    
< bean  id ="steelAxe"  class ="com.gxd.authdemo.springdemo.SteelAxe" ></ bean >
    
< bean  id ="stoneAxe"  class ="com.gxd.authdemo.springdemo.StoneAxe" ></ bean >
    
    
<!--  这下面是用构造注入的方式所写的配置文件  -->
    
< bean  id ="american"  class ="com.gxd.authdemo.springdemo.American" >
        
< constructor-arg >
            
< ref  local ="stoneAxe" />
        
</ constructor-arg >
    
</ bean >
</ beans >

 

 

你可能感兴趣的:(spring,bean,Class,import,interface,encoding)