cdi

Can I (and how) lookup CDI managed beans using javax.naming.Context#lookup in EJB module?

醉酒当歌 提交于 2019-12-17 22:20:18
问题 Can I (and if so, how?) lookup CDI managed beans using javax.naming.Context#lookup in EJB module? I'm using GlassFish v3. I suppose that I can use @Named , but what is JNDI name of CDI managed bean? I want to lookup them from unmanaged POJOs so I can't use @Inject . 回答1: You can lookup the BeanManager via JNDI ( java:comp/BeanManager ) then use the JSR-299 API hung off of the BeanManager to get a contextual reference to a managed bean. JSR-299 managed beans are not available for direct JNDI

Can an Action class be scoped to Singleton?

天涯浪子 提交于 2019-12-17 19:37:01
问题 My question is not only if action classes can be scoped to singleton, but I also want to know which are the best practices. Both in context of Struts2 and Spring. Best scope of VIEW (say request or session), for controller and model. 回答1: Struts2 Actions are managed by the Struts Container. They are ThreadLocal, hence every request has its own thread-safe copy of the Action. If you use Spring to handle them through the Struts2-Spring-plugin, there are multiple levels of usage: you can let the

Why use constructor over setter injection in CDI?

筅森魡賤 提交于 2019-12-17 15:44:33
问题 I couldn't find any reasonable answer here on SO so I hope it's not a duplicate. So why should I prefer setter or constructor injection over simple @Inject MyBean bean; I get the usage of the constructor injection if you need to do something with injected bean during your class initialization like public void MyBean(@Inject OtherBean bean) { doSomeInit(bean); //I don't need to use @PostConstruct now } but still, it's almost the same like @PostConstruct method and I don't get setter injection

Changing faces-config.xml from 2.2 to 2.3 causes javax.el.PropertyNotFoundException: Target Unreachable, identifier 'bean' resolved to null

情到浓时终转凉″ 提交于 2019-12-17 10:01:59
问题 Have the following code snippets: Bean: import javax.faces.view.ViewScoped; import javax.inject.Named; @Named(value = "directoryBean") @ViewScoped public class DirectoryBean implements Serializable { private static final long serialVersionUID = 1L; .... } faces-config.xml <?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp

Meaning of bean discovery mode annotated in CDI 1.1

拟墨画扇 提交于 2019-12-17 07:30:46
问题 I am migrating an application to Java EE 7 and would like to CDI 1.1. But I don't get the meaning of bean-discovery-mode="annotated" . The CDI 1.1 specification is not very helpful. At least I have not found any useful paragraph. Did I miss it? This example runs perfectly with bean-discovery-mode="all" and injects an instance of LoggingClass : public class LoggingClass { public Logger logger = Logger.getLogger("ALOGGER"); } @Test public class MMLoggerProducerIT extends Arquillian { @Inject

@Inject to pass params to a CDI @Named bean via URL

廉价感情. 提交于 2019-12-17 06:14:41
问题 If I cannot use the @ManagedProperty annotation with @Named, because @ManagedProperty doesn't work in CDI(?), then how do you pass params in the URL to the facelets client? In my code, I want to pass javax.mail.getMessageNumber() to details.xhtml through the "back" and "forward" buttons. I understand that @Inject should be used, but what is being injected and how, please? From the glassfish logs, id is always 0, which is quite odd. Even when "forward" is clicked, id never gets above 1 no

Should I use @EJB or @Inject

a 夏天 提交于 2019-12-17 02:52:20
问题 I have found this question: What is the difference between @Inject and @EJB but I did not get any wiser. I have not done Java EE before nor do I have experience with dependency injection so I do not understand what I should use? Is @EJB an old way of injecting? Is the injection done by the EJB container when using this annotation while using @Inject use the new CDI framework? Is that the difference and should I be using @Inject instead of @EJB if this is the case? 回答1: The @EJB is used to

CDI PostConstruct and volatile fields

浪子不回头ぞ 提交于 2019-12-14 03:55:50
问题 Using a post construct approach when we want to conditionally initialise some of the bean's fields, do we need to care about volatility of the field, since it is a multithread environment? Say, we have something like this: @ApplicationScoped public class FooService { private final ConfigurationService configurationService; private FooBean fooBean; @Inject FooService(ConfigurationService configurationService) { this.configurationService = configurationService; } void init(@Observes

Define and inject a map in EJB 3.1 or CDI

心已入冬 提交于 2019-12-14 02:47:20
问题 After several years developing in Spring, I switched to EJB and I am not happy that I have no solution for this use-case. Let's say it is the strategy pattern implemented by a map. In Spring, it could look like this. <bean id="myBean" class="MyBeanImpl"> <property name="handlers"> <map> <entry key="foo" value-ref="fooHandler"/> <entry key="bar" value-ref="barHandler"/> </property> </bean> In EJB/CDI, I have this. @Stateless public class MyBeanImpl implements MyBean { private Map<String, Class

CDI injection in JAX-WS endpoint does not work, results in NPE

倾然丶 夕夏残阳落幕 提交于 2019-12-14 02:43:00
问题 Why doesn't the following CDI work in JAX-WS endpoints in glassfish 3.x.x? I get an NPE when accessing the service from the endpoint. @WebService public class JaxWsTestEndpoint { @Inject private MyService service; @WebMethod public String sayHello(String name) { System.out.println("injected service:" + service); service.callService(); return "Hello, " + name + "."; } } Where the class "service" is defined as follows: @Named("myService") public class MyService { public MyService() { System.out