本实验的目的是学习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配置方式。实际应用中,为方便开发,一般采用注解方式实现。