简体   繁体   中英

CDI: Injecting resources to beans from external libraries

In Spring we have annotation-based and XML-based configuration. While the first is recommended for quick development, the second is more flexible and able to handle special cases. By us there are currently 2: injecting mocks for JUnit tests and configuring beans from external libraries.

I haven't found any equivalent for XML configuration for CDI, so my question is, how to handle dependency injection for such beans? They are from external libraries, they need to be configured and there's no possibility to add any annotations to them.

You have three solutions to your need:

Use a producer

CDI provides a way to transform non CDI class in beans. It is called a producer. If you want to create a bean from a class named NonCdiClass You only have to create something like that

public class MyProducers {
    @Produces
    public NonCdiClass produceNonCdiClass() {
        return new NonCdiClass();
        };
    }
  }

You can now @Inject this bean when needed.

You can put as many producer method as you want in your class.

If you need to simulate injection in your produced bean you can do it thanks to CDI that injects parameters in producer methods calls.

@Produces
public NonCdiClass produceNonCdiClass(MyFisrtBean param1, MySecondBean param2) {
    NonCdiClass res = new NonCdiClass(param1);
    res.setParam(param2);
    return res;
    };
}

In this example MyFirstBean and MySecondBean are existing bean classes that will be injected by CDI at producing time.

Producers can also have Qualifiers (on them or on their parameters) or inject the InjectionPoint which is a CDI internal bean allowing you to produce your bean differently in function of where is the injection and what annotation it has.

You have a nice InjectionPoint example in Weld reference documentation .

Develop an extension

I won't go into much details here since I don't know if it's your need, but you can register bean in an extension in the AfterBeanValidation phase. These registered beans can be of any class you want. Should you need more info I could develop here.

Use Seam Solder (legacy) or wait for Deltaspike 0.6

Solder integrated a config module but this project is no more maintained since it's been in the process to be merged in Apache Deltaspike . This merge is in Deltaspike roadmap for version 0.6: http://issues.apache.org/jira/browse/DELTASPIKE-271 . So you could start using Solder config and switch to Deltaspike when it'll have the feature (which should be quite close). This solution is not my favorite but if you really want to have a config file à la Spring, it's the closest solution

Pure CDI provides the @Alternative annotation to inject for example mock objects during testing phase via beans.xml , but many mock libraries do a better work since they're designed for it.

I'm not aware of a way to use beans.xml to inject anything outside of the ear/war itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM