zoukankan      html  css  js  c++  java
  • spring 注解

    spring 注解

    设置xml配置文件

     
     
     
    xxxxxxxxxx
     
     
     
     
    <?xml version="1.0" encoding="UTF-8"?>
    <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
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd">
        <context:component-scan base-package="需要扫描的目录"/>
    </beans>
     

    官网:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core

    搜索“xmlns:context”

    添加到spring容器的注解

    (等效于“”)

    @Component(英文:组成部分;成分;组件,元件)

    作用:用于当前对象存入到spring容器中

    属性:value:用于指定bean的id 当我我们不写时默认是类名的首字母小写

     
     
     
    xxxxxxxxxx
     
     
     
     
    import org.springframework.stereotype.Component;
    @Component //或 @Compontent(value="id名称") 或 @Compontent("id名称")
    public class TestObj {
        private String Test;
    }
     

    @Service(英文:服务)

    一般用于业务层

    @Controller (英文:控制器)

    一般用于表现层

    @Repository (英文:贮藏室,仓库;知识库)

    一般用于持久层

    总结:以上4个注解等效


    属性注入

    @Autowirted (英文:自动装配)

    作用:自动按照类型注入 (只用容器内有唯一的一个类型与注入类型一样就可以)

    位置:可以是成员属性或属性方法上

    @Qualifier (英文:修饰语)

    必须和@Autowirted一起使用

    按照类型注入的情况下在按照名称

    @Resource (英文:资源,财力)

    直接按照bean的id注入可以单独使用 但是用name指定id 如:@Resource(name="名称")

    以上三个注解只能注入除基本类型和String类型的数据 另外 集合类型只能用xml注入


    @Value

    注入基本类型和String的类型数据

    @PostConstruct

    用于指定初始化方法

    @PreDestroy

    用于指定销毁方法 必须是单例

     
     
     
    xxxxxxxxxx
     
     
     
     
    public class CachingMovieLister {
        @PostConstruct //用于指定初始化方法
        public void populateMovieCache() {
            // populates the movie cache upon initialization...
        }
        @PreDestroy  //用于指定销毁方法
        public void clearMovieCache() {
            // clears the movie cache upon destruction...
        }
    }
     
  • 相关阅读:
    今天不谈技术,谈感情~
    外派金融公司
    OAuth2-简介
    Zookeeper实战-分布式锁
    Zookeeper-基础
    Minio-对象存储
    Redis实战-BloomFilter
    Redis实战-详细配置-优雅的使用Redis注解/RedisTemplate
    Redis-基础
    SpringBoot-表单验证-统一异常处理-自定义验证信息源
  • 原文地址:https://www.cnblogs.com/chengfengchi/p/14423079.html
Copyright © 2011-2022 走看看