cdi

CDI - Injecting objects dynamically at runtime

末鹿安然 提交于 2020-01-02 15:52:32
问题 How do I inject objects at runtime? For example, if I want to inject DerviedOne, DerivedTwo objects at runtime into the Test class in the following example, how do I do that? I found a few examples in Spring, but I'm not using Spring. This is a Dynamic Web Project with CDI using Java EE 6. public abstract class Base { public Base(String initiator) { this.initiator = initiator; } public abstract void process(); public void baseProcess() { System.out.println("base process"); process(); } public

How to pass parameter to injected class from another class in CDI?

China☆狼群 提交于 2020-01-02 05:26:06
问题 I am new to CDI, tried to find solution for this question, but, couln't found any. Question is Suppose I have one class which is being injected(A) from, where some value(toPass) is getting injected, now I want to pass this same value(toPass) to class B, which is getting injected from class A. public class A { String toPass = "abcd"; // This value is not hardcoded @Inject private B b; } public class B { private String toPass; public B(String toPass) { toPass = toPass; } } Can anybody please

List<Integer> received List<String>

旧城冷巷雨未停 提交于 2020-01-02 05:18:19
问题 I think that I found a error in runtime of Java with JSF 2.0 (Using Primefaces), in this project I'm using JSF 2.0 Primefaces and CDI. Resuming the problem, I have a method setter in my business class Role that received a List, but the JSF is setting a ArrayList on that. Should the java throw a exception or at least should not find a method that matches? here is: public void setAcl(List<Integer> acl) { this.acl = acl; System.out.println("Received: " + this.acl); for(Object i : this.acl) {

Unable to inject @ApplicationScoped bean in JAX-RS service

空扰寡人 提交于 2020-01-02 04:10:30
问题 I've created JAX-RS service in which I want to inject an application scoped bean. The problem is that the bean is not injected. How is this caused and how can I solve it? JAX-RS service: @Path("room") public class RoomService { @Inject GameController gc; public RoomService() {} @Path("create") @GET @Produces("application/json") public String create() { Room r = new Room(); gc.addRoom(r); // gc is null return r.toJson(); } } Application scoped bean import java.util.ArrayList; import javax

What is difference between resource injection and dependency injection (CDI) in Java?

被刻印的时光 ゝ 提交于 2020-01-01 07:31:08
问题 I have been learning Java EE for while and found Java EE provides two types of injection mechanisms Resource Injection Dependency Injection Please guide me to understand the difference between Resource Injection & Dependency Injection. 回答1: Java EE provides injection mechanisms that enable our objects to obtain the references to resources and other dependencies without having to instantiate them directly (explicitly with ‘new’ keyword). We simply declare the needed resources & other

Migrate JSF managed beans to CDI managed beans

廉价感情. 提交于 2020-01-01 05:00:07
问题 I'm planning to convert a web app from using JSF managed bean to using CDI managed beans. I know I'll need to do below: Add a empty beans.xml file in WEB-INF. Replace all JSF @ManagedBean to CDI @Named annotations. Replace all JSF scope annotations with CDI or OmniFaces scope annotations. Replace all JSF @ManagedProperty with CDI @Inject annotations. Is that all that needs to be done? Are there any gotchas that I need to be aware of? 回答1: Basically, that's indeed all you need to do provided

How to create instances on the fly in CDI

Deadly 提交于 2020-01-01 04:22:05
问题 Let's assume I have a Car class. In my code I want to create 10 cars. Car class has some @Inject annotated dependencies. What would be the best approach to do this? CDI has a Provider interface which I can use to create the cars: @Inject Provider<Car> carProvider; public void businessMethod(){ Car car = carProvider.get(); } Unfortunately that doesn't work if I don't have a CarFactory that has a method with @Produces annotation which creates the car. As much as it reflects real world that I

Manually Register Class in CDI Container

妖精的绣舞 提交于 2019-12-31 19:51:46
问题 I have a group of classes which are instantiated by reflection, hence these are not managed by the CDI container, and no injections are made by the context. My question is, is there any way to register these classes in the CDI context, so the classes get managed by the context? Bellow, is how I create the classes: String clazz = "org.myorg.thisIsMyClass"; MyClass myClass = Class.forName(clazz).newInstance(); // myClass instance not managed by CDI How do I make the instance of myClass managed

@ManagedProperty does not work in a CDI managed bean

▼魔方 西西 提交于 2019-12-30 10:34:47
问题 I try to learn JSF and encountered on a problem connected with ManagedProperty. However I have tried to use it, it always failed - null exception pointer. What am I doing wrongly? I have read some "similar posts" on stackoverflow, but they did not helped to me. (I use GlassFish 4.0, JSF 2.2, JDK 1.7, Netbeans 7.3.1 (Java EE pack) and Java EE 6.0) <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1

The difference between context and scope in CDI - and Java at all

我只是一个虾纸丫 提交于 2019-12-30 05:56:09
问题 Studying JSR-299, I read the section 5.1 of the Weld reference which explains how scopes works in CDI. Apparently, context is a concept closely related to scope. I have understood a bit about what each one is but it is not very clearly separated in my mind and I feel tempted to even use the words interchangeably. What is the difference between scope and context? What is the relationship between the two concepts? I expect an answer in the CDI domain but it is a doubt I have about Java in