Spring基于xml/Annotation(注解)/自动装配
发布日期:2021-04-30 21:05:08 浏览次数:105 分类:精选文章

本文共 3885 字,大约阅读时间需要 12 分钟。

Spring基于XML和Annotation装配Bean

一、Spring基于XML装配Bean

在Spring应用中,Bean的装配是指依赖关系的注入过程。Spring支持多种Bean装配方式,包括基于XML配置、基于Annotation注解以及自动装配等。本节将详细介绍基于XML和Annotation的Bean装配方式。

1.1 设值注入(Setter Injection)和构造注入(Constructor Injection)

Spring基于XML的Bean装配通常采用两种方式:设值注入和构造注入。设值注入通过反射机制调用Bean的setXxx()方法进行属性注入,而构造注入则通过调用构造函数注入所需参数。

1.1.1 设值注入的要求

  • 类必须提供一个默认的无参构造函数。
  • 类必须提供对应属性的setter方法。

1.1.2 构造注入的实现

在Spring配置文件中,使用<constructor-arg>标签定义构造函数的参数,并通过value属性或子元素指定参数值。

1.2 基于XML的Bean装配示例

通过以下案例展示如何在XML配置文件中配置Bean并进行装配:

张三
李四
王五

1.3 测试与验证

在测试类中,通过Spring的上下文获取Bean实例并验证其属性值:

public class TestMain {    @Test    public void testStudentBean() {        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("Bean.xml");        StudentBean studentBean = applicationContext.getBean("studentBean", StudentBean.class);        // 基本数据类型验证        System.out.println("intVal: " + studentBean.getIntVal());        System.out.println("doubleVal: " + studentBean.getDoubleVal());        System.out.println("charVal: " + studentBean.getCharVal());        System.out.println("booleanVal: " + studentBean.isBooleanVal());        // String验证        System.out.println("stringVal: " + studentBean.getStringVal());        // Date验证        System.out.println("dateVal: " + studentBean.getDateVal());        // 对象验证        ClassBean classBean = studentBean.getClassBeanVal();        System.out.println("ClassBean信息: " + classBean.getClasscode() + " - " + classBean.getClassname());        // 数组验证        System.out.println("String数组: ");        for (String str : studentBean.getStringArrayVal()) {            System.out.println(str);        }        // 其他类型验证(如List、Set、Map等)可按此类推进行验证    }}

二、Spring基于Annotation装配Bean

随着Java 5.0及后续版本的推出,Annotation技术逐渐成为Spring配置的主流方式。Spring3及以上版本支持多种Annotation,包括@Component@Repository@Service@Controller等,用于标注Spring管理Bean。

2.1 常用Annotation说明

2.1.1 @Component

  • 通用Annotation,用于标注Spring管理Bean,默认生成Bean名称(首字母小写)。
  • 可配合@Qualifier指定Bean名称。

2.1.2 @Repository

  • 标注数据访问层Bean,默认生成Bean名称(首字母小写)。

2.1.3 @Service

  • 标注业务逻辑层Bean,默认生成Bean名称(首字母小写)。

2.1.4 @Controller

  • 标注Spring MVC控制层Bean,默认生成Bean名称(首字母小写)。

2.1.5 @Autowired

  • 用于自动注入属性、setter方法和构造函数,根据Bean类型进行装配。

2.1.6 @Resource

  • 用于基于名称或类型的注入,支持J2EE标准Annotation。

2.1.7 @Qualifier

  • @Autowired配合使用,指定具体的Bean名称进行注入。

2.2 基于Annotation的Bean装配示例

以下是一个简单的Annotation装配示例:

// 1. 创建数据访问层实现类@Component("studentDao")public class StudentDaoImpl {    public void insertStudent() {        System.out.println("数据访问层添加方法");    }}// 2. 创建业务逻辑层实现类@Service("studentService")public class StudentServiceImpl {    @Resource    private StudentDaoImpl studentDao;    public void appendStudent() {        studentDao.insertStudent();    }}// 3. 创建控制层实现类@Controller("studentController")public class StudentController {    @Autowired    private StudentServiceImpl studentService;    public void addStudent() {        studentService.appendStudent();    }}

2.3 Spring配置文件(Application Context)


三、Spring自动装配Bean

除了基于XML和Annotation的装配方式,Spring还支持自动装配(Autowire)。自动装配是指容器自动协助Bean之间的依赖关系,完成相互注入。以下是自动装配的实现方式和示例:

3.1 自动装配的实现方式

属性 说明
byName 根据Bean的name属性进行装配。
byType 根据Bean的数据类型进行装配。
constructor 根据构造函数的参数类型进行装配。
autodetect 默认构造函数进行装配,否则使用byType模式。
no 不使用自动装配,需手动配置依赖。

3.2 自动装配的示例


通过以上内容,可以清晰地了解Spring基于XML和Annotation的Bean装配方式,以及自动装配的实现机制。这些内容将有助于您在实际项目中灵活选择合适的Bean装配方式。

上一篇:Leetcode--268. 缺失数字
下一篇:javaIO流应用——递归统计某目录下的所有文件名

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2026年06月08日 12时20分16秒