一、基于XML的Bean的配置
实现方式:1、设置注入;2、构造注入。
在 Spring 实例化 Bean 的过程中,首先会调用默认的构造方法实例化 Bean 对象,然后通过 Java 的反射机制调用 set()方法进行属性的注入。因此,设值注入要求一个 Bean 的对应类必须满足两个要求:一是提供一个无参构造方法;二是为需要注入的属性提供对应的 setter 方法。
使用设值注入时,在配置文件 applicationContext.xml 中需要使用 bean 元素的子元素 property 为每个属性注入值。
使用构造注入时,在配置文件中,需要使用 constructor-arg 标签定义构造方法的参数,可以使用其 value 属性设置该参数的值。
示例:以头歌平台上的实现Person类为例子。
1、创建Person类:
1)、定义name,sex属性。
2)、写出有参和无参构造方法。
3)、写入set(),get()方法。
4)、重写toString()方法。
package educoder;
public class Person {
// 属性值 name
private String name;
// 属性值 age
private String sex;
// 请在此处编写代码
/********* Begin *********/
public Person(){
}
public Person(String name,String sex){
this.name = name;
this.sex = sex;
}
public void setName(String name){
this.name = name;
}
public void setSex(String sex){
this.sex = sex;
}
public String getSex(){
return sex;
}
public String getName(){
return name;
}
/********* End *********/
// 重写 toString 方法
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
2、创建Spring配置文件
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<!-- 请在此处编写代码 -->
<!--********* Begin *********-->
<!-- 使用设值注入方式装配Person实例 -->
<bean id="person1" class="educoder.Person">
<property name="name" value="张三"/>
<property name="sex" value="男"/>
</bean>
<!-- 使用构造方法装配Person实例 -->
<bean id="person2" class="educoder.Person">
<constructor-arg index="0" value="李四"/>
<constructor-arg index="1" value="女"/>
</bean>
<!--********* End *********-->
</beans>
3、创建测试类
package educoder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
String path="educoder/applicationContext.xml";
// 初始化Spring容器,加载配置文件,并对bean进行实例化
ApplicationContext app = new ClassPathXmlApplicationContext(path);
// 设值方式输出结果
System.out.println(app.getBean("person1"));
// 构造方式输出结果
System.out.println(app.getBean("person2"));
}
}
二、基于注解的Bean装配
注解 | 说明 |
@Component | 该注解可以作用在任何层次(Dao 层、Controller 层等),它用来描述 Spring 中的 Bean,但它是一个泛化的概念,仅仅表示一个组件(Bean),使用时只需将该注解标注在相应类上 |
@Repository | 该注解通常作用在数据访问层(DAO 层),用来标识 Spring 中的 Bean,其功能与 @Component 相同 |
@Service | 该注解通常作用在业务层(Service 层),用于将业务层的类标识为 Spring 中的 Bean,其功能与 @Component 相同 |
@Controller | 该注解通常作用在控制层,用于将控制层的类标识为 Spring 中的 Bean,其功能与 @Component 相同 |
@Autowired | 用于对 Bean 的属性变量、属性的 Set 方法及构造函数进行标注,配合对应的注解处理器完成 Bean 的自动配置工作。默认按照 Bean 的类型进行装配 |
@Resource | 其作用与 Autowired 一样。其区别在于 @Autowired 默认按照 Bean 类型装配,而 @Resource 默认按照 Bean 实例名称进行装配,该注解有两个重要属性:name 和 type。Spring 将 name 属性解析为 Bean 实例名称,type 属性解析为 Bean 实例类型。如果指定 name 属性,则按实例名称进行装配;如果指定 type 属性,则按 Bean 类型进行装配。如果都不指定,则先按 Bean 实例名称装配,如果不能匹配,则再按照 Bean 类型进行装配;如果都无法匹配,则抛出 NoSuchBeanDefinitionException 异常 |
示例:以头歌平台上的实现Student类为例子
package com.educoder.springtest;
// 创建 StudentDao 接口
public interface StudentDao {
// 创建 show 方法
public void show();
}
package com.educoder.springtest;
// 创建 StudentDao 接口
public interface StudentService {
// 创建 show 方法
public void show();
}
1、创建 DAO 层接口的实现类
package com.educoder.springtest;
import org.springframework.stereotype.Repository;
// 请在此处编写代码
/********* Begin *********/
@Repository("studentDao")
/********* End *********/
public class StudentDaoImpl implements StudentDao{
// 重写 showTables 方法
@Override
public void showTables() {
System.out.println("执行Dao层的showTables()方法");
}
}
上述代码中,首先使用了 @Repository 注解将 StudentDaoImpl 类标识为 Spring 中的 Bean,其相当于在配置文件中添加
<bean id="studentDao"class="com.educoder.springtest.StudentDaoImpl"/>
然后重写了 show() 方法,并输出提示语,用于验证是否成功调用了该方法。
2、创建 Service 层接口的实现类
package com.educoder.springtest;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
// 请在此处编写代码
/********* Begin *********/
@Service("studentService")
/********* End *********/
public class StudentServiceImpl implements StudentService {
// 请在此处编写代码
/********* Begin *********/
@Resource(name = "studentDao")
/********* End *********/
private StudentDao studentDao;
public StudentDao getStudentDao() {
return studentDao;
}
public void setStudentDao(StudentDao studentDao){
this.studentDao=studentDao;
}
// 重写 toTime 方法
@Override
public void toTime() {
// 调用 studentDao 中的 showTables() 方法
studentDao.showTables();
System.out.println("执行Service层的toTime()方法");
}
}
上述代码中,首先使用了 @Service 注解将 StudentServiceImpl 类标识为 Spring 中的 Bean,其相当于在配置文件中添加
<bean id="studentService"class="com.educoder.springtest.StudentServiceImpl"/>
然后使用 @Resource 注解标注在属性 studentDao 上,这相当于在配置文件中添加
<property name="personDao"ref="personDao"/>
最后在该类的 show() 方法中调用 studentDao 中的 show() 方法,并输出提示语。
3、创建 Controller 层 StudentAction 类
package com.educoder.springtest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
// 请在此处编写代码
/********* Begin *********/
@Controller("studentAction")
/********* End *********/
public class StudentAction {
// 请在此处编写代码
/********* Begin *********/
@Resource(name = "studentService")
/********* End *********/
private StudentService studentService;
public StudentService getStudentService() {
return studentService;
}
public void setStudentService(StudentService studentService ){
this.studentService=studentService;
}
public void show() {
// 调用 personService 中的 toTime() 方法
studentService.toTime();
System.out.println("执行Action层的show()方法");
}
}
上述代码中,首先使用 @Controller 注解标注 StudentAction 类,其写法相当于在配置文件中添加
<bean id="studentAction"class="com.educoder.springtest.StudentAction"/>
然后使用了 @Resource 注解标注在 studentService 上,这相当于在配置文件内添加
<property name="studentService"ref="studentService"/>
最后在其 show() 方法中调用了 studentService 中的 show() 方法,并输出提示语。
三、自动装配
除了使用 XML 和注解的方式装配 Bean 以外,还有一种自动装配的方式。自动装配就是指 Spring 容器可以自动装配相互协作的 Bean 之间的关联关系,将一个 Bean 注入其他 Bean 的 Property 中。一般情况下,在实际的项目中很少使用自动装配功能,因为和自动装配功能所带来的好处比起来,明确清晰的配置文档更有说服力一些。
名称 | 说明 |
---|---|
byName | 根据 Property 的 name 自动装配,如果一个 Bean 的 name 和另一个 Bean 中的 Property 的 name 相同,则自动装配这个 Bean 到 Property 中。当一个 Bean 节点带有 autowire 的属性时,将查找其类中所有的 set 方法名,获得将 set 去掉并且首字母小写的字符串,然后去 spring 容器中寻找是否有此字符串名称 id 的对象。如果有,就取出注入;如果没有,就报空指针异常。 |
byType | 根据 Property 的数据类型(Type)自动装配,如果一个 Bean 的数据类型兼容另一个 Bean 中 Property 的数据类型,则自动装配。 |
constructor | 根据构造方法的参数的数据类型,进行 byType 模式的自动装配。 |
autodetect | 如果发现默认的构造方法,则用 constructor 模式,否则用 byType 模式。 |
no | 默认情况下,不使用自动装配,Bean 依赖必须通过 ref 元素定义。 |
示例:
首先上面student中的注解去掉,然后将 applicationContext.xml 配置文件修改成自动装配形式
StudentAction
package com.educoder.springtest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
// 请在此处修改代码
/********* Begin *********/
/********* End *********/
public class StudentAction {
// 请在此处修改代码
/********* Begin *********/
/********* End *********/
private StudentService studentService;
public StudentService getStudentService() {
return studentService;
}
public void setStudentService(StudentService studentService ){
this.studentService=studentService;
}
public void show() {
// 调用 personService 中的 show() 方法
studentService.toTime();
System.out.println("执行Action层的show()方法");
}
}
StudentServiceImpl
package com.educoder.springtest;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
// 请在此处修改代码
/********* Begin *********/
/********* End *********/
public class StudentServiceImpl implements StudentService {
// 请在此处修改代码
/********* Begin *********/
/********* End *********/
private StudentDao studentDao;
public StudentDao getStudentDao() {
return studentDao;
}
public void setStudentDao(StudentDao studentDao){
this.studentDao=studentDao;
}
// 重写 toTime 方法
@Override
public void toTime() {
// 调用 studentDao 中的 showTables()方法
studentDao.showTables();
System.out.println("执行Service层的toTime()方法");
}
}
StudentDaoImpl
package com.educoder.springtest;
import org.springframework.stereotype.Repository;
// 请在此处修改代码
/********* Begin *********/
/********* End *********/
public class StudentDaoImpl implements StudentDao{
// 重写 showTables 方法
@Override
public void showTables() {
System.out.println("执行Dao层的showTables()方法");
}
}
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 请在此处编写代码 -->
<!--********* Begin *********-->
<bean id="studentDao" class="com.educoder.springtest.StudentDaoImpl" />
<bean id="studentService" class="com.educoder.springtest.StudentServiceImpl" autowire="byName" />
<bean id="studentAction" class="com.educoder.springtest.StudentAction" autowire="byName" />
<!--********* End *********-->
</beans>