常用注解与 SpringBoot 的对比
1、注解对比
Solon 1.12.3 | Springboot 2.3.3 | 说明 |
---|---|---|
@Inject * | @Autowired | 注入Bean(by type) |
@Inject("name") | @Qualifier+@Autowired | 注入Bean(by name) |
@Inject("${name}") | @Value("${name}") | 注入配置 |
@Singleton | @Scope(“singleton”) | 单例(Solon 默认是单例) |
@Singleton(false) | @Scope(“prototype”) | 非单例 |
@Import | @Import + @ComponentScan | 配置组件导入或扫描(一般加在启动类上) |
@PropertySource | @PropertySource | 配置属性源(一般加在启动类上) |
@Configuration | @Configuration | 配置类 |
@Bean | @Bean | 配置Bean |
@Controller | @Controller,@RestController | 控制器类(有类代理) |
@Remoting | 远程控制器类(有类代理;即RPC服务端) | |
@Mapping ... | @RequestMapping,@GetMapping... | 映射 |
@Param | @RequestParam | 请求参数 |
@Header | @RequestHeader | 请求头 |
@Cookie | @CookieValue | 请求Cookie |
@Body | @RequestBody | 请求体 |
@Component | @Component | 普通托管组件 |
@Service | @Service | 服务托管组件(有类代理) |
@Dao | @Dao | 数据访问托管组件(有类代理) |
@Repository | @Repository | 仓库托管组件(有类代理;DDD里的概念类) |
@Init * | @PostConstruct | 组件构造完成并注入后的初始化 |
@TestPropertySource | @TestPropertySource | 配置测试属性源 |
@TestRollback | @TestRollback | 执行测试回滚 |
2、补充说明
- Solon 的 @Inject 算是: Spring 的@Value、@Autowired、@Qualifier 三者的结合,但又不完全等价
- Solon 的 @Import 算是:Spring 的@Import、@ComponentScan 两者的结合,即有导入功能也有扫描功能
- Solon 托管的 Bean 初始化顺序:new() - > @Inject - > @Init
- 注1:Method@Bean,只执行一次(只在 @Configuration 里有效)
- 注2:@Inject 的参数注入,只在 Method@Bean 上有效
- 注3:@Inject 的类注入,只在 @Configuration类 上有效
- 注4:@Import 只在 主类上 或者 @Configuration类 上有效