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"?>
<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.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"?>
<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")
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配置方式。实际应用中,为方便开发,一般采用注解方式实现。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值