[TOC]
spring
Bean
bean的注册
两种办法.
一、@Component或其派生 @Controller, @Service, @Repository注解
位置: 注解在类上
参数: 默认的bean的ID为首字母变为小写的类名
作用域:
通过上述四个注解标识的 Bean,其默认作用域是”singleton”。
如果不想是单例,使用@Scope 注解。只需提供作用域的名称就行了。
1
2
3"prototype") (
public class Demo { … }
@ComponentScan
- 作用: @ComponentScan对应XML配置形式中的<context: component-scan>元素, 用于配合一些元信息 Java Annotation,比如@Component和@Repository等, 将标注了这些元信息 Annotation 的 bean 定义类批量采集到 Spring 的IoC容器中, 我们可以通过 basePackages等属性来细粒度地定制 @ComponentScan自动扫描的范围, 如果不指定, 则默认 Spring框架实现会从声明 @ComponentScan所在类的 package 进行扫描。
二、使用@Configuration 搭配 @Bean注解。
常用在配置第三方提供的bean.
- @Configuration
- 位置: 注解在类上
- 作用: 声明当前类是一个配置类,相当于一个Spring配置的xml文件, 任何POJO+@Configuration都是一个JavaConfig配置类。 @Configuration也是一个@Component
- @Bean
- 位置: 注解在方法上
- 作用: 声明当前方法的返回值为一个Bean。
- @Configuration
1 | // 使用 @Configuration + @Bean 配置一个Tesla Service |
bean的获取
@Autowired 搭配 @Qualifier 或者 @Resource
@Resource和@Autowired使用起来区别不大, @Autowired优先byType装配, @Resource优先byName装配, 更多参考这里
@Autowired(required = false)找不到也没关系
问:如果属性注入找不到我不想让Spring容器抛出异常,而就是显示null,可以吗?答:将@Autowired注解的required属性设置为false即可
1 | ` false) (required = |
@Qualifier(“BMW”)接口有多个实现拿哪一个
问:一个Car接口有两个个实现,类Benz和BMW,要怎么拿?
1 |
|
@Autowired + @Qualifier(“foo”) 等同于 @Resource(name=”foo”)
bean生命周期
在某些情况下,可能需要我们手工做一些额外的初始化或者销毁操作,这通常是针对一些资源的获取和释放操作。
Bean的生命周期 @Scope @PostConstruct 和 @PreDestroy
@Scope指定bean的作用域.
1 | @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) or @Scope("prototype") |
生命周期的钩子
- initMethod & destroyMethod
- 位置: 与@Bean搭配使用
- 作用: 指定bean的生命周期回调方法, bean实例化后, bean销毁前
- 例子: @Bean(initMethod=”init”,destroyMethod=”destory”)
- @PostConstruct & @PreDestroy
- 位置: 与@Component搭配使用, 注解在方法上
- 作用: 指定bean的生命周期回调方法, bean实例化后, bean销毁前
- 例子: @PostConstruct public void init() {}
1 | // @PostConstruct & @PreDestroy 示例 |
@Value去配置文件取值
1 | "${env}") ( |
除了spring的application.properties,还会发现应用里对每一个环境都有不同的配置文件,诸如dev_config.properties、online_config.properties。
@Value取值先去application.properties,如果配置文件以@开头结尾,如env=@env@,则根据当前环境去对应的配置文件里取
SpringBoot
- @SpringBootApplication
- @Configuration
- 启动类标注了@Configuration之后,本身其实也是一个IoC容器的配置类
- @EnableAutoConfiguration
- 从classpath中搜索所有META-INF/spring.factories配置文件, 加载配置到spring容器
- 比如moguboot的spring.factories就配置了很多内部中间件
- @ComponentScan
- @Configuration
SpringMVC
- @RequestParam & @PathVariable 做参数绑定
- 默认绑定
- 请求参数名和方法参数名一致
- 使用RequestParam明确指定绑定关系
- @RequestParam(value=”age” required=false) required默认是true
- @PathVariable
- @RequestMapping(value=”users/{userId}, method=RequestMethod.GET)
- foo(@PathVariable(“userId”) long userId)
- 默认绑定
- @RequestBody
- 添加 @RequestBody 后 Spring 会根据请求中的 Content-Type 头信息来选择合适的转换器, 将请求数据转为 Java 对象
- 比如Content-Type是application/json, 那么就是 JSON -> Model
- @ResponseBody
- 添加 @ResponseBody 后 Spring 会根据请求中的 Accept 头信息来选择合适的转换器, Java 对象转化为客户端可接受的表述形式
- 比如Accept头部信息包含“application/json”, 就是Model -> JSON
- @RestController
- @RestController 等于 @Controller + 每个方法默认加@ResponseBody
Lombok
@Data,相当于*@Getter+ @Setter+ toString + EqualsAndHashCode + RequiredArgsConstructor.
@ builder 一个静态内部类https://juejin.im/post/5cfdf467f265da1b5d579fdd
@EqualsAndHashCode(callSuper = true)
测试
- @Test
- @Test注解标注这个方法需要测试
- @Before
- 所有的测试方法之前都先执行这个方法
- @After
- 所有的测试方法之后都要执行这个方法
- 还有@BeforeClass @AfterClass等注解
1 |
|