Spring Bean創建及使用(三種方式xml配置、註解和Java配置方式)

本實驗的目的是學習Spring bean的配置方式,掌握XML配置、註解和Java配置方式裝載Bean的方法。
首先創建基礎類:
包路徑:com.helloworld
接口:Human
package com.helloworld;

/**

  • Human接口
    /
    public interface Human {
    public void sayHello(String name);
    }
    類:Chinese
    /
    *
  • Chinese 實現類
    */
    public class Chinese implements Human {
    public void sayHello(String name) {
    String helloWorld = “你好,” + name;
    System.out.println(helloWorld);
    }
    }
    類:English
    package com.helloworld;

import org.springframework.stereotype.Component;

/**

  • English 實現類
    */
    public class English implements Human {
    public void sayHello(String name) {
    String helloWorld = "Hello, " + name;
    System.out.println(helloWorld);
    }
    }
    1、 XML配置方式
    包路徑:com.inspur.helloworld.test1
    第一步:創建bean的XML配置文件 bean.xml
<?xml version="1.0" encoding="UTF-8"?>







第二步:創建HelloWorld類:

package com.helloworld.test1;

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

import com.helloworld.Human;

/**

  • HelloWorld 類
    */
    public class HelloWorld {

    //聲明ApplicationContext
    private static ApplicationContext context;

    public void dealWith(String type, String name){

     //獲取id爲type的bean
     Human human = (Human)context.getBean(type);
     //調用sayHello()
     human.sayHello(name);
    

    }

    public static void main(String[] args) {
    //通過ClassPathXmlApplicationContext方式加載bean.xml
    context = new ClassPathXmlApplicationContext(“com/helloworld/test1/bean.xml”);
    String type1 = “english”;
    String name1 = “zhangsan”;
    new HelloWorld().dealWith(type1, name1);
    String type2 = “chinese”;
    String name2 = “張三”;
    new HelloWorld().dealWith(type2, name2);
    }

}

第三步,執行HelloWorld類,執行結果如下:

在這裏插入圖片描述
2、註解方式
包路徑:com.inspur.helloworld.test2
第一步:創建Spring配置文件 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>


<context:component-scan base-package=“com.inspur.helloworld”></context:component-scan>

第二步,實現類增加註解@Component
Chinese類增加註解(紅色是新增的內容):

@Component(“chinese”)
public class Chinese implements Human {
public void sayHello(String name) {
String helloWorld = “你好,” + name;
System.out.println(helloWorld);
}
}

English類增加註解(紅色是新增的內容):

@Component(“english”)
public class English implements Human {
public void sayHello(String name) {
String helloWorld = "Hello, " + name;
System.out.println(helloWorld);
}
}

第三步:創建HelloWorld類
package com.helloworld.test2;

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

import com.helloworld.Human;

/**

  • HelloWorld 類
    */
    public class HelloWorld {

    //聲明ApplicationContext
    public static ApplicationContext context;

    static {
    //加載Spring配置文件applicationContext.xml
    context = new ClassPathXmlApplicationContext(“com/helloworld/test2/applicationContext.xml”);
    }
    public void dealWith(String type, String name){
    //獲取id爲type的bean
    Human human = (Human)context.getBean(type);
    //調用sayHello()
    human.sayHello(name);
    }

    public static void main(String[] args) {
    String type1 = “english”;
    String name1 = “zhangsan”;
    new HelloWorld().dealWith(type1, name1);
    String type2 = “chinese”;
    String name2 = “張三”;
    new HelloWorld().dealWith(type2, name2);
    }

}

第四步,執行HelloWorld類,執行結果如下:

在這裏插入圖片描述
3、 Java配置方式
包路徑:com.helloworld.test3
第一步:創建配置類AnoBean
package com.helloworld.test3;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.helloworld.Chinese;
import com.helloworld.English;

@Configuration
public class AnoBean {

@Bean(name="chinese")
public Chinese getChinese(){
	return new Chinese();
}

@Bean(name="english")
public English getEnglish(){
	return new English();
}

}
第二步,創建HelloWorld類
package com.helloworld.test3;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.helloworld.Human;

/**

  • HelloWorld 類
    */
    public class HelloWorld {

    //聲明ApplicationContext
    public static ApplicationContext context;

    static {
    //加載Spring配置文件applicationContext.xml
    context = new AnnotationConfigApplicationContext(AnoBean.class);
    }
    public void dealWith(String type, String name){
    //獲取id爲type的bean
    Human human = (Human)context.getBean(type);
    //調用sayHello()
    human.sayHello(name);
    }

    public static void main(String[] args) {
    String type1 = “english”;
    String name1 = “zhangsan”;
    new HelloWorld().dealWith(type1, name1);
    String type2 = “chinese”;
    String name2 = “張三”;
    new HelloWorld().dealWith(type2, name2);
    }

}

第三步,執行HelloWorld類,執行結果如下:

在這裏插入圖片描述

總結
本實驗學習Spring bean的配置方式,包含XML配置、註解和Java配置方式。實際應用中,爲方便開發,一般採用註解方式實現。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章