- Developing Middleware in Java EE 8
- Abdalla Mahmoud
- 268字
- 2021-07-23 19:24:32
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:
- UI設(shè)計基礎(chǔ)培訓(xùn)教程
- Learning C# by Developing Games with Unity 2020
- Python入門很簡單
- PyQt從入門到精通
- PHP+MySQL網(wǎng)站開發(fā)技術(shù)項目式教程(第2版)
- C語言程序設(shè)計立體化案例教程
- 零基礎(chǔ)學(xué)Java程序設(shè)計
- 算法訓(xùn)練營:提高篇(全彩版)
- Access 2010數(shù)據(jù)庫應(yīng)用技術(shù)(第2版)
- Web前端應(yīng)用開發(fā)技術(shù)
- Test-Driven JavaScript Development
- Java并發(fā)編程之美
- Advanced UFT 12 for Test Engineers Cookbook
- Drupal 8 Development:Beginner's Guide(Second Edition)
- Python編程快速上手2