随手记
get和post区别
- get产生一个数据包,post两个数据包
- get请求浏览器会把 HTTP header 和date一并发送,返回200
- post请求浏览器会先发送header,服务器响应100后,浏览器在继续发送date,返回200
Spring注解
@Configuration
修饰的类相当于xml中的beans,SignService即用@Bean注入spring容器
必须使用context:component-scanbase-package=”XXX”/扫描
@Configuration @EnableConfigurationProperties({DsscProperties.class}) public class DsscConfiguration { @Bean @ConditionalOnMissingBean public SignService signService(DsscProperties dsscProperties) { if (dsscProperties.isNeedSgin()) { if ("Itrus".equals(dsscProperties.getSupplier())) { return new ItrusSignServiceImpl(); } else if ("Net".equals(dsscProperties.getSupplier())) { return new NetSignServiceImpl(); } } return signDto -> "签名未启用"; } }
- @EnableConfigurationProperties
使 @ConfigurationProperties 注解修饰的类生效
@ConfigurationProperties
修饰范围==@Target({ ElementType.TYPE, ElementType.METHOD })==可以获取整个properties配置文件
- @Value
一般使用修饰字段,获取properties配置文件单个属性
- @ConditionalOnMissingBean
bean工厂有符合则不执行,无则注入修饰的bean
- @PostConstruct 和 @PreDestory
初始化和销毁bean之前进行的操作,只能有一个方法可以用此注解进行注释,方法不能有参数,返回值必需是void,方法需要是非静态的。
- @Primary
出现bean候选者的时候,被注解的作为首选者,
eg: 一个接口两个实现类,@Primay注解标注的为首选
- @Autowired和@Resource
@Autowired默认byType注入,如果spring上下文出现多个实例则搭配@Qualifier注解使用
@Autowired(required=false):表示忽略当前要注入的bean,如果有直接注入,没有跳过,不会报错
@Resource默认byName注入,找不到则byType。因为查两遍效率低
- @Qualifier
一般搭配@Autowired使用,使@Autowired具备byName的注入
- @Controller
@Component
@Repository
@Service
- @Service无指定则为类的全限定名(首字母小写)(com.server.server)
- @Service(“service”)指定则为指定值,即为别名
作用一样都是被spring容器发现注入,名字不一样
- @PathVariable
处理request url的时候加在入参前
- @ResponseStatus
- 修饰类时,一般修饰的是一个异常类,当处理器的方法被调用时@ResponseStatus指定的code和reason会被返回给前端
- 修饰方法时,该方法得到调用,不论是否抛异常,都会把value和reason添加到response里
- @ControllerAdvice
即把@ControllerAdvice注解内部使用@ExceptionHandler、@InitBinder、@ModelAttribute注解的方法, 应用到所有使用 @RequestMapping注解的方法