一个Spring入门小案例

创建两个类,配置类的bean对象。从容器取对象,调用方法。

Spring类

package com.southstar;

public class Spring {
    private String name;

    public void setBye(Bye bye) {
        this.bye = bye;
    }

    public Bye bye;

    public void setName(String name) {
        this.name = name;
    }
    public void sayHello(){
        System.out.println("hello:"+ name);
        bye.sayBye();
    }

Bye类

package com.southstar;

public class Bye {

    private String name;
    public void setName(String name) {
        this.name = name;
    }
    public void sayBye()
    {
        System.out.println("bye:"+name);
    }
}

Test.java

package com.test;

import com.southstar.Spring;

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


public class Test{

    public static void main(String[] arg)
    {
        ApplicationContext ac =new ClassPathXmlApplicationContext("Test.xml");
        Spring sp = (Spring)ac.getBean("spring");
        sp.sayHello();

    }
}

Test.xml







    没鱼

    



    
        
    

结果:

hello:没鱼
bye:weib

 

你可能感兴趣的:(Java框架)