本篇内容介绍了“Spring与JSR330中Solon Ioc注解对比分析”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
Solon 1.0.10 | Spring | JSR 330 | |
---|---|---|---|
@XInject * | @Autowired | @Inject | 注入Bean(by type) |
@XInject("name") | @Qualifier+@Autowired | @Qualifier+@Inject | 注入Bean(by name) |
@XInject("${name}") | @Value("${name}") | - | 注入配置 |
@XBean * | @Component | @Named | 托管Bean |
@XSingleton | @Scope(“singleton”) | @Singleton | 单例(Solon 默认是单例) |
@XSingleton(false) | @Scope(“prototype”) | - | 非单例 |
@XEvent | - | - | 内部事件订阅 |
@XInit * | @PostConstruct | - | 构造完成并注入后的初始化 |
@XConfiguration | @Configuration | - | 配置类 |
@XController | @Controller,@RestController | - | 控制器类 |
@XMapping | @RequestMapping,@GetMapping... | - | 映射 |
Solon 的 @XInject 算是: Spring 的@Value、@Autowired、@Qualifier 三者的结合,但又不完全等价
Solon 托管的 Bean 初始化顺序:new() - > @XInject - > @XInit -> Method@XBean
注1:Method@XBean,只执行一次(只在 @XConfiguration 里有效)
注2:@XInject 的参数注入,只在Method@XBean时有效
Solon 强调有节制的注解使用,尤其对于增加处理链路的都会节制。
@XBean(Bean的托管:一种基于name,一种基于类型;且只记录第一次的注册)
@XBean
public class UserService{
@Db("db1") //@Db为第三方扩展的注解
BaseMapper<User> mapper;
UserModel getUser(long puid){
return db1.selectById(puid);
}
}
/* @XBean("userService")
public class UserService{
@Db("db1")
BaseMapper<User> mapper;
UserModel getUser(long puid){
return db1.selectById(puid);
}
} */
@XController
@XSingleton(false) //非单例注解
@XController
public class UserController{
@XInject("${message.notnull}")
String message;
@XInject
UserService userService
@XMapping("/user/{puid}")
public Object user(Long puid){
if(puid == null){
return message;
}
return userService.getUser(puid);
}
}
@XConfiguration
@XConfiguration
public class Config {
@XBean("db1")
public DbContext db1(@XInject("${test.db1}") HikariDataSource dataSource) {
String schema = XApp.cfg().get("test.db1.schema");
return new DbContext(schema, dataSource);
}
}
@XEvent (使用事件监听时,要确保有人发起事件)
//系统异常监听(这个系统会发的,还可以监听不同的异常)
//
@XEvent(Throwable.class)
public class ThrowableListener implements XEventListener<Throwable> {
WaterLogger log = new WaterLogger("rock_log");
@Override
public void onEvent(Throwable err) {
XContext ctx = XContext.current();
if (ctx != null) {
String _in = ONode.stringify(ctx.paramMap());
log.error(ctx.path(), _in, err);
}
}
}
//Bean扩展监听(为Mybatis配置类,添加插件)
//
@XEvent(Configuration.class)
@XConfiguration
public class SqlHelperMybatisAutoConfiguration implements XEventListener<Configuration> {
//...
@Override
public void onEvent(Configuration configuration) {
SqlHelperMybatisPlugin plugin = new SqlHelperMybatisPlugin();
//...
configuration.addInterceptor(plugin);
}
}
“Spring与JSR330中Solon Ioc注解对比分析”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/noear/blog/4486811