zoukankan      html  css  js  c++  java
  • 注解的概述

    注解只是想替代传统xml配置文件的方式,注解这种方式更简便。

    注解的配置步骤:applicationContext.xml
    1、创建约束:
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->

    </beans>

    2、在applicationContext.xml配置文件中开启组件扫描
    <context:component-scan base-package="com.itheima.demo1"/>
    上面的配置意思是扫描com.itheima.demo1包下面的所有类。

    3、在类名上面添加注解:
    @Component(value="userService")
    -- 相当于在XML的配置方式中 <bean id="userService" class="...">

    ------------------------------------------------
    其他注解: (功能目前来讲都是一致的)
    * @Controller -- 作用在WEB层
    * @Service -- 作用在业务层
    * @Repository -- 作用在持久层
    上面几种都是写在类上面的注解,也有可以写在属性上面的注解:
    如果是注入的普通类型,可以使用value注解
    @Value -- 用于注入普通类型

    如果注入的是对象类型,使用如下注解
    @Autowired -- 默认按类型进行自动装配
    如果想按名称注入
    @Qualifier -- 强制使用名称注入

    @Resource -- 相当于@Autowired和@Qualifier一起使用
    强调:Java提供的注解
    属性使用name属性


    Bean的作用范围和生命周期的注解**

    Bean的作用范围注解
    注解为@Scope(value="prototype"),作用在类上。值如下:
    singleton -- 单例,默认值
    prototype -- 多例

    下面的是加载工厂的注解:
    在类上面使用,简化的加载工厂的代码,只要写一次,以后在这个类里面就不用再去加载了,方便我们以后
    去做测试
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext.xml")



  • 相关阅读:
    Windows SDK编程(Delphi版) 之 应用基础,楔子
    一个小问题引发的论证思考
    Delphi 组件开发教程指南(7)继续模拟动画显示控件
    用PyInstaller将python转成可执行文件exe笔记
    使用 .Net Memory Profiler 诊断 .NET 应用内存泄漏(方法与实践)
    Microsof Office SharePoint 2007 工作流开发环境搭建
    How to monitor Web server performance by using counter logs in System Monitor in IIS
    LINQ之Order By
    window 性能监视器
    内存泄露检测工具
  • 原文地址:https://www.cnblogs.com/tidhy/p/6740614.html
Copyright © 2011-2022 走看看