How to use bean in dagger 2? - javabeans

I've 2 packages. In Package A, There is bean
#Bean(name = BEAN_NAME)
public PkgA getPkgA_Bean() {
return new JobReasonCodesFetcherImpl(componentConfiguration.component());
}
So In package B, I've dagger 2.
So how should I use this bean in package B. So that it injects PkgA through bean name ?
Can anyone please help me to understand how to fix this ?

Hello i think you are using Spring term instead of dagger
You want to use #Produce inside a module instead of #Bean
For using name in dager you can use #Named
And be careful dagger2 is using code generation instead of reflection for spring. In consequence some things can be easy to do in spring can't be done with dagger. Like classpath analysis

Related

Why does Quarkus complain about my (unused) #ApplicationScoped data class with a #ConfigProperty property?

This kotlin data class is part of an internal library used in some Quarkus microservices (Quarkus 2.0.0.Final):
#ApplicationScoped
data class FooConfiguration(
#ConfigProperty(name = "foo.bar")
val fooBar: String
)
The library is used in a few microservices, and most of them do use that configuration. But some are not. For those, the foo.bar property is not defined in the application.properties. I would expect this to not matter at all, as those services are never injecting a FooConfiguration, so I'd expect that to never be constructed.
However, the application refuses startup with this error message:
SRCFG00014: The config property foo.bar is required but it could not be found in any config source
I know how to workaround this issue (simply supplying a nonsense value), but I currently wonder why this is an issue in the first place. The configuration bean should never get constructed. Why is this happening?
This is a MicroProfile Config-related issue.
Your foo.bar property is optional, not required, in the parlance of the MicroProfile Config specification, because a value for it is not present in any configuration source, as you have indicated. To inject a value for an optional MicroProfile Config configuration property, you need to use java.util.Optional. I don't know Kotlin, so here is what it would look like in Java:
#Inject
#ConfigProperty(name = "foo.bar")
private Optional<String> fooBar;

Injected messages returning null in eclipse e4

In my plugin, I have created separate packages for maintaining the messages.properties.In that same package I have created my Mesages.java class as well. Following is my message class
#Message(contributionURI="platform:/plugin/com.chinna.test.properties.messages")
public class Messages {
public String test1;
public String test2;
}
Then injected the message in a different package of the same plugin. like follows. The class in which injected is singleton class.
#Inject
#Translation
Messages messages;
after I injected it I tried access messages.properties in the code like follows
messages.test1
But my application is throwing Null-Pointer exception. since the "messages" is null.
even though I injected, I do not know why "messages" is null.
Could any please help me?
You can only use this in classes which are injected.
Injection is only done on classes referenced in the Application.e4xmi, some extension points or created/injected using the ContextInjectionFactory make or inject methods.
For other classes the older style of messages file extending NLS and calling NLS.initializeMessages can be used.

how to lookup cdi beans while is not stateless and localbean?

I have CDI RequestScoped bean and much more java class that are not beans. in any of these java classes, I want to use my bean but it can be injected (why? I do not know) one possible way is using this code :
final Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
final Context context = new InitialContext(jndiProperties);
EntityManagment em = (EntityManagment) context.lookup("java:app/drools-guvnor/EntityManagment");
that work well if my bean is stateless and localbean. what I can do for none stateless and localbeans?
There is no JNDI-Name for CDI given.
You could either register them yourself or you create an EJB that has been injected with your object. Then you can lookup this EJB and get your injected object from there.

Is it possible to define a spring bean backed by a dynamic language in Mule ESB?

I'm attempting to define a spring bean using as below:
<lang:groovy id="mmmm" name="GroovyRssGeneralTestServer" script-source="${app.home}/groovy/RssGeneralTestServerImpl.groovy">
<lang:property name="xmlFile" value="${app.home}/rss/GeneralTestServer.xml" />
</lang:groovy>
I've tried several different locations for the script-source, but have had no luck in getting mule to find the source for the groovy script.
Second, I'm curious whether I can even wire up a groovy component to use this bean even if I do get it configured correctly above?
Why are you trying Spring bean when there is a Groovy component in Mule that can execute external Groovy script ? Please refer :- http://www.mulesoft.org/documentation/display/current/Groovy+Component+Reference
and this :-
http://groovy.codehaus.org/Dynamic+language+beans+in+Spring
Hope this help

How to Inject Singleton EJB(Container Resource) into POJO(non Container resource)?

I want to inject a Singleton EJB into my POJO class.
With the new EJB 3.1 spec, is it possible to inject an EJB into a POJO?
I know in EJB 3.0, the #EJB annotation could be used to inject an EJB, but this did not work on simple POJOs.
#javax.inject.Inject is also not working for me.
One more thing is, what is the difference between a container and a non-container resource?
How do I achieve it, I am using JBoss AS 7.1.1.
EE is designed around the idea of component classes (EJBs, servlets, etc.). An EE container can only perform injection when it controls the creation of the object, which does not apply for POJO, so you cannot use EE injection on POJO objects.
For CDI to work, you need to add META-INF/beans.xml to your archive. Even then, you cannot create the POJO instance using new. You always have to let the container create the instance, so either #Inject the POJO, which can then #Inject the EJB, or use javax.enterprise.inject.spi.BeanManager.
#EJB won't work for you so you have only two options - JNDI lookup or using CDI. Something like
#Inject
private MyEJB ejb;
should work for you. Also check that you have beans.xml in the WEB-INF folder in order to activate CDI container.
And for the difference - it is almost the same (while it's recommended to use #Inject) with only exception - you still have to use #EJB for injecting remote beans.
Reason why you can't use service = new ServiceClass(); is that service object will not be managed by the container - that means that no injections will be peformed after creation of this class because container is no longer in control of this object. Very naively said, if you do
#Inject
ServiceClass service;
container will create new instance, then perform injections and return it to you.