Spring之路(12)--在注解配置中装配bean

简介: 本文目录1. 背景2. 按名称自动装配2.1 定义歌手类,并通过@component注解为其生成对应bean。2.2 定义舞台类,并指定名称装配其属性2.3 配置xml文件,开启对bean的扫描2.4 测试3. 按类型自动装配3.1 定义歌手类和舞者类3.2 通过类型自动装配舞台bean3.3 测试运行4. 总结

1. 背景

上一篇讲了xml配置中如何装配bean,其实注解配置中装配bean的原理与xml一模一样,而且达到的效果也是相同的,不过是采用了不同的方式而已。


所以本篇我们也并不更换实例,还是以在舞台中注入歌手、舞者为例,进行装配。由于注解情况下,都是直接在类、属性上添加注解,没有必要显示指定bean的包路径+类名,所以都是自动装配。


2. 按名称自动装配

我们对bean进行命名,同时在装配时指定装配的bean名称。


2.1 定义歌手类,并通过@component注解为其生成对应bean。

package org.maoge.annotationassemble;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

@Component("liujia") // 通过注解指定Dancer类生成的bean名称为liujia

public class Dancer {

@Value("刘迦") // 通过注解注入姓名

private String name;

public String getName() {

 return name;

}

public void setName(String name) {

 this.name = name;

}

}


package org.maoge.annotationassemble;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

@Component("daolang") // 通过注解指定Singer类生成的bean名称为daolang

public class Singer {

@Value("刀郎") // 通过注解注入姓名

private String name;


public String getName() {

 return name;

}

public void setName(String name) {

 this.name = name;

}

}


此处再次解释下@Component("xxx")注解的作用,Spring容器在启动时,会扫描该注解标注的类,并生成一个命名为xxx的bean对象放入容器中。如果使用注解@Component标注,则生成bean的命名为类的首字母转小写。


2.2 定义舞台类,并指定名称装配其属性

可以通过@Autowired使bean通过指定规则注入进来,同时使用@Qualifier("xxx")装配命名为xxx的bean,代码如下:


package org.maoge.annotationassemble;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

//舞台类,注意舞台也要生成一个bean

@Component("stage")

public class Stage {

@Autowired//表示自动注入

@Qualifier("daolang")//指定要装配名称为daolang的bean

private Singer singer;

@Autowired

@Qualifier("liujia")//指定要装配名称为liujia的bean

private Dancer dancer;

public Singer getSinger() {

 return singer;

}

public void setSinger(Singer singer) {

 this.singer = singer;

}

public Dancer getDancer() {

 return dancer;

}

public void setDancer(Dancer dancer) {

 this.dancer = dancer;

}

}


2.3 配置xml文件,开启对bean的扫描

不要忘记使用注解配置bean时,还是需要一个xml文件,其中要开启对bean所在包的扫描,所以spring.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.xsd

      http://www.springframework.org/schema/context

      http://www.springframework.org/schema/context/spring-context-4.0.xsd">

  <context:component-scan

   base-package="org.maoge.annotaionassemble" />

</beans>


2.4 测试

启动主类,从容器中获取舞台,会发现舞台中的歌手和舞者均已装配成功。


package org.maoge.annotationassemble;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {

 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(

   "/org/maoge/annotationassemble/spring.xml");

 Stage stage = (Stage) context.getBean("stage");

 System.out.println(stage.getSinger().getName());

 System.out.println(stage.getDancer().getName());

}

}


输出结果:


刀郎

刘迦


3. 按类型自动装配

这个就更加简单了啊,因为使用注解时,一般一个类就生成一个bean(一个注解加到一个类上,生成一个对应bean)。


所以按类型匹配基本上很稳啊,没啥问题,也非常流程。目前这种方式应该是非常非常流行的,不论是SSM项目还是SpringBoot项目,这种方式都使用的非常多。


看例子:


3.1 定义歌手类和舞者类

package org.maoge.annotationassemble;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

@Component // 未指定命名,所以默认命名应为singer

public class Singer {

@Value("刀郎") // 通过注解注入姓名

private String name;

public String getName() {

 return name;

}

public void setName(String name) {

 this.name = name;

}

}


package org.maoge.annotationassemble;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

@Component // 未指定命名,所以默认命名应为dancer

public class Dancer {

@Value("刘迦") // 通过注解注入姓名

private String name;

public String getName() {

 return name;

}

public void setName(String name) {

 this.name = name;

}

}


3.2 通过类型自动装配舞台bean

不用加@Qualifier,默认就是自动按类型装配,因为太常用了。


package org.maoge.annotationassemble;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

@Component

public class Stage {

@Autowired // 按类型自动装配

private Singer singer;

@Autowired // 按类型自动装配

private Dancer dancer;

public Singer getSinger() {

 return singer;

}

public void setSinger(Singer singer) {

 this.singer = singer;

}

public Dancer getDancer() {

 return dancer;

}

public void setDancer(Dancer dancer) {

 this.dancer = dancer;

}

}


3.3 测试运行

还是同样的xml和Main.java,直接运行Main进行测试,依然没问题。


4. 总结

在使用注解时,实现了装配的高度自动化,非常锋利。


装配这个事,并不复杂,就是往一个bean的属性中注入其他bean。


自动装配,就是指定要装配的bean的类型或者名称,然后指定自动装配的规则(名称/类型),然后由容器自动将相应bean注入被装配bean的属性。

相关文章
|
15天前
|
存储 Java 数据安全/隐私保护
|
15天前
|
缓存 Java Sentinel
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
|
4天前
|
运维 Java 程序员
Spring5深入浅出篇:基于注解实现的AOP
# Spring5 AOP 深入理解:注解实现 本文介绍了基于注解的AOP编程步骤,包括原始对象、额外功能、切点和组装切面。步骤1-3旨在构建切面,与传统AOP相似。示例代码展示了如何使用`@Around`定义切面和执行逻辑。配置中,通过`@Aspect`和`@Around`注解定义切点,并在Spring配置中启用AOP自动代理。 进一步讨论了切点复用,避免重复代码以提高代码维护性。通过`@Pointcut`定义通用切点表达式,然后在多个通知中引用。此外,解释了AOP底层实现的两种动态代理方式:JDK动态代理和Cglib字节码增强,默认使用JDK,可通过配置切换到Cglib
|
2天前
|
Java Spring
Spring文件配置以及获取
Spring文件配置以及获取
10 0
|
3天前
|
JavaScript Java 开发者
Spring Boot中的@Lazy注解:概念及实战应用
【4月更文挑战第7天】在Spring Framework中,@Lazy注解是一个非常有用的特性,它允许开发者控制Spring容器的bean初始化时机。本文将详细介绍@Lazy注解的概念,并通过一个实际的例子展示如何在Spring Boot应用中使用它。
16 2
|
4天前
|
前端开发 Java
SpringBoot之自定义注解参数校验
SpringBoot之自定义注解参数校验
15 2
|
8天前
|
Java 微服务 Spring
Spring Boot中获取配置参数的几种方法
Spring Boot中获取配置参数的几种方法
20 2
|
10天前
|
消息中间件 安全 Java
在Spring Bean中,如何通过Java配置类定义Bean?
【4月更文挑战第30天】在Spring Bean中,如何通过Java配置类定义Bean?
19 1
|
10天前
|
Java Spring
springboot自带的@Scheduled注解开启定时任务
springboot自带的@Scheduled注解开启定时任务
|
12天前
|
Java 开发者 Spring
Spring Boot中的资源文件属性配置
【4月更文挑战第28天】在Spring Boot应用程序中,配置文件是管理应用程序行为的重要组成部分。资源文件属性配置允许开发者在不重新编译代码的情况下,对应用程序进行灵活地配置和调整。本篇博客将介绍Spring Boot中资源文件属性配置的基本概念,并通过实际示例展示如何利用这一功能。
22 1