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缓存支持;

 

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}) 禁用特定的自动配置

@SpringBootApplication 注解等价于以默认属性使用 @Configuration,@EnableAutoConfiguration和 @ComponentScan。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值