Can you package an ejb interceptor in a library? - ejb-3.0

And if so how do you do it?
I have got an ejb #javax.interceptor.AroundInvoke interceptor which I like to move into a library for reuse. I moved the code into a library fixed the dependencies in maven and all compiles well now. Only on deploy I get the following error message:
09.02.2011 14:19:48 com.sun.logging.LogDomains$1 log
SCHWERWIEGEND: Exception while invoking class org.glassfish.ejb.startup.EjbApplication start method
java.lang.RuntimeException: java.lang.ClassNotFoundException:

From JSR 318: Enterprise JavaBeans, Version 3.1 - EJB Core Contract and Requirements:
20.3 Packaging Requirements
The ejb-jar file or .war file must
contain, either by inclusion or by
reference, the class files of each
enterprise bean as follows:
The enterprise bean class.
The enterprise bean business interfaces, web service endpoint
interfaces, and home and com-ponent
interfaces.
Interceptor classes.
The primary key class if the bean is an entity bean.
We say that a .jar file contains a second file “by reference” if the
second file is named in the Class-Path
attribute in the Manifest file of the
referencing .jar file or is contained
(either by inclusion or by reference)
in another .jar file that is named in
the Class-Path attribute in the
Manifest file of the referencing .jar
file.
So I'd say yes, you can package the interceptor class in a library .jar file.
Check that: 1) the library actually got packaged in and deployed with the ejb-jar file; and 2) the library .jar is referenced in the Manifest file as described above.

Related

Autowire a Bean from dependent library jar

I have a Spring Boot application (main-app) which users Library jar client-app.jar and mentioned as a dependency in of the application main-app.
Now I want to use #Autowire to inject bean from client-app. For this i have to add #ComponentScan on my main-app application.
But is there a way that i don't have to anything on my main-app by changing code on client-app.
Yes, you can create a sub-package by following the main-app let's say main-app has the following root of package com.example.main-app then on your client-app you should create something like com.example.main-app.client-app then the main application will scan its base package and will look the client-app as well.

WebSphere Liberty - How to access OSGi bundle services from a servlet deployed via an EAR file?

I have a servlet deployed to the WebSphere Liberty profile via an EAR file. The application entry in server.xml looks like this:
<application id="MyApp" name="MyApp" type="ear" location="MyApp.ear" />
The servlet needs to access a service provided by an OSGi bundle. I followed the example mentioned here. The OSGi bundle exports the package the Counter interface is in. Adding an "Import-Package" statement to the EAR's manifest (like an OSGi bundle would have) to resolve the reference to Counter resulted in a class-def exception. So I added the class directly to the EAR. Now InitialContext.lookup() does return a proxy object but typecasting it to the Counter interface results in a casting exception.
I suspect the casting exception is happening because the EAR has its own Counter.class. Is there some other way I should be resolving that reference? How can an EAR import a package from another OSGi bundle?

Mapping resource i.e hbm.xml not found during running test in maven

I am working on a multi module maven project . I am working on integration of spring and hibernate.
I have two modules
Model
Core
In the Model i have my domain objects.And i have kept the sourceUser.hbm.xml file in the model/src/main/resources/common folder.
The corresponding model class i.e SourceUser.java is in model/src/main/java/common.
Now i have core module ,i have my application context file in core/src/main/resources folder.In the application context file i have defined data source bean and session factory bean.
Now i have one test in the core module which is correctly getting the data source bean.But when i try to get session factory object it gives below error
Tests in error:
testApp(org.db.AppTest): Error creating bean with name 'sessionFactory' define
d in class path resource [applicationContext-core.xml]: Invocation of init metho
d failed; nested exception is java.io.FileNotFoundException: class path resource
[classpath:common/sourceUser.hbm.xml] cannot be opened because it does not exist
In the pom for the core project i have added dependency for the model .I believe that during test execution its not getting model jar in the class path and hence it is not able to find the mapping resource.I have below questions
1. Adding the dependency of model in core, will add model project in class path of core project?
2. If not how to make the hbm.xml's available for core module and in test. I dont want to copy the mapping files in the core project resource folder.

Bootstrap a Spring #Configuration class from another Maven module in a multi-module project

Before the spiel, I think the question essentially boils down to this:
How can I instantiate correctly #Configuration beans from an application's XML-based configuration?
In an attempt to modularize my project and follow something of a clean architecture, I have created a Maven project composed of three modules. There is a "web" module an "interface" module and a "core" module and both web and core use Spring's Java-based configuration.
The web module declares in it's POM a runtime dependency on the core module and a compile-time dependency on the interface module. The core module is the implementation of the interface module, the latter being composed of only Java interfaces and DTOs. (This is an attempt to program to interface modules.)
When I start the web module I want all the Spring-managed beans from the core module to become known to the web module's application context. I've had some success doing this the "XML-way" by creating an XML file in the core module that looks like this:
// This xml snippet is part of the "core" module
<beans>
<context:annotation-config />
<context:component-scan base-package="com.acme.core"/>
</beans>
... and referencing this in the web module configuration like so:
// The configuration of the "web" module
#Configuration
#ImportResource("classpath*:come/acme/configuration/spring/*.xml")
public class RootConfig {}
It works but I'm not happy with the solution because I want all the configuration for the core module to be done the Java way.
So to that end, I note that Spring says one can do the following:
...#Configuration classes may be declared as normal definitions within Spring XML files:
<beans>
<context:annotation-config/>
<bean class="com.acme.configuration.spring.CoreConfig"/>
</beans>
That would be (almost) ideal if it worked because the XML file in the core module would be very lean and essentially just bootstrapping the meaty configuration in CoreConfig. However it doesn't work for me and the web module cannot find any of the beans that are defined in the core module. I think this might be because if the beans are instantiated then they are done so in a different application context or maybe because CoreConfig, being marked with #Configuration, is special and instantiating it this way from the XML file doesn't trigger the creation of the other beans it defines.
Incidentally, I'd rather have a way to do this without any XML configuration but referencing com.acme.configuration.spring.AppConfig directly from the web module is not possible since there is no compile time dependency on the code. (Sigh) Modularizing is so far proving to be more trouble than it's worth...
The following works when specified in the config class of the "Web" module in my example:
#Configuration
#ComponentScan(basePackages={"com.acme.configuration"})
public class RootConfig {}
In fact, it is what #M. Deinum said to do in a comment on the question. In this example, all com.acme.configuration packages, regardless of whether they might be in another Maven module, will be picked up and processed correctly. It is necessary then, by convention, that all configuration classes of other modules be placed in com.acme.configuration. With this approach there is no need for any XML configuration file to "bootstrap" the configuration as I was trying to do.

How to load a spring beans which are in jar file in projects lib folder

How can I load a spring beans which are in jar file say for eg. sample.jar with all the beans declared in that jar with sample-applicationContext.xml?
Now I am using some of the beans from this jar in my project so when I deploy my ear file this sample.jar is in lib folder. Now when I deploy this project to server(jboss -5) it is not injecting the bean i have referenced in my main project.
we dont have any web app in this ear so the way we are loading beans are using ClassPathXmlApplicationContext. Can somebody gave me an example of how to load those beans from sample.jar(lib folder) first and then load those are in the project, so when spring creates beans in the main project it will have beans from sample.jar and would inject them.
Thanks
Beans are looked-up on the classpath, so if they are annotated and your have the proper component scan, they should be discovered.
If they are not annotated, but only listed in the sample-applicationContext.xml, then you can <import resource="classpath:sample-applicationContext.xml" />

Resources