Spring 各种注解备注
Spring 各种注解备注
转载 (http://blog.csdn.net/sudiluo_java/article/details/51858978)
@ConditionalOnBean 配置了某个特定Bean
@ConditionalOnMissingBean 没有配置特定的Bean
@ConditionalOnClass Classpath里有指定的类
@ConditionalOnMissingClass Classpath里缺少指定的类
@ConditionalOnExpression 给定的Spring Expression Language(SpEL)表达式计算结果为true
@ConditionalOnJava Java的版本匹配特定值或者一个范围值
@ConditionalOnJndi 参数中给定的JNDI位置必须存在一个,如果没有给参数,则要有JNDI
InitialContext
@ConditionalOnProperty 指定的配置属性要有一个明确的值
@ConditionalOnResource Classpath里有指定的资源
@ConditionalOnWebApplication 这是一个Web应用程序
@ConditionalOnNotWebApplication 这不是一个Web应用程序
声明Bean的注解:
@Component : 组件,没有明确的角色
@Service : 在业务逻辑层(service层)使用
@Repository : 在数据访问层(dao层)使用.
@Controller : 在展现层(MVC--SpringMVC)使用
注入Bean的注解:
@Aautowired : Spring提供的注解.
@Inject : JSR-330提供的注解
@Resource : JSR-250提供的注解
@Primary 注入时优先使用此注解下的实现类,对应org.springframework.beans.factory.NoUniqueBeanDefinitionException异常
配置文件的注解:
@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 注解,以明确指出该类是 Bean 配置的信息源
@ComponentScan 注解会告知Spring扫描指定的包来初始化Spring Bean这能够确保我们声明的Bean能够被发现。
@ImportResource 注解加载XML配置文件
@EnableAutoConfiguration (exclude={xxxx.class}) 禁用特定的自动配置
@SpringBootApplication 注解等价于以默认属性使用 @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
-------Mr.Ge提供以上资源
Spring 各种注解备注的更多相关文章
- 使用轻量级Spring @Scheduled注解执行定时任务
WEB项目中需要加入一个定时执行任务,可以使用Quartz来实现,由于项目就一个定时任务,所以想简单点,不用去配置那些Quartz的配置文件,所以就采用了Spring @Scheduled注解来实现了 ...
- Spring MVC注解的一些案列
1. spring MVC-annotation(注解)的配置文件ApplicationContext.xml <?xml version="1.0" encoding=& ...
- Spring系列之Spring常用注解总结
传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...
- spring @condition 注解
spring @condition注解是用来在不同条件下注入不同实现的 demo如下: package com.foreveross.service.weixin.test.condition; im ...
- spring mvc(注解)上传文件的简单例子
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- Spring的注解方式实现AOP
Spring对AOP的实现提供了很好的支持.下面我们就使用Spring的注解来完成AOP做一个例子. 首先,为了使用Spring的AOP注解功能,必须导入如下几个包.aspectjrt.jar,asp ...
- Spring 之注解事务 @Transactional
众所周知的ACID属性: 原子性(atomicity).一致性(consistency).隔离性(isolation)以及持久性(durability).我们无法控制一致性.原子性以及持久性,但可以 ...
- 数据库事务中的隔离级别和锁+spring Transactional注解
数据库事务中的隔离级别和锁 数据库事务在后端开发中占非常重要的地位,如何确保数据读取的正确性.安全性也是我们需要研究的问题.ACID首先总结一下数据库事务正确执行的四个要素(ACID): 原子性(At ...
- Spring JSR-250注解
Java EE5中引入了“Java平台的公共注解(Common Annotations for the Java Platform)”,而且该公共注解从Java SE 6一开始就被包含其中. 2006 ...
随机推荐
- win2008 配置TLS1.2
配置TLS1.2 提供两种方法, 选择其中一种就行了 1.手动设置 找到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProv ...
- Python基础学习之字符串(2)
字符串常用方法 1.s.capitalize() 描述:返回字符串s的副本,并将首字符变为大写. 示例: >>> s='yesterday when I was Young!' &g ...
- vs2008使用mysql链接错误
原因是因为安装了64位的mysql,而开发工具室32位的,需要安装32位的开发库就可以了
- Selenium入门系列3 单个元素的定位方法
UI自动化首先要识别对象,再操作对象,最后判定实际结果与预期结果是否一致. 这一节学习的是识别单个对象,webdriver提供了8种方式. <a id="idofa" cla ...
- IOS instancetype的使用好处
instancetype的类型表示上,跟id一样,可以表示任何对象类型 instancetype只能用在返回值类型上,不能像 id 一样用在参数类型上 instancetype 比 id 多一个好处 ...
- Unable to launch the Java Virtual Machine
看看国内的回答,http://zhidao.baidu.com/question/119993351.html 再看看国外的,http://www.mkyong.com/oracle/oracle-s ...
- image retrieval数据集
1. Oxford,vgg组,主要是building方面的数据.http://www.robots.ox.ac.uk/~vgg/data/oxbuildings/index.html 2. Calte ...
- javascript入门笔记3-dom
1.通过ID获取元素 document.getElementById("id") <!DOCTYPE HTML> <html> <head> & ...
- 网际协议 IP
网际协议 网际协议(internet protocol),简称IP; 概念:TCP/IP网络体系结构中网际层的协议.用以提供无连接的数据服务. 1.IP地址的概念及组成 概念:IP地址就是用来唯一标 ...
- mysql基础 日期类型