在classpath中扫描组件

组键扫描:能够从classpath下自动扫描,侦测和实例化具有特定注解的组件
特定的组件包括:
->@Componment:基于注解,标识一个受Spring管理的组键
->@Respository:标识持久层组件
->@Service:标识服务层
->@controller:标识表现层组件
对于扫描到的组件,Spring有默认的命名策略,使用非限定类名,第一个字母小写,也可以通过注解中value属性值标识组建的名称

在classpath中扫描组键
当在组键类中使用了特定的注解后,还需要再Spring的配置中声明<context:componment-scan>
->base-packge属性是指定一个需要扫描的基类包,Spring容器将会扫描这个基类包里及其他子类包中的所有类
->当需要扫描多个包时,可以使用逗号分隔
->如果仅希望扫描特定类而非集包下的所有类,可使用resource-pattren属性过滤特定的类

通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个标签,配置完这个标签后,spring就会去自动

扫描base-package对应的路径或者该路径的子包下面的java文件,
如果扫描到文件中带有@Service,@Component,@Repository,@Controller等这些注解的类,则把这些类注册为bean
注:在注解后加上例如@Component(value=”abc”)时,注册的这个类的bean的id就是abc.

TestObject.java
@Component
public class TestObject {
//@Componment:基于注解,标识一个受Spring管理的组键
}
UserController.java
@Controller
public class UserController {
//建立关联关系
@Autowired
private UserService userService; public void execute() {
System.out.println("UserController execute...");
userService.add();
}
}

UserRepository.java

public interface UserRepository {
void save();
}

UserRepositoryImpl.java

@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository {
@Override
public void save() {
System.out.println("UserRepository save...");
}
}
UserService.java
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public void add() {
// TODO Auto-generated method stub
System.out.println("UserService add...");
userRepository.save();
}
}

applicationContext.xml

<!-- 指定IOC容器扫描的包 -->
<!-- 下面的引入文件,没有进入IOC容器的立马进入IOC容器,有S的标志 -->
<!-- 扫描这个包以及它的子包 -->
<context:component-scan base-package="com.MrChengsb.Annotations" ></context:component-scan> <!--
只扫描子包中这个包里的类
resource-pattern="repository/*.class" 不包含...
指定排除那些表达式的组键
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
--> <!--
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
--> <!--
和 context:component-scan 中的 use-default-filters="false" 进行搭配才可以只包括,就是只有包含的这个
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
-->
<!--
   只包含
   use-default-filters="false" 共同使用
<context:include-filter type="assignable" expression="com.MrChengsb.Annotations.repository.UserRepository"/>
-->
<!--
不包含这个接口以及它的实现类
<context:exclude-filter type="assignable"
expression="com.MrChengsb.Annotations.repository.UserRepository"/>
-->

测试以及相关注释:

public static void main(String[] args) {

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

        //没有设立关联关系的
// TestObject test = (TestObject) ctx.getBean("testObject");
// System.out.println(test);
//
// UserController uc = (UserController) ctx.getBean("userController");
// System.out.println(uc);
//
// UserRepositoryImpl urp = (UserRepositoryImpl) ctx.getBean("userRepository");
// System.out.println(urp);
//
// UserService use = (UserService) ctx.getBean("userService");
// System.out.println(use);
// //设立关联关系的
// TestObject test = (TestObject) ctx.getBean("testObject");
// System.out.println(test); UserController uc = (UserController) ctx.getBean("userController");
System.out.println(uc);
uc.execute(); // UserRepositoryImpl urp = (UserRepositoryImpl) ctx.getBean("userRepository");
// System.out.println(urp);
//
// UserService use = (UserService) ctx.getBean("userService");
// System.out.println(use); //没建立Bean之家的引用关系
// com.MrChengsb.Annotations.controller.UserController@2925bf5bException in thread "main"
// UserController execute...
// java.lang.NullPointerException
// at com.MrChengsb.Annotations.controller.UserController.execute(UserController.java:18)
// at com.MrChengsb.Annotations.main.main(main.java:37)
}

测试代码省略.......

5.spring:注解配置 Bean的更多相关文章

  1. Spring 注解配置Bean

    一.使用注解配置Bean 1.注解 在类定义.方法定义.成员变量定义前使用.其简化<bean>标签,功能同<bean>标签.格式为: @注解标记名. 2.组件扫描 Spring ...

  2. [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. Spring(二)--FactoryBean、bean的后置处理器、数据库连接池、引用外部文件、使用注解配置bean等

    实验1:配置通过静态工厂方法创建的bean  [通过静态方法提供实例对象,工厂类本身不需要实例化!] 1.创建静态工厂类 public class StaticFactory { private st ...

  4. Spring(十五):通过注解配置 Bean

    在ClassPath中扫描组件 1)组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件: 2)特定组件包含: --- @C ...

  5. IDEA02 利用Maven创建Web项目、为Web应用添加Spring框架支持、bean的创建于获取、利用注解配置Bean、自动装配Bean、MVC配置

    1 环境版本说明 Jdk : 1.8 Maven : 3.5 IDEA : 专业版 2017.2 2 环境准备 2.1 Maven安装及其配置 2.2 Tomcat安装及其配置 3 详细步骤 3.1 ...

  6. Spring IOC机制之使用注解配置bean

    一. 通过注解配置bean 1.1       概述 相对于XML方式而言,通过注解的方式配置bean更加简洁和优雅,而且和MVC组件化开发的理念十分契合,是开发中常用的使用方式. 1.2       ...

  7. Spring框架入门之基于Java注解配置bean

    Spring框架入门之基于Java注解配置bean 一.Spring bean配置常用的注解 常用的有四个注解 Controller: 用于控制器的注解 Service : 用于service的注解 ...

  8. (转)Spring注解完成Bean的定义

    使用Spring注解完成Bean的定义 2010-04-21 16:48:54|  分类: spring|举报|字号 订阅     下载LOFTER客户端     通过@Autowired或@Reso ...

  9. 通过注解配置Bean

    之前说的三种配置方式,都是使用XML配置,现在我们说说使用注解配置Bean. 这部分内容主要分为两个部分:使用注解配置Bean,使用注解配置Bean属性. 在classpath中扫描组件 组件扫描:S ...

随机推荐

  1. [转]Entity Framework Sprocs with Multiple Result Sets

    本文转自:https://msdn.microsoft.com/en-us/data/jj691402.aspx Entity Framework Sprocs with Multiple Resul ...

  2. HTTP POST上传文件说明及注意事项

    这一阵子因为项目需要要对接别家公司的restful接口,其中有一个接口是上传视频文件.接着上网找找资料.后来文件是上传成功了,但上服务器一看,文件没有后缀,我勒个去,这是什么个鬼.接着继续找资料,最终 ...

  3. mybatis学习之分页

    分页一般分为物理分页:先查询所有值再分页输出,逻辑分页:直接分页查询输出,mybatis支持物理分页,如下: 1.物理分页: mapper映射: <select id="findStu ...

  4. spring-boot之简单定时任务

    首先是pom.xml依赖: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:/ ...

  5. centos7关于防火墙的操作

    1.firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status firewalld  停止: systemctl disab ...

  6. javaweb带父标签的自定义标签

    1.完整的示例代码:要实现的功能是父标签中有name属性,子标签将父标签的name属性值打印到jsp页面上. 1.1 父类和子类的标签处理器类 testParentTag.java package c ...

  7. ueditor PHP版本使用方法

    1.ueditor是百度很好用的一款文本编辑器,第一次使用,在此记录使用方法. 2.从http://ueditor.baidu.com/website/download.html#ueditor下载e ...

  8. laravel后台注册登入

    1.只需要在新安装的 Laravel 应用下运行 php artisan make:auth 和 php artisan migrate,这两个命令会生成用户登录注册所需要的所有东西 2.你会发现 h ...

  9. python3在anaconda下安装caffe失败

    Python 跟 Python3 完全就是两种语言 0x00 import caffe FAILED 环境为 Ubuntu 16 cuda 8.0 NVIDIA 361.77 Anaconda2.昨天 ...

  10. Logger性能优化

    最近排查线上问题,无意中发现了Logger堵塞的情况,排查的同时也做下总结,做个笔记,以防备用. 先上图,看下实际堵塞的情况 从图中可以清楚的看到标黄的都在 waiting to lock <0 ...