Spring基础:Bean的装配
Posted nuist__NJUPT
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring基础:Bean的装配相关的知识,希望对你有一定的参考价值。
Spring基础:Bean的装配
- Bean的装配可以理解为将Bean依赖注入到Spring容器中
- Bean的装配方式即依赖注入方式
- 常用的Bean的装配方式有基于Xml的装配,基于注解的装配
- 最常用的是基于注解的装配
下面介绍基于Xml的装配方式
1-在eclipse中创建名为ch14的web应用,并导入相关jar包,如图所示。
2-在ch14的src目录下assemble包,并在该包下创建ComplexUser类,在该类中分别使用构造方法注入和使用属性setter方法注入。
import java.util.List;
import java.util.Map;
import java.util.Set;
public class ComplexUser {
private String uname ;
private List<String> hobbyList ;
private Map<String,String> residenceMap ;
private Set<String> aliasSet ;
private String [] array ;
//使用构造方法注入,需要提供带参数的构造方法
public ComplexUser(String uname, List hobbyList, Map residenceMap, Set aliasSet, String [] array) {
this.uname = uname ;
this.hobbyList = hobbyList ;
this.residenceMap = residenceMap ;
this.aliasSet = aliasSet ;
this.array = array ;
}
//使用属性的setter方法注入
public ComplexUser() {}
public void setUname(String uname) {
this.uname = uname ;
}
public void setHobbyList(List<String> hobbyList) {
this.hobbyList = hobbyList;
}
public void setResidenceMap(Map<String, String> residenceMap) {
this.residenceMap = residenceMap;
}
public void setAliasSet(Set<String> aliasSet) {
this.aliasSet = aliasSet;
}
public void setArray(String[] array) {
this.array = array;
}
public String toString() {
return "uname = " + uname + ";hobbyList = " + hobbyList + ";residenceMap = " + residenceMap + ";aliasSet = " + aliasSet + ";array = " + array ;
}
}
2-配置Bean,在Spring配置文件中使用实现类ComplexUser配置Bean的两个实例。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 使用构造方法方式装配注入ComplexUser实例user1 -->
<bean id = "user1" class = "assemble.ComplexUser">
<constructor-arg index = "0" value = "王国栋"/>
<constructor-arg index = "1" >
<list>
<value>唱歌</value>
<value>打球</value>
<value>爬山</value>
</list>
</constructor-arg>
<constructor-arg index = "2">
<map>
<entry key = "nanjing" value = "南京"/>
<entry key = "nanchang" value = "南昌"/>
<entry key = "shanghai" value = "上海"/>
</map>
</constructor-arg>
<constructor-arg>
<set>
<value>王国栋 101</value>
<value>王国栋 102</value>
<value>王国栋 103</value>
</set>
</constructor-arg>
<constructor-arg>
<array>
<value>努力</value>
<value>加油</value>
<value>坚强</value>
</array>
</constructor-arg>
</bean>
<!-- 使用属性的setter方法装配ComplexUser实例user2 -->
<bean id = "user2" class = "assemble.ComplexUser">
<property name = "uname" value = "王国栋2"/>
<property name="hobbyList">
<list>
<value>看书</value>
<value>学习</value>
<value>睡觉</value>
</list>
</property>
<property name ="residenceMap" >
<map>
<entry key = "beijing" value = "北京"/>
<entry key = "shanghai" value = "上海"/>
<entry key = "nanjing" value = "南京"/>
</map>
</property>
<property name = "aliasSet">
<set>
<value>王国栋 104</value>
<value>王国栋 105</value>
<value>王国栋 106</value>
</set>
</property>
<property name = "array">
<array>
<value>挫折</value>
<value>困难</value>
</array>
</property>
</bean>
</beans>
3-在ch3中创建test包,在该包中创建TestAssemble测试类。
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import assemble.ComplexUser;
public class TestAssemble {
public static void main(String[] args) {
//初始化Spring容器,架子啊配置文件
ApplicationContext appCon = new ClassPathXmlApplicationContext("applicationContext.xml") ;
//使用构造方法装配测试
ComplexUser u1 = (ComplexUser) appCon.getBean("user1") ;
System.out.println(u1);
//使用属性的setter方法装配测试
ComplexUser u2 = (ComplexUser) appCon.getBean("user2") ;
System.out.println(u2) ;
}
}
4-测试结果如下:
接下来,使用注解的方式装配Bean
1-导入所需的jar包,如下图所示。
2-在src目录下创建annotation.dao包,并在该包中创建TestDao接口和接口的实现方法TestDaoImpl方法
public interface TestDao {
public void save() ;
}
import org.springframework.stereotype.Repository;
@Repository("testDao")
public class TestDaoImpl implements TestDao{
@Override
public void save() {
System.out.println("testDao save") ;
}
}
3-在src文件下创建TestService接口和该接口的实现类TestServiceImpl类
public interface TestService {
public void save() ;
}
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import annotation.dao.TestDao;
@Service("testService")
public class TestServiceImpl implements TestService{
@Resource(name = "testDao")
private TestDao testDao ;
@Override
public void save() {
testDao.save();
System.out.println("testService save") ;
}
}
4-在src目录下创建annotation.controller包,并在该包中创建TestController实现类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import annotation.service.TestService;
@Controller
public class TestController {
@Autowired
private TestService testService ;
public void save() {
testService.save();
System.out.println("testController save") ;
}
}
5-在src目录下创建annotationContext.xml文件,在该文件中配置扫描包为annotation
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 使用context命名空间,通过Spring扫描包annotation及其子包下所有Bean的实现类,进行注释解析 -->
<context:component-scan base-package = "annotation"/>
</beans>
6-在test包中创建测试类TestMoreAnnotation
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import annotation.contoller.TestController;
public class TestMoreAnnotaion {
public static void main(String[] args) {
//初始化Spring容器,加载配置文件
ApplicationContext appCon = new ClassPathXmlApplicationContext("annotationContext.xml") ;
//实例化
TestController tc = (TestController)appCon.getBean("testController") ;
//调用对象的方法
tc.save();
}
}
7-测试结果如下:
以上是关于Spring基础:Bean的装配的主要内容,如果未能解决你的问题,请参考以下文章
Spring学习bean装配详解之 通过注解装配 Bean基础配置方式
Spring 框架基础(02):Bean的生命周期,作用域,装配总结