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

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

使用方法:

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

@Component("标识符")
POJO类

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

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

 <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>
  • 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表示过滤器表达式。

下面为案例分析:

 package com.proc.bean;

 import org.springframework.stereotype.Component;

 @Component
public class TestObject { }

TestObject

 package com.proc.bean.Controller;

 import org.springframework.stereotype.Controller;

 @Controller
public class UserController { }

UserController

 package com.proc.bean.repository;

 public interface UserRepository {

     void save();
}

UserRepository

 package com.proc.bean.repository;

 import org.springframework.stereotype.Repository;

 @Repository("userRepository")
public class UserRepositoryImps implements UserRepository{ @Override
public void save() {
System.out.println("UserRepository save");
}
}

UserRepositoryImps

 package com.proc.bean.service;

 import org.springframework.stereotype.Service;

 @Service
public class UserService { }

UserService

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

 <context:component-scan base-package="com.proc.bean"></context:component-scan>

测试输出

 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

 TestObject testObject=(TestObject) ctx.getBean("testObject");
System.out.println(testObject); UserController userController=(UserController) ctx.getBean("userController");
System.out.println(userController); UserService userService=(UserService) ctx.getBean("userService");
System.out.println(userService); UserRepository userRepository=(UserRepository)ctx.getBean("userRepository");
System.out.println(userRepository);

输出结果:

com.proc.bean.TestObject@50d156
com.proc.bean.Controller.UserController@61c7e3
com.proc.bean.service.UserService@11d0846
com.proc.bean.repository.UserRepositoryImps@1e4c80f

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

<context:component-scan base-package="com.proc.bean" resource-pattern="controller/*.class">
</context:component-scan>

这里是能够正确获取到com.proc.bean.Controller.UserController@61c7e3

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

 <context:component-scan base-package="com.proc.bean">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>

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

这里能够正确获取到

com.proc.bean.TestObject@191f517
com.proc.bean.controller.UserController@5965f2
com.proc.bean.service.UserService@9bd883

4、排除指定标识符的类

 <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类型的,所以只能够正确得到

com.proc.bean.TestObject@134517
com.proc.bean.service.UserService@50d156
com.proc.bean.repository.UserRepositoryImps@61c7e3

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

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

这里只能够正确得到

com.proc.bean.repository.UserRepositoryImps@1200884

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

6、包含指定类型的类

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

这里只能够正确得到

com.proc.bean.TestObject@17f23d9

最后提醒:

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

Spring bean注解配置(1)的更多相关文章

  1. JavaWeb_(Spring框架)注解配置

    系列博文 JavaWeb_(Spring框架)xml配置文件  传送门 JavaWeb_(Spring框架)注解配置 传送门 Spring注解配置 a)导包和约束:基本包.aop包+context约束 ...

  2. spring aop注解配置

    spring aop是面向切面编程,使用了动态代理的技术,这样可以使业务逻辑的代码不掺入其他乱七八糟的代码 可以在切面上实现合法性校验.权限检验.日志记录... spring aop 用的多的有两种配 ...

  3. java框架之Spring(2)-注解配置IOC&AOP配置

    注解配置IoC 准备 1.要使用注解方式配置 IoC,除了之前引入的基础 jar 包,还需要引入 spring-aop 支持包,如下: 2.在 applicationContext.xml 中引入 c ...

  4. Spring自定义注解配置切面实现日志记录

    一: spring-mvc.xml: <!--配置日志切面 start,必须与mvc配置在同一个配置文件,否则无法切入Controller层--><!-- 声明自动为spring容器 ...

  5. 学妹问的Spring Bean常用配置,我用最通俗易懂的讲解让她学会了

    你好呀,我是沉默王二,一枚有趣的程序员,写的文章一直充满灵气,力求清新脱俗.昨天跑去王府井的小米店订购了一台小米 10,说是一周之内能到货,但我还是忍不住今天就想见到她.见我茶不思饭不想的,老婆就劝我 ...

  6. Spring纯注解配置

    待改造的问题 我们发现,之所以我们现在离不开 xml 配置文件,是因为我们有一句很关键的配置: <!-- 告知spring框架在,读取配置文件,创建容器时,扫描注解,依据注解创建对象,并存入容器 ...

  7. springmvc学习指南 之---第25篇 Spring Bean有三种配置方式

    writed by不要张艳涛, 从tomcat转到了springmvc 现在开始有点不知道该看什么书了,看完了springmvc 学习指南之后 又查了一些书,好多都是内容相近,在找书的过程之中,发现s ...

  8. Spring @Bean 注解的使用

    使用说明 这个注解主要用在方法上,声明当前方法体中包含了最终产生 bean 实例的逻辑,方法的返回值是一个 Bean.这个 bean 会被 Spring 加入到容器中进行管理,默认情况下 bean 的 ...

  9. spring @bean注解

    1.@bean注解用于注册一个bean到 到ioc容器中.类似于@component注解 2.@configure注解,相当于指明这个类是配置文件 3.@bean还可以指定initMethod,des ...

随机推荐

  1. settype和gettype

    settype — 设置变量的类型 <?php$foo = "5bar"; // string$bar = true;   // boolean settype($foo,  ...

  2. IOS延时加载网络图片

        重网上下载图片是很慢的,为了不影响体验,选择延时加载图片是很好的办法. 一个tableView 列表,左边暂时没有图 - (UITableViewCell *)tableView:(UITab ...

  3. 获取当前进程目录 GetCurrentDirectory() 及 获取当前运行模块路径名GetModuleFileName()

    GetCurrentDirectory 获得的是当前进程的活动目录(资源管理器决定的),可以用SetCurrentDirectory 修改的. 转自 http://m.blog.csdn.net/bl ...

  4. webstrom vue配置eslint

    (得出结论,还是得从官方文档中找,哇!!) 1.安装eslint插件,可以从search in repositories中获得,或者:http://plugins.jetbrains.com/plug ...

  5. 那些年,追寻JMeter的足迹,免费送……

    ​我们测试技术部武汉团队自16年引入jmeter以来,利用jmeter做了很多事情.首先运用jmeter进行接口测试,后续实现jmeter自动化冒烟测试,顺带也实现了线上环境的巡检,节省了大量人力.j ...

  6. UI基础:UIButton.UIimage 分类: iOS学习-UI 2015-07-01 21:39 85人阅读 评论(0) 收藏

    UIButton是ios中用来响应用户点击事件的控件.继承自UIControl 1.创建控件 UIButton *button=[UIButton buttonWithType:UIButtonTyp ...

  7. 人脸对齐matlab实现-FaceAlignment 3000fps

    前言 最近研读了孙剑团队的Face Alignment at 3000fps via Regressing Local Binary Features这篇paper,基于matlab进行实现. 实现原 ...

  8. [LeetCode&Python] Problem 136. Single Number

    Given a non-empty array of integers, every element appears twice except for one. Find that single on ...

  9. CTF-练习平台-Misc之 这是一张单纯的图片??

    一.这是一张单纯的图片?? 分析图片,首先查看图片属性 没有发现有用的信息,再用WinHex打开 发现最后面有点眼熟,和ASCII表对应后发现是flag

  10. hdu 5185 dp(完全背包)

    BC # 32 1004 题意:要求 n 个数和为 n ,而且后一个数等于前一个数或者等于前一个数加 1 ,问有多少种组合. 其实是一道很水的完全背包,但是没有了 dp 的分类我几乎没有往这边细想,又 ...