I've seen a couple of folks wondering why CDI requires a beans.xml file in every bean archive. If there's no alternatives, interceptors, or decorators to declare, why do you need to have the empty file?

Well, there's two things about CDI that we need to keep in mind:

  • CDI does not require any special declaration for a Java class to be injected - that's right, no annotation or XML declaration at all!
  • CDI does not define any special kind of module - CDI beans can be deployed in a library jar, EJB jar, war, rar, or JVM classpath directory.

The CDI specification calls the process of identifying beans in modules bean discovery.

So there are potentially a lot of classes in the classpath which might be beans! We don't want to force the container to scan every one of those classes at startup and build its internal metamodel for each of them. This really could make bean discovery very slow.

But there's another reason we need to give the user some control over which classes are available for injection. We don't want every class in the classpath to potentially match an injection point, including classes that were never intended to be injected into things. This would force the developer to have to use qualifiers much more often to disambiguate injection points.

So we have two choices. We could have the developer:

  1. explicitly exclude modules which do not contain beans, or
  2. explicitly declare modules which do contain beans.

I think it's clear that the second option is a much better way to go. Thus, CDI has the notion of a bean archive. A bean archive is just a module that has a file named beans.xml in the metadata directory. The container looks for beans in bean archives. It ignores other modules.

Now, you might be wondering if we've got the granularity wrong here. Why should module be the right criteria to use for including/excluding a class. Why not consider:

  • a class-level annotation,
  • the package,
  • some type it implements or extends, or
  • some naming convention.

Well, I think we've got the first option covered. Annotate a bean @Alternative, or with an alternative stereotype, and it will be considered disabled by CDI, as long as you don't explicitly enable it in beans.xml. That's not quite the same thing as excluding the class from scanning altogether, but it's close. (One difference is that a portable extension with still get a ProcessAnnotatedType event for that class.)

Excluding a package makes sense to me, and a future version of CDI might allow you to declare excluded packages in beans.xml.

Excluding a bean by type or naming convention doesn't appeal to me at all. In the world of CDI, we use stereotypes for identifying architectural roles. We don't use marker interfaces or naming conventions. I strongly disapprove of having names affect functionality.

We'll experiment with giving you some finer grained control over bean discovery in Weld. However, my expectation is that the current solution is going to work great for most people.

21. Dec 2009, 06:15 CET | Link
Piero Sartini | piero(AT)

I am not sure I like this behaviour. IMHO it would be way better to have the possibility to define top level packages that are used while discovering beans. In general I like to define what should be included, not what should be excluded.

While prototyping a webapp, it's convenient to have all my classes inside one war - and split them into different modules when they get more stable.Also, how does CDI play together with other IoC containers? Take Tapestry as an example: someone would need to exclude all tapestry-ioc managed classes (pages, components, services, ...) in order to use CDI inside the war. Not very developer friendly.

Maybe I am missing something.. so please take this as the oppinion of someone who just started to learn CDI.

28. Jan 2010, 05:21 CET | Link

Gavin, you saved my day! I was searching for 3 days, why my @Injects are not working, reading this, I know why.

Greets from Germany

12. Apr 2010, 03:26 CET | Link

One thing I am struggling with in CDI is the fact that there is basically only one global scope. If I understand correctly, when I have one interface X and an implementation XImpl in the same bean archive then XImpl will be injected when '@Inject X x' is used.

However, when I add a new bean archive to my classpath that contains another implementation of X, then all of a sudden a different implementation would be used. This might be desired behavior but could also be very unwelcome.

In other words, what I would like to be able to define is something like an injection domain, where an injection domain would consist of an explicit list of packages or even classes. Then, for a given injection I could define the injection domain explicitly.

Another question I have is w.r.t. unit testing. In unit test code, I typically create a mock object (for instance using Mockito), like so: X x = mock(X.class);

Then I want to verify interactions on x and want to make sure that my mock is injected instead of the normal implementation. The only way I see that this is currently possible is by defining an alternative and then creating a separate beans.xml only for unit test to enable a mock alternative. Of course, the mock defined in beans.xml is not the mock I create in my unit test (I don't even know the classname of this mock). So from the unit test I would need to make my mock object available as static and then from the mock class declared in the beans.xml delegate to my mock object. This approach is rather cumbersome. Is there perhaps another way to do this?

12. Apr 2010, 03:28 CET | Link

Small correction: adding another bean archive with another implementation would lead to an ambiguity that I would need to resolve.

23. Dec 2013, 09:15 CET | Link

Looking for a quick and Discount Coach Outlet Online simple sewing craft for Halloween? How about a ‘Boogie Monster’ Tissue Holder from Creative Cheap Coach Factory Outlet Store Team member, Jessica! This easy Halloween craft is kid friendly and fun! Plus you can make them in under 15 minutes.

02. Jan 2014, 08:48 CET | Link

Click HELP for text formatting instructions. Then edit this text and check the preview.

08. Jan 2014, 13:39 CET | Link

The software's license gives the user the right to use the software in the licensed environment. Some software comes with the license when purchased off the shelf, or an OEM license when bundled with hardware. Other software comes with a free software license, granting the recipient the rights to modify and redistribute the software. Software can also be in the form of freeware or shareware.

28. Mar 2014, 22:51 CET | Link

An alternative is a bean that must be explicitly declared in the beans.xml file if it should be available for lookup, injection or EL resolution. By default, a bean archive has no active alternatives. An alternative must be explicitly declared using the fully qualified bean class name or stereotype class name in either a child class or stereotype element of alternatives, respectively.

12. Apr 2014, 11:46 CET | Link

This post is so informative especially the images posted here do make this blog more outstanding in comparison to others maybe this post needs to be briefed here

12. Apr 2014, 11:48 CET | Link

Really these contents needs to be shared on social networking sites as they are really the brief and great masterpieces here

12. Apr 2014, 11:49 CET | Link

Got some depth knowledge in advance after having a quick and brief glance on to it, thanks a lot for sharing here on this webpage.

12. Apr 2014, 12:06 CET | Link

No content is comparable and admirable then this mentioned for the explanations, a must one to read for everyone.

12. Apr 2014, 12:07 CET | Link

Great information shared with the users here, if eel delighted with it.

20. Apr 2014, 16:36 CET | Link

It's an old. This will not work. There is nothing in the CDI, or any implementations that allow you to do this.

02. May 2014, 07:48 CET | Link
Click HELP for text formatting instructions. Then edit this text and check the preview.
02. May 2014, 07:54 CET | Link
another glorious work shown here, most outstanding one to have a quick access to article here
10. May 2014, 12:17 CET | Link

My reasearch work has just came to a conclusion after reading this brilliant masterpiece

10. May 2014, 12:18 CET | Link

another glorious work shown here, most outstanding one to have a quick access to article here

10. May 2014, 12:19 CET | Link

Well explained logic, that's true and must be followed for best outputs to have a brilliant experience for future assistance

10. May 2014, 12:20 CET | Link

Brilliant information being shared here with us, learned best examples from it to make myself more awared of with this situation

10. May 2014, 12:21 CET | Link

Still these solutions are brilliant one, everyone must follow these procedures wisely to have a great termed knowledge

13. May 2014, 18:43 CET | Link

women , to deny them the right to gender aesthetcheap mulberry bayswaterics and male nudity world through human self ,forskolin

14. May 2014, 14:37 CET | Link
best pregnancy test

There are many locations that offer more than just one higher education institution. In the state of Virginia, there are several schools there including th

16. May 2014, 19:09 CET | Link

Just let me say here that this is a very nice step taken by the author. This post will definitely be the inspiration for many of the new authors that need some kind of push through for themselves. 4 cycle Fat Loss Review

17. May 2014, 08:10 CET | Link

Whoa! Where did you find this information? I have been looking for this for almost a year now. I was told I would find it here previous week, and now I've finally found it. Thank you so much! kontorimööbel

17. May 2014, 11:48 CET | Link

You know what CDI is? Want to know more about the same and why beans.xml is required? The detailed description of the same is given in the article that attracts the readers in every possible manner.

outlook office email

20. May 2014, 14:34 CET | Link

This really is my first chance to click here. I discovered some interesting things and i'll affect the introduction of my blog. Thank you for discussing helpful information.reishi mushroom extract6TnXld

21. May 2014, 09:23 CET | Link

Amazing work. But, I would like to have more information on this topic, please. Thank you! Click Here

21. May 2014, 21:10 CET | Link
Wolfgang Puck Pressure Cooker Review

Great post. Thank you a lot for this. My wife will be extremely happy when I read this article in front of her. You are a gem when it comes to article writing! Wolfgang Puck Pressure Cooker Review

01. Jun 2014, 00:50 CET | Link

It was a very nice idea! Just want say thank you for the data you have distributed. Just continue posting this kind of post. I will be your loyal reader. Thank you again. party ideas .

24. Jun 2014, 13:09 CET | Link

A trader who is offering signals services to guide his introduced clients to trade and generate income can structure a business plan where he uses the live forex quotes to determine how much he can make from his clients trading activity A trader who is offering signals services to guide his introduced clients to trade and generate income can structure a business plan where he uses the quotes to determine how much he can make from his clients trading activity.

28. Jun 2014, 15:42 CET | Link
FreedomPop reviews

FreedomPop is one of the most affordable 4G mobile broadband providers, offering service in more than 70 major cities across the nation. FreedomPop reviews

28. Jun 2014, 16:49 CET | Link

FreedomPop, the MVNO that provides “free” service under the right circumstances, is adding refurbished iPhone 5 smartphones to the line-up. The iPhone 5 will have access to LTE connectivity, and will use FreedomPop’s voice-over-IP service as the primary carrier, with access to Sprint’s network in other circumstances. FreedomPop

28. Jun 2014, 20:09 CET | Link
Sultan Alhokair

Sultan Alhokair always had an interest in business, and studied many different aspects at Northeastern University. Including subjects like Finance, Small Business Management, and even Family Business Management. All of which would serve him well on his career path.

29. Jun 2014, 21:46 CET | Link
CEG TEK International

We all know that piracy is a huge problem in the US, but not everybody realizes just how big the problem has become. That’s the issue at hand that CEG TEK International is trying to solve. Their mission is to track copyright infringement, and try to prevent it overall by providing the information and going after the people actually stealing the content outright. CEG TEK International

30. Jun 2014, 06:38 CET | Link

Eating a healthy diet is of key importance to your success. The same can be said for a regular exercise program.

30. Jun 2014, 21:06 CET | Link
Ben Shaoul

This article is certainly the most close to being a 'real deal' in the real world. I would love it if you understand what that phrase means and work accordingly to achieve the maximum for your article. This article deserves it all! Ben Shaoul

01. Jul 2014, 22:54 CET | Link
Andrew Heiberger

Mr. Heiberger, 43, is the founder and former chief executive of Citi Habitats, one of the city’s largest rental brokers. In 2004, he sold the company to NRT, the parent of the Corcoran Group, and a year later started the development group Buttonwood Real Estate. Andrew Heiberger

03. Jul 2014, 02:32 CET | Link

Thanks for the outstanding function. I like this internet website extremely considerably, Its a actually good billet to read and obtain info . Enjoyed searching at this, really very good stuff, thanks .

07. Jul 2014, 09:16 CET | Link

HOLLYWOOD’S LATEST darling Lupita Nyong’o has been almost flawless in her choice of fashion and clothes since she lit up the screen in Steve McQueen’s Oscar-winning film 12 Years A Slave, but omega replica she flirted with disaster at the Met Gala ball. The actress’ green and brown Prada dress caused debate as she donned the outfit at last night’s (May 5) annual event in New York, organised by fake chanel bags Vogue editor Anna Wintour. The 31-year-old star, who won best supporting actress for her part in McQueen’s masterpiece, is famed for her selection of bold and colourful clothes, but many fashion commentators fake gucci bags considered her latest outing to be wide of hitting the mark. Others called the dress risky, but what can be agreed is that the Prada dress definitely split opinion – a reaction unfamiliar to the Kenyan-born beauty. The cheap longchamp bags outfit featured a brown dress that was covered in a jewelled green net, that looked akin to something one finds in children’s playgrounds. Finishing off the ensemble were plumes of green feathers, while Nyong’o opted to wear a patterned headband.

14. Jul 2014, 23:29 CET | Link
Laurene Powell Jobs

A wonderful article this is! One of the best things that I've ever encountered in a long time is definitely this article. I really love this and hope that more will follow after this success. Cheers! Laurene Powell Jobs

01. Aug 2014, 12:32 CET | Link

Everybody knows which piracy is really a huge problem in the us, however, not everyone becomes aware of just how huge the problem is becoming. That’s the problem accessible which CEG TEK Intercontinental is trying to resolve. Their particular objective is to observe copyright infraction, and also make an effort to stop the idea total by giving the info and also going after the folks truly robbing this article outright.

03. Aug 2014, 01:49 CET | Link
Yan Gillbert | ohonaoho(AT)

I was not sure about this. But now I know how important this is. I am a new developer and can see many developers are still ignoring this. Tile and Grout Cleaning Chandler

12. Aug 2014, 08:12 CET | Link

Click HELP for text formatting instructions. Pure White Kidney Bean Extract Then edit this text and check the preview.

23. Sep 2014, 06:35 CET | Link

Surprisingly, one of the most well loved sports in the world is also relatively young. Despite the game's youth, it has gone through a wide variety of changes and evolution as a part of its rich hist rugby league

11. Nov 2014, 05:53 CET | Link

Do you want to learn meditation online? Light Watkins has been providing meditation course from many year in new york.Get the best meditation audio course online and best personal trainer now.New york meditation classes by light watkins

18. Dec 2014, 15:08 CET | Link
bad credit loan lenders

It was created to give new credence to certain early human ideas and ideologies that were favored by the leaders of the then current societies.bad credit loan lenders