Spring Bean创建及使用

本实验是基于Spring Bean方式进行开发,实现XML配置、注解和Java配置方式加载Bean,因此,本实验包含3个独立的小实验,分别介绍XML配置、注解和Java配置方式装载Bean。

本实验的目的是学习Spring bean的配置方式,掌握XML配置、注解和Java配置方式装载Bean的方法。

创建基础类:

       包路径:com.inspur.helloworld

       接口:Human

package com.inspur.helloworld;

 

/**

 * Human接口

 */

publicinterface Human {

    publicvoid sayHello(String name);

}

       类:Chinese

/**

 * Chinese 实现类

 */

publicclass Chinese implements Human {

    publicvoid sayHello(String name) {

       String helloWorld = "你好," + name;

       System.out.println(helloWorld);

    }

}

类:English

package com.inspur.helloworld;

 

import org.springframework.stereotype.Component;

 

/**

 * English 实现类

 */

publicclass English implements Human {

    publicvoid sayHello(String name) {

       String helloWorld = "Hello, " + name;

       System.out.println(helloWorld);

    }

}

XML配置方式

包路径:com.inspur.helloworld.test1

第一步:创建bean的XML配置文件 bean.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"

    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

                      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

                     http://www.springframework.org/schema/context

                      http://www.springframework.org/schema/context/spring-context-3.2.xsd"

    >

    <bean id="chinese" class="com.inspur.helloworld.Chinese">

    </bean>

    <bean id="english" class="com.inspur.helloworld.English">

    </bean>

</beans>

第二步:创建HelloWorld类:

package com.inspur.helloworld.test1;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import com.inspur.helloworld.Human;

 

/**

 * HelloWorld

 */

publicclass HelloWorld {

   

    //声明ApplicationContext

    privatestatic ApplicationContext context;

 

    publicvoid dealWith(String type, String name){

      

       //获取idtypebean

       Human human = (Human)context.getBean(type);

       //调用sayHello()

       human.sayHello(name);

    }

   

    publicstaticvoid main(String[] args) {

       //通过ClassPathXmlApplicationContext方式加载bean.xml

       context = new ClassPathXmlApplicationContext("com/inspur/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类

包路径:com.inspur.helloworld.test2

第一步:创建Spring配置文件 applicationContext.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"

    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

                      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

                     http://www.springframework.org/schema/context

                      http://www.springframework.org/schema/context/spring-context-3.2.xsd"

    >

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

   

</beans>


       第二步,实现类增加注解@Component

       Chinese类增加注解(红色是新增的内容):

@Component("chinese")

publicclass Chinese implements Human {

    publicvoid sayHello(String name) {

       String helloWorld = "你好," + name;

       System.out.println(helloWorld);

    }

}


       English类增加注解(红色是新增的内容):

@Component("english")

publicclass English implements Human {

    publicvoid sayHello(String name) {

       String helloWorld = "Hello, " + name;

       System.out.println(helloWorld);

    }

}


第三步:创建HelloWorld类

package com.inspur.helloworld.test2;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import com.inspur.helloworld.Human;

 

/**

 * HelloWorld

 */

publicclass HelloWorld {

   

    //声明ApplicationContext

    publicstatic ApplicationContext context;

 

    static {

       //加载Spring配置文件applicationContext.xml

       context = new ClassPathXmlApplicationContext("com/inspur/helloworld/test2/applicationContext.xml");

    }

    publicvoid dealWith(String type, String name){

       //获取idtypebean

       Human human = (Human)context.getBean(type);

       //调用sayHello()

       human.sayHello(name);

    }

   

    publicstaticvoid 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类,执行结果如下:

java配置方式

包路径:com.inspur.helloworld.test3

第一步:创建配置类AnoBean

package com.inspur.helloworld.test3;

 

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

 

import com.inspur.helloworld.Chinese;

import com.inspur.helloworld.English;

 

@Configuration

publicclass AnoBean {

   

    @Bean(name="chinese")

    public Chinese getChinese(){

       returnnew Chinese();

    }

   

    @Bean(name="english")

    public English getEnglish(){

       returnnew English();

    }

   

}

       第二步,创建HelloWorld类

package com.inspur.helloworld.test3;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

 

import com.inspur.helloworld.Human;

 

/**

 * HelloWorld

 */

publicclass HelloWorld {

   

    //声明ApplicationContext

    publicstatic ApplicationContext context;

 

    static {

       //加载Spring配置文件applicationContext.xml

       context = new AnnotationConfigApplicationContext(AnoBean.class);

    }

    publicvoid dealWith(String type, String name){

       //获取idtypebean

       Human human = (Human)context.getBean(type);

       //调用sayHello()

       human.sayHello(name);

    }

   

    publicstaticvoid 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类

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值