Spring-基于注解的配置[01定义Bean+扫描Bean]

概述

前几篇博文中主要讲述了基于XML的配置。

不管是XML还是注解,他们都是在表达Bean定义的载体,其实质都是为Spring容器提供Bean定义的信息,在表现形式上都是将XML定义的内容通过类注解进行描述。

基于注解的配置方式,在Spring2.0引入,Spring2.5完善,Spring4.0得到了进一步的增强。

我们知道,Spring容器成功启动的三大要件分别是:

  1. bean的定义信息
  2. bean的实现类
  3. Spring本身

如果采用XML的配置,则Bean的定义信息和Bean的实现类本身是分离的。
而如果采用基于注解的配置方式,则Bean的定义信息通过Bean实现类上标注的注解实现。


使用注解定义Bean

在另外一篇博客中:Spring-Spring MVC + Spring JDBC + Spring Transaction + Maven 构建web登录模块

以案例来说明:

// 通过Spring注解定义一个DAO(这里略微改动了下,使用@Component ,更合适的是@Repository,仅仅是为了演示)
@Component 
public class UserDao {

    .......

}

使用@Component注解在UserDao类声明处对类进行标注,它可以被Spring容器识别,Spring容器自动将POJO转换为容器管理的Bean。

它和下面的XML配置是等效的

<bean id="userDao" class="com.xgj.dao.UserDao">

使用注解的方式,默认生成的beanId为类名的首字母小写,也可指定其id,如:@Service("xgjService")

除了@Component注解外,Spring还提供了3个功能基本和@Component等效的注解,分别用于对DAO、Service和Web层的Controller进行注解

  • @Repository用于对DAO实现类进行标注
  • @Service用于对Service实现类进行标注
  • @Controller用于对Controller实现类进行注解

之所以要在@Component之外提供3个特殊的注解,是为了让标注类本身的用途清晰化,当然了完全可以用@Component替代这3个特殊的注解, 但是我们推荐使用特定的注解标注特定的Bean,这样可以明确的了解Bean的真是身份


扫描注解定义的Bean

Spring提供了一个context命名空间,它提供了通过扫描类包以应用注解定义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" 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">


    <!-- (1)声明Context命名空间以及Schema文件   (2)扫描类包以及应用注解定义的bean -->
    <context:component-scan base-package="com.xgj.ioc.configuration"/>

</beans>

使用步骤

  1. 首先声明context命名空间和schema文件

  2. 然后通过context命名空间的component-scan的base-package属性指定一个需要扫描的基类包,Spring容器会扫描这个基类包里的所有类,并从类的注解信息中获取Bean的定义信息。


扫描特定的类 resource-pattern

假设我们希望扫描特定的类,而非基包下所有的类,怎么办呢?

Spring context命名空间提供了resouce-pattern属性过滤出特定的类

    <!-- 通过resource-pattern使Spring加载特定的Bean -->
    <context:component-scan base-package="com.xgj.ioc.configuration.resourcePattern"
        resource-pattern="scan/*.class"/>

这里设置的基类包为com.xgj.ioc.configuration.resourcePattern,默认情况下resource-pattern属性的值为 **./*.class,即基类包里所有的类。

如果有设置为scan/*.class,则Spring仅会扫描基类包里scan子包中的类。

实例

通过配置文件指定Spring只加载 scan目录下的类的注解,测试resource-pattern属性。

这里写图片描述

配置文件:

<?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">

    <!-- 通过resource-pattern使Spring加载特定的Bean -->
    <context:component-scan base-package="com.xgj.ioc.configuration.resourcePattern"
        resource-pattern="scan/*.class"/>

</beans>

scan目录下的POJO

package com.xgj.ioc.configuration.resourcePattern.scan;

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

@Component
public class Pilot {

    @Autowired
    private Plane plane;

    public void drivePlane() {
        plane.fly();
    }
}
package com.xgj.ioc.configuration.resourcePattern.scan;

import org.springframework.stereotype.Component;

@Component
public class Plane {

    private String brand;
    private String color;
    private int speed;

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public void introduce() {
        System.out.println("Plane information【 brand:" + brand + ",color:"
                + color + ",speed:" + speed);
    }

    public void fly() {
        System.out.println("Plane begins to  fly");
    }
}

测试类:

package com.xgj.ioc.configuration.resourcePattern.scan;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ConfigBeanTest {

    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "classpath:com/xgj/ioc/configuration/resourcePattern/resourcePatternBeans.xml");

        Pilot pilot = ctx.getBean("pilot", Pilot.class);
        pilot.drivePlane();

        Plane plane = ctx.getBean("plane", Plane.class);
        plane.setBrand("A380");
        plane.setColor("White");
        plane.setSpeed(800);

        plane.introduce();

    }
}

NoScan包下的POJO (PilotNoScan、PlaneNoScan)仅仅类名不同(同名的话,默认ID相同,Spring加载会报错。)

测试类

package com.xgj.ioc.configuration.resourcePattern.noScan;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ConfigBeanTest {

    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "classpath:com/xgj/ioc/configuration/resourcePattern/resourcePatternBeans.xml");

        PilotNoScan pilot = ctx.getBean("pilotNoScan", PilotNoScan.class);
        pilot.drivePlane();

        PlaneNoScan plane = ctx.getBean("planeNoScan", PlaneNoScan.class);
        plane.setBrand("A380-NoScan");
        plane.setColor("White-NoScan");
        plane.setSpeed(800);

        plane.introduce();

    }
}

运行scan报下的测试类

这里写图片描述

运行noScan报下的测试类,报错

No bean named 'pilotNoScan' available

这里写图片描述

说明,Spring容器并没有实例化这个类。resource-pattern起了作用。

当我们去掉配置文件中的 resource-pattern="scan/*.class"后,再此运行

这里写图片描述

可见,Spring容器可以正确加载并实例化Bean


include-filter exclude-filter过滤子元素的使用

通过resource-pattern属性可以按照资源名称对基类包中的类进行过滤,如果我们有个需求:仅需过滤基类包中实现了XxxService接口的类或者标注了某个特定注解的类等 ,该怎么办呢?

Spring的<context:componet-scan>为我们提供了过滤子元素,我们可以轻松的通过其实现上面的需求。

<context:component-scan base-package="com.xgj.ioc.configuration.resourcePattern"
        resource-pattern="scan/*.class">
    <context:include-filter type="regex" expression="com\.xgj\.ioc\.configuration\.resourcePattern\.scan.*"/>   
    <context:exclude-filter type="aspectj" expression="com.xgj.ioc.configuration.resourcePattern..*Controller+"/>
</context:component-scan>
  • <context:include-filter>表示要包含的目标类
  • <context:exclude-filter>表示要排除的目标类
  • 一个<context:component-scan>下可以有多个<context:include-filter><context:exclude-filter>元素

支持多种类型的过滤表达式

类别示例说明
annotationcom.xgj.XxxAnnotation所有标注了XxxAnnotation的类。该类型采用目标类是否标志了某个注解进行过滤
assignablecom.xgj.XxService所有继承或扩展XXXService的类。该类型采用目标类是否继承或者扩展了某个特定类进行过滤
aspectjcom.xgj..*Service+所有类名以Service结束的类及继承或者扩展他们的类。该类型采用AspectJ表达式进行过滤
regexcom.xgj.auto..*所有com.xgj.auto类包下的类。该类型采用正则表达式根据目标类的类名进行过滤
customcom.xgj.XxxTypeFilter采用XxxTypeFilter代码方式实现过滤规则,该类必须实现org.springframework.core.type.TypeFilter接口

在所有的过滤类型中,除了custom类型外,aspectj的过滤表达能力是最强的,可以轻易实现其他类型所能表达的过滤规则。

实例

我们先将测试类改下名,保证不重名,否则会抛出如下异常

Annotation-specified bean name 'configBeanTest' for bean class [com.xgj.ioc.configuration.resourcePattern.scan.ConfigBeanTest] conflicts with existing, non-compatible bean definition of same name and class [com.xgj.ioc.configuration.resourcePattern.noScan.ConfigBeanTest]

这里写图片描述

配置文件:

<?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">

    <!-- 通过resource-pattern使Spring加载特定的Bean 
    <context:component-scan base-package="com.xgj.ioc.configuration.resourcePattern" 
        resource-pattern="scan/*.class"/> -->

    <!-- context:include-filter context:exclude-filter -->
    <context:component-scan base-package="com.xgj.ioc.configuration.resourcePattern">
        <context:include-filter type="regex"
            expression="com\.xgj\.ioc\.configuration\.resourcePattern.*" />
        <context:exclude-filter type="aspectj" expression="com.xgj.ioc.configuration.resourcePattern..*NoScan+"/>
    </context:component-scan>

</beans>

配置文件解析:

<context:include-filter type="regex"
    expression="com\.xgj\.ioc\.configuration\.resourcePattern.*" />

使用<context:include-filter> ,通过regex正则表达式过滤方式,包含com.xgj.configuration.resourcePattern目录下的所有类,按照项目结果包括scan和noScan子包中的注解类。

<context:exclude-filter type="aspectj"    expression="com.xgj.ioc.configuration.resourcePattern..*NoScan+"/>

使用<context:exclude-filter>,通过aspectj的过滤方式,排除掉com.xgj.ioc.configuration.resourcePattern包及子包下所有以NoScan+结尾的类,按照项目结构即排除掉noScan子包下的注解类。


其余POJO和测试类同上。

测试scan包下的测试类

这里写图片描述

测试noScan包下的测试类

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'pilotNoScan' available

这里写图片描述

可见 <context:include-filter><context:exclude-filter>标签起了作用。


use-default-filters属性

use-default-filters属性默认值为true,表示会对标注@Component、@Controller、@Service、@Reposity的Bean进行扫描。

<context:component-scan>首先根据 exclude-filter列出需要排除的黑名单,然后再根据include-filter流出需要包含的白名单。 但是由于use-default-filters属性默认值的作用,看下下面的配置片段,不但会对@Controller的bean进行扫描,还会会对@Component、@Service、@Reposity的Bean进行扫描。

    <context:component-scan base-package="com.xgj.ioc.configuration.resourcePattern">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />

    </context:component-scan>

这里写图片描述

换句话讲,上面的配置和不加<context:include-filter>的效果是一样的,如果仅仅想扫描@Controller的bean,必须将use-default-filters属性设置为false.

如下所示

<context:component-scan base-package="com.xgj.ioc.configuration.resourcePattern" use-default-filters="false">
        <context:include-filter type="annotation"
    expression="org.springframework.stereotype.Controller" />   
</context:component-scan>

实例

配置文件

<?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:component-scan base-package="com.xgj.ioc.configuration.resourcePattern" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

</beans>

运行测试类 com.xgj.ioc.configuration.resourcePattern.scan.ConfigBeanTest

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'pilot' available

这里写图片描述

如果我们去掉use-default-filters=”false”试下呢?

这里写图片描述

由此可见 use-default-filters=”false” 起到了只加载或者排除特定注解的功能。

Spring中,我们可以使用注解配置和装配Bean,这可以使我们的代码更加简洁和易于维护。下面是关于如何基于注解配置和装配Bean的一些简要介绍: 1. 基于注解配置Bean 在Spring中,我们可以使用以下注解配置Bean: - @Component:表示该类是一个Spring Bean,需要被Spring容器管理。 - @Service:表示该类是一个服务层的Bean。 - @Controller:表示该类是一个控制层的Bean。 - @Repository:表示该类是一个数据访问层的Bean。 这些注解都是基于@Component注解的衍生注解,它们的作用是更加明确地表示Bean的角色。我们可以在Bean类上添加这些注解,告诉Spring容器该类需要被管理。例如: ``` @Service public class UserService { // ... } ``` 2. 基于注解装配Bean 在Spring中,我们可以使用以下注解来装配Bean: - @Autowired:自动装配Bean。 - @Qualifier:指定具体的Bean名称进行装配。 - @Resource:指定具体的Bean名称进行装配,与@Qualifier类似。 - @Value:注入一个具体的值。 使用@Autowired注解进行自动装配时,Spring会自动在容器中寻找与该类型匹配的Bean,并将其注入到类的属性中。例如: ``` @Service public class UserService { @Autowired private UserDao userDao; // ... } ``` 使用@Qualifier或@Resource注解可以指定具体的Bean名称进行装配。例如: ``` @Service public class UserService { @Autowired @Qualifier("userDaoImpl") private UserDao userDao; // ... } ``` 使用@Value注解可以注入一个具体的值。例如: ``` @Service public class UserService { @Value("10") private int maxCount; // ... } ``` 以上就是关于Spring中基于注解配置和装配Bean的简要介绍,希望能对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小工匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值