• 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...
        }
    }
     
  • 相关阅读:
    GZIPInputStream 流未关闭引起的内存泄漏问题
    Java ExcutorService优雅关闭方式
    redis pipline
    LeetCode 30与所有单词相关联的字串
    Scala不使用null 而使用Option,None,Some的好处
    记录: 一次解决整型溢出攻击(使用scala,隐式转换)
    Scala 封装可break和continue的foreach循环
    记录: 百度webuploader 分片文件上传java服务器端(spring mvc)示例的优化
    HBase shell 中的十六进制数值表示
    关于getSystemResource, getResource 的总结
  • 原文地址:https://www.cnblogs.com/chengfengchi/p/14423079.html
Copyright © 2020-2023  润新知