前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring bean注解配置(1)—— @Component

Spring bean注解配置(1)—— @Component

作者头像
浩Coding
发布2019-07-03 12:53:11
1.7K0
发布2019-07-03 12:53:11
举报
文章被收录于专栏:浩Coding浩Coding

Spring自带的@Component注解及扩展@Repository@Service@Controller,如图:

在使用注解方式配置bean时,需要引进一个包:

使用教程

1、为需要使用注解方式的类添加注解标记。

代码语言:javascript
复制
@Component("名称")  
public class Test {
}

在类上使用@Component注解,表示该类定义为Spring管理Bean,使用默认value(可选)属性表示Bean标识符如果不指定标识符,默认为首字母小写类名。例如类Test的标识符为test。

2、在xml中配置自动扫描策略

代码语言:javascript
复制
<?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: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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
   <!-- 指定需要扫描的包,使注解生效 -->
    <context:component-scan
            base-package=""
            resource-pattern="**/*.class"
            name-generator="org.springframework.context.annotation.AnnotationBeanNameGenerator"
            use-default-filters="true"
            annotation-config="true">
            <context:include-filter type="aspectj" expression=""/>
            <context:exclude-filter type="regex" expression=""/>
    </context:component-scan>
</beans>

base-package:表示扫描注解类的开始位置,即将在指定的包中扫描,其他包中的注解类将不被扫描,默认将扫描所有类路径

resource-pattern:表示扫描注解类的后缀匹配模式,即“base-package+resource-pattern”将组成匹配模式用于匹配类路径中的组件,默认后缀为“**/*.class”,即指定包下的所有以.class结尾的类文件

name-generator:默认情况下的Bean标识符生成策略,默认是AnnotationBeanNameGenerator,其将生成以小写开头的类名(不包括包名);可以自定义自己的标识符生成策略。

use-default-filters:默认为true表示过滤@Component、@ManagedBean、@Named注解的类,如果改为false默认将不过滤这些默认的注解来定义Bean,即这些注解类不能被过滤到,即不能通过这些注解进行Bean定义。

annotation-config:表示是否自动支持注解实现Bean依赖注入,默认支持,如果设置为false,将关闭支持注解的依赖注入,需要通过<context:annotation-config/>开启。

<context:include-filter>:表示过滤到的类将被注册为Spring管理Bean。需要配合use-default-filters使用。

<context:exclude-filter>:表示过滤到的类将不被注册为Spring管理Bean,它比<context:include-filter>具有更高优先级。

type表示过滤器类型,目前支持注解类型、类类型、正则表达式、aspectj表达式过滤器,当然也可以自定义自己的过滤器,实现org.springframework.core.type.filter.TypeFilter即可。

expression:表示过滤器表达式

实例分析

代码语言:javascript
复制
@Component()
/**相当于@Component("annotationUser")或@Component(value = "annotationUser"),annotationUser为Bean的id,默认为首字母小写的类名**/
public class AnnotationUser {
  @Value("chenheng")//只注入了简单的值,复杂值的注入目前使用该方式还解决不了
  private String uname;
}
代码语言:javascript
复制
@Controller
public class TestController {
  @Autowired
  //@Autowired 自动按照Bean类型(本例子中就是TestService类型)装配,注入给testService变量,下章详细讲解
  private TestService testService;
  public void save() {
    testService.save();
    System.out.println("testController save");
  }
}
代码语言:javascript
复制
public interface TestService {
  public void save();
}
@Service("testService")//相当于@Service
public class TestSeviceImpl implements TestService{
  @Resource(name="testDao")
  /**相当于@Autowired,@Autowired默认按照Bean类型装配**/
  private TestDao testDao;
  @Override
  public void save() {
    testDao.save();
    System.out.println("testService save");
  }
}

需要在配置文件中配置注解通过base-package指定扫描指定包及其子包下所有类,注解配置方式如下:

代码语言:javascript
复制
<!-- 使用context命名空间,通过Spring扫描指定包下所有Bean的实现类,进行注解解析 -->
  <context:component-scan base-package="annotation"/>

测试输出:

代码语言:javascript
复制
import annotation.controller.TestController;
public class TestMoreAnnotation {
  public static void main(String[] args) {
    ApplicationContext appCon = new ClassPathXmlApplicationContext("annotationContext.xml");
    TestController testcon = (TestController)appCon.getBean("testController"); 
    testcon.save();
  }
}

1、在xml中配置,通过base-package指定扫描指定包及其子包下所有类

代码语言:javascript
复制
<context:component-scan base-package="com.proc.bean"></context:component-scan>

2、指定resource-pattern:资源匹配,只扫描controller包下面的所有类

代码语言:javascript
复制
<context:component-scan base-package="com.proc.bean" resource-pattern="controller/*.class">
</context:component-scan>

3、 排除使用指定注解标签的类

代码语言:javascript
复制
<context:component-scan base-package="com.proc.bean">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>

type:选择类型 annotation:注解标签、assignable:类名方式

4、排除指定标识符的类

代码语言:javascript
复制
<context:component-scan base-package="com.proc.bean">
    <context:exclude-filter type="assignable" expression="com.proc.bean.controller.UserController"/>
</context:component-scan>

这里排除了com.proc.bean.controller.UserController类型的。

5、包含指定注解标记的类

代码语言:javascript
复制
<context:component-scan base-package="com.proc.bean" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>

在使用include-filter是需要配合use-default-filters="false",让自动扫描注解不使用默认的filter,而是使用我们指定的filter,否则将无效。

6、包含指定类型的类

代码语言:javascript
复制
<context:component-scan base-package="com.proc.bean" use-default-filters="false">
   <context:include-filter type="assignable" expression="com.proc.bean.TestObject"/>
</context:component-scan>

<context:include-filter/>和<context-exclude-filter/>标签可以使用多个。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-03-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 浩Coding 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档