组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件。

特定组件包括:

  1、@Component:基本注解,识别一个受Spring管理的组件

  2、@Respository:标识持久层组件

  3、@Service:标识业务层组件

  4、@Controller:标识表现层组件

Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称

当在组件类上使用了特定的注解之后, 还需要在 Spring 的配置文件中声明<context:component-scan>
  1、base-package 属性指定一个需要扫描的基类包,Spring 容器将会扫描这个基类包里及其子包中的所有类.
  2、当需要扫描多个包时, 可以使用逗号分隔.
  3、<context:include-filter /><context:exclude-filter />

 <!-- 扫描@Controller注解 -->
<context:component-scan base-package="com.hzg.controller" use-default-filters="false">
<context:include-filter type="annotation"expression="org.springframework.stereotype.Controller" />
</context:component-scan> <!-- 配置扫描注解,不扫描@Controller注解 -->
<context:component-scan base-package="com.hzg.controller">
<context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller" />
</context:component-scan>

  当使用<context:include-filter />的时候,在<context:component-scan >里必须加上use-default-filters="false",否则不起作用。

  其中属性expression的值不是你的包所在位置,别搞错了,它是你注解的具体类地址。

实例:

  创建包com.hzg.anotation

  创建包com.hzg.anotation.controller

  创建UserController类

 @Controller
public class UserController { @Autowired(required = false)
private UserService userService;
//@Autowired也可以放在setter方法上,那就去掉上面的Autowired注解
public void setUserService(UserService userService) {
this.userService = userService;
} public void excute(){
System.out.println("UserController excute");
userService.diao();
}
}

  创建包com.hzg.anotation.service

  创建UserService类

 @Service
public class UserService { @Autowired
private UserRepostory userRepostory;
public void diao(){
System.out.println("UserService diao");
userRepostory.save();
}
}

  创建包com.hzg.anotation.repostory

  创建UserRepostory接口

 public interface UserRepostory {
void save();
}

  创建UserRepostoryImlp类

 @Repository("userRepostory")
public class UserRepostoryImlp implements UserRepostory { @Override
public void save() {
System.out.println("UserRepostory save");
}
}

  创建configautowire.xml文件

 <context:component-scan base-package="com.hzg.anotation"></context:component-scan>

  Main方法

 public static void main(String[] args) {
2  ApplicationContext ctx = new ClassPathXmlApplicationContext("configautowire.xml");
3  UserController userController = (UserController) ctx.getBean("userController");
4  userController.excute();
}

  输出接口:

UserController excute
UserService diao
UserRepostory save

其中:

  1、@Autowired(required = false)中required = false的意思是:如果没有这个类的实例化,那么会赋值成NULL,而不是报错。

  2、@Autowired注解可以为成员变量、方法、构造函数赋值。

  3、@Repository("userRepostory")等同于@Repository(value = "userRepostory"),value是默认值,代表给这个Bean

     赋值了id的值,防止有重复的Bean。

  4、如果在UserService类的@Autowired下面使用限定修饰符@Qualifier("userRepostoryImlp"),那么

     @Repository("userRepostory")必须写成@Repository或者写成@Repository("userRepostoryImlp"),否则就有歧义了。

------------------------------------------------------------------------------------------------------------------------

跟着刚哥学习Spring框架系列:

跟着刚哥学习Spring框架--创建HelloWorld项目(一)

跟着刚哥学习Spring框架--Spring容器(二)

跟着刚哥学习Spring框架--通过XML方式配置Bean(三)

跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

跟着刚哥学习Spring框架--AOP(五)

跟着刚哥学习Spring框架--JDBC(六)

跟着刚哥学习Spring框架--事务配置(七)

跟着刚哥学习Spring框架--通过注解方式配置Bean(四)的更多相关文章

  1. 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)

    Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式   √ id:标识容器中的bean.id唯一. √ cl ...

  2. 跟着刚哥学习Spring框架--创建HelloWorld项目(一)

    1.Spring框架简介 Spring是一个开源框架,Spring是在2003年兴起的一个轻量级的开源框架,由Rod johnson创建.主要对JavaBean的生命周期进行管理的轻量级框架,Spri ...

  3. 跟着刚哥学习Spring框架--AOP(五)

    AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...

  4. 跟着刚哥学习Spring框架--Spring容器(二)

    Spring容器 启动Spring容器(实例化容器) -- IOC容器读取Bean配置创建Bean实例之前,必须对它进行实例化(加载启动),这样才可以从容器中获取Bean的实例并使用.  Bean是S ...

  5. 跟着刚哥学习Spring框架--事务配置(七)

    事务 事务用来保证数据的完整性和一致性. 事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACID特性.1.原子性(atomicity).一个事务是一个不可分割的工作单位,事务 ...

  6. 跟着刚哥学习Spring框架--JDBC(六)

    Spring的JDBC框架 Spring JDBC提供了一套JDBC抽象框架,用于简化JDBC开发. Spring主要提供JDBC模板方式.关系数据库对象化方式.SimpleJdbc方式.事务管理来简 ...

  7. spring之通过注解方式配置Bean(一)

    (1)组件扫描:spring能够从classpath下自动扫描.侦测和实例化具有特定注解的组件. (2)特定组件包括: @Component:基本注解,标识一个受spring管理的组件: @Respo ...

  8. Spring使用ioc注解方式配置bean

    context层 : 上下文环境/容器环境 applicationContext.xml 具体示例: 现在ioc容器中添加context层支持: 包括添加xmlns:context.xsi:schem ...

  9. Spring初学之注解方式配置bean

    直接看代码: UserController.java package spring.beans.annotation.controller; import org.springframework.be ...

随机推荐

  1. Time.fixedDeltaTime和Time.DeltaTime

    在Update中使用 Time.deltaTime,获取到的是这一帧的时间,如果游戏卡,帧率低,那这个值就大.如果游戏流畅,帧率高,这个值就小,Time.deltaTime = 1.0f / 帧率 在 ...

  2. kbmmw 的HTTPSmartService 上传文件到服务器端

    前面我写过了 HTTPSmartService 使用介绍,可以参见以前的文章. 前一向有同学问如何在http 页面表单上上传文件.一直没有时间回答,自己简单做了例子, 发现无法实现功能,今天花了一天时 ...

  3. 【转】【MySQL】时间类型存储格式选择

    一  前言  昨天在给开发同学做数据库设计规范分享的时候,讲到时间字段常用的有三个选择datetime.timestamp.int,应该使用什么类型的合适?本文通过三种类型的各个维度来分析,声明:本文 ...

  4. 2019.02.09 bzoj4455: [Zjoi2016]小星星(容斥原理+dp)

    传送门 题意简述:给一张图和一棵树(点数都为n≤17n \le17n≤17),问有多少种给树的标号方法方法使得图中去掉多余的边之后和树一模一样. 思路: 容斥好题啊. 考虑fi,jf_{i,j}fi, ...

  5. 2019.01.22 zoj3583 Simple Path(并查集+枚举)

    传送门 题意简述:给出一张图问不在从sss到ttt所有简单路径上的点数. 思路: 枚举删去每个点然后把整张图用并查集处理一下,同时不跟sss和ttt在同一个连通块的点就是满足要求的点(被删去的不算). ...

  6. jquery遍历之parent()与parents()的区别 及 parentsUntil() 方法

    来自:http://blog.csdn.net/zm2714/article/details/8117746 .parent(selector) 获得当前匹配元素集合中每个元素的父元素,由选择器筛选( ...

  7. 哪些优秀的 Windows 小工具,类似 clover 或 everything

    有哪些优秀的 Windows 小工具,类似 clover 或 everything? 目前已知的有everything, listary, total commander, clover, dexpo ...

  8. idea intellij对Spring进行单元测试

    1.加入Junit4及SpringJUnit4支持 <!-- junit --> <dependency> <groupId>junit</groupId&g ...

  9. mysql 入门 jdbc

    在java程序中连接mysql,先要到mysql的网站上面去下载驱动,并且安装,默认安装在c盘(我的都是默认安装,目录为C:\Program Files\MySQL\MySQL Connector J ...

  10. css 特殊使用技巧

    1  border颜色设置 border-color: transparent black black black;   分别设置四条边框的颜色  上边transparent 透明无色 2  阴影 t ...