官术网_书友最值得收藏!

Creating your first CDI bean

A CDI bean is an application component that encapsulates some business logic. Beans can be used either by some Java code or by the unified EL (expression language used in JSP and JSF technologies). Beans' life cycles are managed by the container and can be injected into other beans. All you need to do to define a bean is to write a POJO and declare it to be a CDI bean. To declare that, there are two primary approaches:

  • Using annotations
  • Using the beans.xml file

Both ways should work; however, folks prefer using annotations over XML as it's handy and included in the actual coding context. So, why is XML just over there? Well, that's because annotations are relatively new in Java (released in Java 5). Until they were introduced, there was no other way in Java than XML to provide configuration information to your application server. And since then, it continued to be just another way, alongside the annotations approach.

Moreover, if both are used together, XML is going to override annotations. Some developers and application administrators tend to perform temporary changes or hot-fixes sometimes, by overriding some hard-coded programmatic configuration values, using external XML files. It worth mentioning that this approach is not a recommended way to actually deploy things into your production.

In all of the coming examples, we are going to use the annotations approach. However, in the Using interceptors section, a usage to the beans.xml file will be required and used in one example. Now, let's start our first example and define our first CDI bean:

主站蜘蛛池模板: 泌阳县| 崇州市| 兴隆县| 东丽区| 娄底市| 尼勒克县| 湖州市| 东山县| 梁平县| 壶关县| 周至县| 铜鼓县| 喜德县| 龙江县| 衡南县| 嘉荫县| 田阳县| 盐津县| 临城县| 呼和浩特市| 枣庄市| 平顶山市| 那坡县| 堆龙德庆县| 大丰市| 枞阳县| 重庆市| 离岛区| 思南县| 上林县| 合阳县| 喜德县| 邵阳市| 松潘县| 高邮市| 迭部县| 清水河县| 西城区| 邛崃市| 武威市| 贺兰县|