1. @FeignClient
在入口程序通过@EnableFeignClients开启
https://www.cnblogs.com/moonandstar08/p/7565442.html
2. @Slf4j
可直接使用log对象输出日志,代替
备注:如果找不到log对象,可能是idea没有安装lombok plugins引起的。>
private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);
3. @component
把普通pojo实例化到spring容器中,相当于配置文件中的
<bean id="" class=""/>
泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类。
4. @Scheduled
定时任务,使用Cron表达式。
在入口程序添加注解
@EnableScheduling告诉springboot我们需要用定时任务完成业务逻辑。
@Scheduled拥有四个属性:cron、fixedRate、fixedDelay、initialDelay。
参考:https://www.jianshu.com/p/c7492aeb35a1
5. @PostConstruct、@PreDestroy
1.@PostConstruct说明
被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。
2.@PreDestroy说明
被@PreDestroy修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。被@PreDestroy修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前。
参考: https://www.cnblogs.com/landiljy/p/5764515.html
https://www.jianshu.com/p/98cf7d8b9ec3
6. @SpringBootApplication
@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan
参考:https://www.cnblogs.com/MaxElephant/p/8108140.html
7. @NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor
基于lambok,分别表示:无参构造器、部分参数构造器、全参构造器。
Lombok没法实现多种参数构造器的重载。
8. @CrossOrigin
@CrossOrigin允许跨域使用在@RequestMapping注解中指定的源和HTTP方法
9. @Controller 与 @RestController
使用@Controller注解,视图解析器可以解析return的html和jsp页面,并跳转
使用@Controller+@Response,解析器可以返回json内容到页面
@RestController相当于@Controller+@Response,但视图解析器无法解析html和jsp页面。
10. @Reference 与 @Service
springboot提供:
dubbo中的@Reference 用在消费端,表明使用的是服务端的什么服务(等价于@autowired + consumer.xml)
dubbo中的@Service 用在服务提供者中,在类或者接口中声明。服务提供者实现相关的服务接口,当消费端调用相关的类时,最终会调用提供者的实现方法。
需要通过@EnableDubbo注释启动。
参考:https://www.cnblogs.com/lgjlife/p/10225271.html
dubbo在controller中reference注解为空的问题深度解析
参考:https://blog.youkuaiyun.com/zhou_java_hui/article/details/53039491
11. @EnableDubbo 与 @EnableDubboConfig
@EnableDubbo = @EnableDubboConfig + @DubboComponentScan
//eg.
@EnableDubbo (scanBasePackages = "com.alibaba.dubbo.samples.impl")
//等价于
@EnableDubboConfig
@DubboComponentScan("com.alibaba.dubbo.samples.impl")
参考:http://dubbo.apache.org/zh-cn/docs/user/demos/multi-versions.html
12. @ConfigurationPropertis
将类与.yml文件绑定
@ConfigurationPropertis要生效,一定要是spring组件,所以要加@Component注解
当通过@Autowired自动注入使用时,就会携带yml配置文件中的值。
13. @Value
单一属性的配置
与@ConfigurationPropertis区别
@ConfigurationPropertis | @Value | 说明 | |
---|---|---|---|
功能 | 批量注入属性 | 注入一条属性 | |
松散绑定 | 支持 | 不支持 | |
SpEL | 不支持 | 支持 | Spring表达式 |
JSR303数据校验 | 支持 | 不支持 | 如@NotNull, @Email |
复杂类型 | 支持 | 不支持 | 如object.list |
14. @PropertySource
导入指定的.properties文件
@PropertySource(value = {"classpath:person.properties"}) //例子
15. @ImportResource
导入指定的.xml文件
@ImportResource(locations= {"classpath:beans.xml"}) //例子
16. @Bean
将方法返回值加入Spring容器中,用于配置类。
@Configuration
public class DemoConfig{
// 将方法返回值加入容器中,容器中组件的默认id就是方法名。
@Bean
public MyService myService(){
return new MyService();
}
}