注入值注解
目录
在 imi 框架中,提供了一类特殊的注解,称为“注入值注解”,这些注解允许将值动态注入到注解的属性中。
通过这种方式,可以在运行时动态地将一些值注入到注解属性中,而不是在编写代码时硬编码这些值。这样可以更加灵活地配置代码的行为,减少硬编码造成的代码耦合和维护成本。
注解说明
@ConstValue
从常量中读取值
| 属性名称 | 说明 |
|---|---|
| name | 常量名 |
| default | 常量不存在时,返回的默认值 |
@ConfigValue
从配置中读取值
| 属性名称 | 说明 |
|---|---|
| name | 配置名,支持@app、@currentServer等用法 |
| default | 配置不存在时,返回的默认值 |
@EnvValue
从环境变量中读取值
| 属性名称 | 说明 |
|---|---|
| name | 环境变量名称 |
| default | 配置不存在时,返回的默认值 |
@Inject
对象注入,使用:App::getBean()
| 属性名称 | 说明 |
|---|---|
| name | Bean名称或类名 |
| args | Bean实例化参数 |
@RequestInject
对象注入,使用:RequestContext::getBean()
同@Inject
@Callback
回调注解
| 属性名称 | 说明 |
|---|---|
| class | 类名,或者传入对象,比如可以使用 @Inject、@RequestInject 再次值注入 |
| method | 方法名 |
用法示例
/*
* @Cacheable(
* key="index:{page}",
* ttl=10,
* lockable=@Lockable(
* id="index:{page}",
* waitTimeout=999999,
* ),
* preventBreakdown=true,
* )
*/@DbInject
注入数据库对象
| 属性名称 | 说明 |
|---|---|
| name | 连接池名,如果为null则取配置@app.db.defaultPool |
| queryType | 查询类型,影响读写分离逻辑。可选:QueryType::READ/QueryType::WRITE,默认为QueryType::WRITE |
@RedisInject
注入Redis对象
| 属性名称 | 说明 |
|---|---|
| name | 连接池名,如果为null则取配置@app.redis.defaultPool |
@PoolResource
注入连接池资源
| 属性名称 | 说明 |
|---|---|
| name | 连接池名 |
