CDI 1.1 - 在 ApplicationScope 中注入依赖范围
CDI 1.1 - Inject Dependent scope in ApplicationScope
假设我有一个 @ApplicationScoped
服务 class。
@ApplicationScoped
class MyCustomerService {
...
}
然后我想将 Connection
对象注入该服务。
@ApplicationScoped
class MyCustomerService {
private final Connection connection;
@Inject
MyCustomerService(final Connection connection) {
this.connection = connection;
}
...
}
Connection
对象由 @Produces
方法使用数据源生成。
class ConnectionProducer {
...
@Produces
Connection getConnection(final DataSource dataSource) {
return dataSource.getConnection();
}
...
}
Connection
class 会被代理吗?每次我使用 connection
bean 时 CDI 都会调用生产者方法吗(不像 RequestScoped,我的意思是每次调用)?
我知道我可以 @Inject
DataSource
,这就是 "learning" CDI 管理作用域的方式。
Will CDI invoke the producer method each time I use the connection bean
没有。 producer 方法被调用一次,因为默认范围是 Dependent 。您的连接生命周期受限于 MyCustomerService 生命周期
Will the Connection class be proxied
如果 bean 在 @Dependent 范围内,那么客户端持有对其实例的直接引用(clientproxy 只是为 NormalScope 创建的)
但是如果 bean 有装饰器或拦截器,将创建一个代理(没有创建客户端代理,因为没有任何用于选择 bean 的上下文,但是创建了另一个代理来应用装饰器和拦截器)我在 weblogic 应用程序中测试了这个服务器
假设我有一个 @ApplicationScoped
服务 class。
@ApplicationScoped
class MyCustomerService {
...
}
然后我想将 Connection
对象注入该服务。
@ApplicationScoped
class MyCustomerService {
private final Connection connection;
@Inject
MyCustomerService(final Connection connection) {
this.connection = connection;
}
...
}
Connection
对象由 @Produces
方法使用数据源生成。
class ConnectionProducer {
...
@Produces
Connection getConnection(final DataSource dataSource) {
return dataSource.getConnection();
}
...
}
Connection
class 会被代理吗?每次我使用 connection
bean 时 CDI 都会调用生产者方法吗(不像 RequestScoped,我的意思是每次调用)?
我知道我可以 @Inject
DataSource
,这就是 "learning" CDI 管理作用域的方式。
Will CDI invoke the producer method each time I use the connection bean
没有。 producer 方法被调用一次,因为默认范围是 Dependent 。您的连接生命周期受限于 MyCustomerService 生命周期
Will the Connection class be proxied
如果 bean 在 @Dependent 范围内,那么客户端持有对其实例的直接引用(clientproxy 只是为 NormalScope 创建的)
但是如果 bean 有装饰器或拦截器,将创建一个代理(没有创建客户端代理,因为没有任何用于选择 bean 的上下文,但是创建了另一个代理来应用装饰器和拦截器)我在 weblogic 应用程序中测试了这个服务器