zoukankan      html  css  js  c++  java
  • spring11

    IOC 操作 Bean 管理(基于注解方式)
    ​ 1、什么是注解

    ​ (1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值, 属性名称=属性值…)

    ​ (2)使用注解,注解作用在类上面,方法上面,属性上面

    ​ (3)使用注解目的:简化 xml 配置

    ​ 2、Spring 针对 Bean 管理中创建对象提供注解

    ​ 下面四个注解功能是一样的,都可以用来创建 bean 实例

    ​ (1)@Component

    ​ (2)@Service

    ​ (3)@Controller

    ​ (4)@Repository

    ​ 3、基于注解方式实现对象创建

    ​ 第一步 引入依赖 (引入spring-aop jar包)

    ​ 第二步 开启组件扫描

    <!--开启组件扫描
    1 如果扫描多个包,多个包使用逗号隔开
    2 扫描包上层目录
    -->
    <context:component-scan base-package="com.atguigu"></context:component-scan>
    1
    2
    3
    4
    5
    ​ 第三步 创建类,在类上面添加创建对象注解

    //在注解里面 value 属性值可以省略不写,
    //默认值是类名称,首字母小写
    //UserService -- userService
    @Component(value = "userService") //注解等同于XML配置文件:<bean id="userService" class=".."/>
    public class UserService {
    public void add() {
    System.out.println("service add.......");
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    ​ 4、开启组件扫描细节配置

    <!--示例 1
    use-default-filters="false" 表示现在不使用默认 filter,自己配置 filter
    context:include-filter ,设置扫描哪些内容
    -->
    <context:component-scan base-package="com.atguigu" use-defaultfilters="false">
    <context:include-filter type="annotation"

    expression="org.springframework.stereotype.Controller"/><!--代表只扫描Controller注解的类-->
    </context:component-scan>
    <!--示例 2
    下面配置扫描包所有内容
    context:exclude-filter: 设置哪些内容不进行扫描
    -->
    <context:component-scan base-package="com.atguigu">
    <context:exclude-filter type="annotation"

    expression="org.springframework.stereotype.Controller"/><!--表示Controller注解的类之外一切都进行扫描-->
    </context:component-scan>

  • 相关阅读:
    [转]利用EnteLib Unity Interception Extension和PIAB实现Trans
    [转]SynchronizationContext 综述
    【Java】JacksonUtils(Jackson ObjectMapper 工具类)
    【Java】GsonUtils(Gson 工具类)
    第十五章:指针类型
    第十六章:接口
    MySQL 的日志:binlog
    第八章:变量、常量和基础类型
    AUFS:多点合一,写时复制
    数据库多对多的几种形式(转载)
  • 原文地址:https://www.cnblogs.com/huaobin/p/14892023.html
Copyright © 2011-2022 走看看