说明写在最前面:摘录于   博客园--受伤滴小萝卜   文章

文章链接受伤滴小萝卜文章——Spring注解

本文章只用作学习和帮助其他人学习记录使用

Spring 注解学习笔记

声明Bean的注解:
  • @Component : 组件,没有明确的角色
  • @Service : 在业务逻辑层(service层)使用
  • @Repository : 在数据访问层(dao层)使用.
  • @Controller : 在展现层(MVC--SpringMVC)使用

注入Bean的注解:
  • @Aautowired : Spring提供的注解.
  • @Inject : JSR-330提供的注解
  • @Resource : JSR-250提供的注解

配置文件的注解:
  • @Configuration : 声明当前类是个配置类,相当于一个Spring配置的xml文件.
  • @ComponentScan (cn.test.demo):
    自动扫描包名下所有使用 @Component @Service  @Repository @Controller 的类,并注册为Bean
  • @WiselyConfiguration :
    组合注解 可以替代 @Configuration和@ComponentScan
  • @Bean : 注解在方法上,声明当前方法的返回值为一个Bean.
    • @Bean(initMethod="aa",destroyMethod="bb")-->
      指定 aa和bb方法在构造之后.Bean销毁之前执行.

AOP切面编程注解:
  • @Aspect : 声明这是一个切面
  • @After @Before. @Around
    定义切面,可以直接将拦截规则(切入点 PointCut)作为参数
  • @PointCut : 专门定义拦截规则 然后在 @After
    @Before. @Around 中调用
  • @Transcational : 事务处理
  • @Cacheable : 数据缓存
  • @EnableAaspectJAutoProxy
    : 开启Spring 对 这个切面(Aspect )的支持
  • @Target (ElementType.TYPE):元注解,用来指定注解修饰类的那个成员
    -->指定拦截规则
  • @Retention(RetentionPolicy.RUNTIME)
    • --->当定义的注解的@Retention为RUNTIME时,才能够通过运行时的反射机制来处理注解.-->指定拦截规则

Spring 常用配置:
  • @import :导入配置类
  • @Scope : 新建Bean的实例 @Scope("prototype")
    声明Scope 为 Prototype
  • @Value : 属性注入
    • @Value ("我爱你")  --> 普通字符串注入
    • @Value ("#{systemProperties['os.name']}")
      -->注入操作系统属性
    • @Value ("#{ T (java.lang.Math).random()
       * 100.0 }") --> 注入表达式结果
    • @Value ("#{demoService.another}")
      --> 注入其他Bean属性
    • @Value ( "classpath:com/wisely/highlight_spring4/ch2/el/test.txt"
      ) --> 注入文件资源
    • @Value ("http://www.baidu.com")-->注入网址资源
    • @Value ("${book.name}"
      ) --> 注入配置文件  注意: 使用的是$ 而不是 #
  • @PostConstruct : 在构造函数执行完之后执行
  • @PreDestroy  : 在 Bean 销毁之前执行
  • @ActiveProfiles : 用来声明活动的
    profile
  • @profile: 为不同环境下使用不同的配置提供了支持
    • @Profile("dev") .......对方法名为
      dev-xxxx的方法提供实例化Bean
  • @EnableAsync : 开启异步任务的支持(多线程)
  • @Asyns : 声明这是一个异步任务,可以在类级别
    和方法级别声明.
  • @EnableScheduling : 开启对计划任务的支持(定时器)
  • @Scheduled : 声明这是一个计划任务
    支持多种计划任务,包含 cron. fixDelay fixRate
    • @Scheduled (dixedDelay
      = 5000) 通过注解 定时更新
  • @Conditional : 条件注解,根据满足某一特定条件创建一个特定的Bean
  • @ContextConfiguration :
    加载配置文件
    • @ContextConfiguration(classes
      = {TestConfig.class})
    • @ContextConfiguration用来加载ApplicationContext
    • classes属性用来加载配置类
  • @WebAppCofiguration : 指定加载
    ApplicationContext是一个WebApplicationContext
@Enable*注解:
  • @EnableAsync : 开启异步任务的支持(多线程)
  • @EnableScheduling : 开启对计划任务的支持(定时器)
  • @EnableWebMVC : 开启对Web
    MVC 的配置支持
  • @EnableAaspectJAutoProxy
    : 开启Spring 对 这个切面(Aspect )的支持
  • @EnableConfigurationProperties
    开启对@ConfigurationProperties注解配置Bean的支持
  • @EnableJpaRepositories
    : 开启对Spring Data JAP Repository 的支持
  • @EnableTransactionManagement
    开启对注解式事物的支持
  • @EnableCaching开启注解是缓存的支持.
  • @EnableDiscoveryClient
    让服务发现服务器,使用服务器.Spring cloud 实现服务发现
  • @EnableEurekaServer 注册服务器
    spring cloud 实现服务注册@
  • @EnableScheduling 让spring可以进行任务调度,功能类似于spring.xml文件中的命名空间<task:*>
  • @EnableCaching 开启Cache缓存支持;
SpringMVC 常用注解:
  • @Controller : 注解在类上 声明这个类是springmvc里的Controller,将其声明为一个spring的Bean.
  • @RequestMapping :可以注解在类上和方法上
    映射WEB请求(访问路径和参数)
    • @RequestMapping(value=
      "/convert",produces+{"application/x-wisely"}) 设置访问URL 返回值类型
  • @ResponseBody : 支持将返回值放入response体内
    而不是返回一个页面(返回的是一个组数据)
  • @RequestBody : 允许request的参数在request体中,而不是直接连接在地址后面
    次注解放置在参数前
  • @Path Variable : 用来接收路径参数
    如/test/001,001为参数,次注解放置在参数前
  • @RestController : @Controller
    + @ResponseBody 组合注解
  • @ControllerAdvice : 通过@ControllerAdvice可以将对已控制器的全局配置放置在同一个位置
  • @ExceptionHandler : 用于全局处理控制器的异常
    • @ExceptionHandier(value=Exception.class)
      -->通过value属性可过滤拦截器条件,拦截所有的异常
  • @InitBinder : 用来设置WebDataBinder
    , WebDataBinder用来自动绑定前台请求参数到Model中.
  • @ModelAttrbuute : 绑定键值对到Model中,
  • @RunWith : 运行器
    • @RunWith(JUnit4.class)就是指用JUnit4来运行
    • @RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境
    • @RunWith(Suite.class)的话就是一套测试集合,
  • @WebAppConfiguration("src/main/resources")
    : 注解在类上,用来声明加载的ApplicationContex 是一个WebApplicationContext ,它的属性指定的是Web资源的位置,默认为 src/main/webapp ,自定义修改为 resource
  • @Before :
    在 xxx 前初始化

Spring Boot 注解:
  • @SpringBootApplication
    : 是Spring Boot 项目的核心注解 主要目的是开启自动配置
    • @SpringBootApplication注解是一个组合注解,主要组合了@Configuration
      .+@EnableAutoConfiguration.+@ComponentScan
  • @Value : 属性注入,读取properties或者
    Yml 文件中的属性
  • @ConfigurationProperties
    : 将properties属性和一个Bean及其属性关联,从而实现类型安全的配置
    • @ConfigurationProperties(prefix
      = "author",locations = {"classpath:config/author.properties"})
    • 通过@ConfigurationProperties加载配置,通过prefix属性指定配置前缀,通过location指定配置文件位置
  • @EnableAutoConfiguration
    注解:作用在于让 Spring Boot   根据应用所声明的依赖来对 Spring 框架进行自动配置

            这个注解告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring。由于spring-boot-starter-web添加了Tomcat和Spring MVC,所以auto-configuration将假定你正在开发一个web应用并相应地对Spring进行设置。
  • @ Configuration @EnableAutoConfiguration (exclude={xxxx.class})
    禁用特定的自动配置
  • @Configuration,@EnableAutoConfiguration和
        @ComponentScan。


@SuppressWarnings注解
  • @SuppressWarnings("unchecked")
    • 告诉编译器忽略 unchecked 警告信息,如使用
      list ArrayList等未进行参数化产生的警告信息
  • @SuppressWarnings("serial")
    • 如 果编译器出现这样的警告信息: The
      serializable class WmailCalendar does not declare a static final serialVersionUID field of type long     使用这个注释将警告信息去掉。
  • @SuppressWarnings("deprecation")
    • 如果使用了使用@Deprecated注释的方法,编译器将出现警告信息。使用这个注释将警告信息去掉。
  • @SuppressWarnings("unchecked",
    "deprecation")
    • 告诉编译器同时忽略unchecked和deprecation的警告信息。
  • @SuppressWarnings(value={"unchecked",
    "deprecation"})
    • 等同于@SuppressWarnings("unchecked",
      "deprecation")
案例

@Entity
@Table(name
= "S_PRODUCEINFO" )
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProduceInfoEntity
{

    @Id
    @Column(name
= "app_name", unique
= true,
length = 50)
    private String name;

    @Column(name
= "status")
    @Enumerated(EnumType. STRING)
    private ProduceStatus status;

    @Column(name
= "create_time", updatable
= false)
    @Temporal(TemporalType. TIMESTAMP)
    @CreationTimestamp
    private Date createTime;

    @Column(name
= "update_time")
    @Temporal(TemporalType. TIMESTAMP)
    @UpdateTimestamp
    private Date updateTime;
@Entity
: 映射数据库实体类
@Table(name
= "S_PRODUCEINFO" )
: 表名为 "S_PRODUCEINFO"
@Id : 声明主键ID
@Column(name
= "app_name", unique
= true,
length = 50) :对应数据库字段,属性
@Enumerated(EnumType. STRING)
: 采用枚举值类型和数据库字段进行交互 
@Temporal : 时间格式 映射数据库会得到规定时间格式的日期
       @Enumerted(EnumType.STRING)
 HH:MM:SS 格式的日期
       @Enumerted(EnumType.DATE)
获取年月日  yyyy-MM-dd 

@Enumerted(EnumType.TIME)
获取时分秒  HH:MM:SS

Spring的相关注解的更多相关文章

  1. Spring中@相关注解的意义

    1.@controller 控制器(注入服务) 用于标注控制层,相当于struts中的action层 2.@service 服务(注入dao) 用于标注服务层,主要用来进行业务的逻辑处理 3.@rep ...

  2. spring注入相关注解

    本次主要整理四个注解 @ComponentScan.@Scope.@Conditional.@Import 1. @ComponentScan(value = "com.xiaoguo&qu ...

  3. Spring bean依赖注入、bean的装配及相关注解

    依赖注入 Spring主要提供以下两种方法用于依赖注入 基于属性Setter方法注入 基于构造方法注入 Setter方法注入 例子: public class Communication { priv ...

  4. spring boot @ConditionalOnxxx相关注解总结

    Spring boot @ConditionalOnxxx相关注解总结 下面来介绍如何使用@Condition public class TestCondition implements Condit ...

  5. spring相关注解

    spring相关注解: 使用之前需要<context:annotation-config/>在配置文件中启用 @Required 应用于类属性的set方法,并且要求该属性必须在xml容器里 ...

  6. 利用spring AOP 和注解实现方法中查cache-我们到底能走多远系列(46)

    主题:这份代码是开发中常见的代码,查询数据库某个主表的数据,为了提高性能,做一次缓存,每次调用时先拿缓存数据,有则直接返回,没有才向数据库查数据,降低数据库压力. public Merchant lo ...

  7. SpringMVC4 + Spring + MyBatis3 基于注解的最简配置

    本文使用最新版本(4.1.5)的springmvc+spring+mybatis,采用最间的配置方式来进行搭建. 1. web.xml 我们知道springmvc是基于Servlet: Dispatc ...

  8. Spring 的@Scheduled注解实现定时任务运行和调度

    Spring 的@Scheduled注解实现定时任务运行和调度 首先要配置我们的spring.xml   ---  即spring的主配置文件(有的项目中叫做applicationContext.xm ...

  9. Spring笔记04_AOP注解开发_模板_事务

    目录 1. Spring基于AspectJ的注解的AOP开发 1. 1 SpringAOP的注解入门 1.2 Spring的AOP的注解通知类型 1.2.1 @Before:前置通知 1.2.2 @A ...

随机推荐

  1. 第三十四章 POSIX消息队列

    POSIX消息队列相关函数 mq_open 功能: 用来创建和访问一个消息队列 原型: mqd_t mq_open(const char *name, int oflag); //只能用来打开消息队列 ...

  2. centos创建kvm虚拟机

    1.检查kvm模块是否已经加载 lsmod |grep kvm 上图已经加载 没有加载 2.加载kvm 模块至内核 modprobe kvm modprobe kvm-intel modprobe: ...

  3. 一文教您如何通过 Java 压缩文件,打包一个 tar.gz Filebeat 采集器包

    欢迎关注笔者的公众号: 小哈学Java, 专注于推送 Java 领域优质干货文章!! 个人网站: https://www.exception.site/essay/create-tar-gz-by-j ...

  4. 『题解』[NOI2016]优秀的拆分

    如果一个字符串可以被拆分为\(AABB\)的形式,其中$A和 B是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串\(aabaabaa\),如果令\(A=aab\),\(B=a\ ...

  5. JDBC报错:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone

    报错原因:查阅资料发现这都是因为安装mysql的时候时区设置的不正确 mysql默认的是美国的时区,而我们中国大陆要比他们迟8小时,采用+8:00格式 解决方法: 1.修改MySQL的配置文件,MyS ...

  6. OpenCV的Mat构造函数

    1.函数说明 构造函数:public Mat(int rows, int cols, MatType type, IntPtr data, long step = 0) 可以通过数据指针构造Mat对象 ...

  7. 更新linux时候提示“由于没有公钥,无法验证下列签名".

    本文链接:https://blog.csdn.net/loovejava/article/details/21837935 新安装的Ubuntu在使用sudo apt-get update更新源码的时 ...

  8. Jetpack Compse 实战 —— 全新的开发体验

    公众号回复 Compose 获取安装包 项目地址: Wanandroid-Compose 经过前段时间的 Android Dev Summit ,相信你已经大概了解了 Jetpack Compose ...

  9. 深入理解计算机系统 第九章 虚拟内存 Part1 第二遍

    这次花了4小时40分钟,看了第 559~575 页,共 17 页 第一遍对应地址 https://www.cnblogs.com/stone94/p/10264044.html 注意:本章的练习题一定 ...

  10. 爬虫实践--CBA历年比赛数据

    闲来无聊,刚好有个朋友来问爬虫的事情,说起来了CBA这两年的比赛数据,做个分析,再来个大数据啥的.来了兴趣,果然搞起来,下面分享一下爬虫的思路. 1.选取数据源 这里我并不懂CBA,数据源选的是国内某 ...