JavaEE 6 Study

关于CDI 的 inject。

在java 官方的陈述中, 要用inject, 需要满足以下的条件:

1.To use @Inject, the first thing you need is a META-INF/beans.xml file in the module or jar。

注意的是,所谓的inject, 只有在container 中谈才有意义,离开容器,就没有用武之地了。

关于 CDI Interceptor。

CDI Interceptor 注解的基本用法:

1.创建注解
@InterceptorBinding
@Target({ TYPE, METHOD })
@Retention(RUNTIME)
public @interface Log {
}

2.创建 Interceptor class:

@Interceptor
@Log //binding the interceptor here. now any method annotated with @Log would be intercepted by logMethodEntry
public class LoggingInterceptor {
@AroundInvoke
public Object logMethodEntry(InvocationContext ctx) throws Exception {
System.out.println("Entering method: " + ctx.getMethod().getName());
//or logger.info statement
return ctx.proceed();
}
}

以上两步注意 @InterceptorBinding, @AroundInvoke and @Interceptor 的用法。

3. 使用这个注解在其他地方,用在class 和 method 上。

4. 启用拦截器:
<beans>
<interceptors>
<class>org.superbiz.cdi.bookshow.interceptors.LoggingInterceptor
</class>
</interceptors>
</beans>

关于CDI @RequestScoped

对于requestScoped 的对象,每次请求都会生成一个对象。
“This means that an instance will be created only once for every request and will be shared by all the beans injecting it.”

关于 @ApplicationScoped

对于 @ApplicationScoped 对象,每个application 只会生成一个对象。
An object which is defined as @ApplicationScoped is created once for the duration of the application.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值