Inject bean by CDI from different deployment - java

This might be a bit complex.
I have an EAR and a WAR deployed in a JBoss 7 container side by side.
The EAR has a service.jar module with EJBs and in its lib folder there is another utility jar. In the utility jar there is a resource producer class, like this:
public class BaseResources {
#Produces
private Logger getLogger(InjectionPoint ip) {
String category = ip.getMember()
.getDeclaringClass()
.getName();
return LoggerFactory.getLogger(category);
}
}
Now the WAR depends on the EAR and sees all its classes.
But when I try to inject a Logger instance into a class in the WAR, there is a org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [Logger] with qualifiers [#Default] at injection point thrown, telling me there is no Logger instance to inject.
Is there a way to inject the Logger into a class in the WAR?

Related

EJB Injection Between EARs on the Same Server

I'm running into an issue trying to migrate an application from Jboss AS6 to Wildfly. The application consists of a 'Product' ear, a 'Person' ear, and a 'Common' jar containing shared interfaces and utilities. All are being deployed as modules on a Wildfly application server. I need to be able to inject a service bean defined in Person into Product. The LoginService bean is located in Person and looks like this:
#Stateless
#Remote(LoginService.class)
public class LoginServiceBean implements LoginService {
#Resource
protected SessionContext context;
}
When I build and deploy the Person ear, I get the following log for my jndi bindings:
java:global/Person/Person-ejb/LoginServiceBean!com.tura.common.service.login.LoginService
java:app/Person-ejb/LoginServiceBean!com.tura.common.service.login.LoginService
java:module/LoginServiceBean!com.tura.common.service.login.LoginService
java:jboss/exported/Person/Person-ejb/LoginServiceBean!com.tura.common.service.login.LoginService
ejb:Person/Person-ejb/LoginServiceBean!com.tura.common.service.login.LoginService
java:global/Person/Person-ejb/LoginServiceBean
java:app/Person-ejb/LoginServiceBean
java:module/LoginServiceBean
In the Product project I then have a stateless EJB bean
#Stateless(name = "ClientServiceProvider")
public class ClientServiceProviderBean implements ClientServiceProvider{
#EJB(name = "ejb:Person/Person-ejb/LoginServiceBean!com.tura.common.service.login.LoginService")
protected LoginService loginService;
}
When I try to deploy, it then fails with the error:
No EJB found with interface of type
'com.tura.common.service.login.LoginService' for binding
ejb:Person/Person-ejb/LoginServiceBean!com.tura.common.service.login.LoginService"
I've tried every binding configuration I can think of; nothing seems to work. All the documentation I can find seems to want to use a manual jndi lookup rather than the annotation. What am I doing wrong here? Is there really no way to inject services between EARs?

How to access application scoped cdi bean packed as jboss shared library

I have to use an application scoped cdi bean present in jboss shared libraries in my ear application.
Example:
jboss\modules\com\test\test.jar
In test.jar I have one application scoped bean
#ApplicationScoped
public class Test {
#Inject
SomeClass someClass;
#PostConstructor
public void init() {
someClass.doSomething();
}
public void testMethod() {
}
}
myapp.ear → ejb.jar
In ejb.jar I have one class which initializes Test application scoped bean.
public class Another {
#Inject
Test test;
public void myMethod() {
test.testMethod();
}
}
When I test this example, I'll get null pointer exception as I'm trying to inject application scoped bean which is out side an ear application.
Note: I cannot have test.jar in my ear lib directory, as per my requirement it will be delivered as jboss shared library.
Any ideas how to access application scoped bean?
Add beans.xml to META-INF directory of your test.jar. In this case container will scan test.jar file for CDI beans.
This is also strange that you get NPE. You would get "unsatisfied dependencies" if injected bean was not found. Probably you instantiate Test bean incorrectly. For example, via the New operator.

Spring Annotations in XPages Java classes

I'm trying to use Spring annotations in Domino XPages Java classes. Spring works when I'm defining beans in the configuration file. However I fail to use the annotations.
To illustrate the problem, I have created two simple empty classes annotated with #Component annotation - com.geo168.a.B (#Component public class B {}) and com.geo168.a.C (#Component public class C {})
The first one I have created in Eclipse, packed and added to the application in a jar. The second one I add directly in Code/Java section.
I add a component-scan tag in the configuration file: <context:component-scan base-package="com.geo168.a"/> and try to instantiate the classes:
ApplicationContext ctx = new ClassPathXmlApplicationContext(SPRING_CONFIG);
// class defined in the jar, works ok
com.geo168.a.B b = ctx.getBean(com.geo168.a.B.class);
// class defined in Code/Java, throws exception
com.geo168.a.C c = ctx.getBean(com.geo168.a.C.class);
I get an error: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.geo168.a.C] is defined
Spring has found the annotated class only in the JAR file. It works if I add the bean explicitly to the configuration file:
<bean class="com.geo168.a.C"/>
A similar post: Is possible to add annotation #ManagedBean in XPages? seems to address only the particular JSF annotations (that do not work, because they are not implemented).
In the Spring documentation: http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-scanning-autodetection I find a note: The scanning of classpath packages requires the presence of corresponding directory entries in the classpath. When you build JARs with Ant, make sure that you do not activate the files-only switch of the JAR task.
Could this be somehow related? I do not know in what form Domino deploys the classes internally.

What is the difference between declaring #Autowired and declaring bean in the application context

I build the spring project1 of module1 as a jar file, then added this jar file dependency in another module2 of project2, then the module2 is added in the project3. Now, while starting the tomcat service getting the error as NoSuchBeanDefinitionException for Module1 class object.
While checking with module2 code found injected module1 class object using #Autowired annotation, By including the bean declaration in xml (<bean id="object" class="claspackage"/>. Issue has been fixed. but I am unable to find the root cause for this, how the bean functionality got differs?

Injection of an autowired field failed in a multi-module Maven project - NoSuchBeanDefinitionException

I read many posts here at Stackoverflow and at other sites regarding this problem, but didn't find a solution.
I have the following structure of my Maven modules with one main parent pom which declares all these modules (I simplified the structure here in order to show only the relevant part):
The base and "A" modules depend on the base-api module. The base module contains implementations of the interfaces contained in the base-api module.
I have an interface IFoo in the "base-api" module. The interface IFoo is implemented by a class Foo in the "base" module. The class Foo is annotated with the Spring's "#Service" annotation.
I would like the Foo service to be autowired in my test class which is contained in the module "A":
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration
public class FooTest {
#Autowired
private IFoo foo;
I created also a context configuration file for my test which contains among others the following line
<context:component-scan base-package="x.y.z"/>
Both IFoo and Foo are contained in subpackages of x.y.z (in different maven modules, as described above).
When I run the test in Eclipse (with the m2eclipse plugin), then it passes correctly. However, when I run the maven build (mvn clean install), then the following error occurs:
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [x.y.z.v.IFoo] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations {#org.springframework.beans.factory.annotation.Autowired(required=true)}
What am I doing wrong?
If module A is not dependent on base module, any execution on module A will not find any of the components in base module. Consequently, any component in module A dependent on an implementation from base module will fail (since the implementation is not visible from module A).
If you simply want components in base module to be accessible to module A to run tests, you can add a dependency from base module to module A with scope set to test. This way, your tests on module A will run just fine. You will have the flexibility to introduce a completely different JAR at runtime with a different implementation of IFoo, if you so require.

Categories