Deployment lifecycle when init method is called - spring

Hi I am newbie to Servlet/JSP/Springs .Wanted to know
I have a ear file which I deploy on weblogic server . Now as part of this ear some modules are exposed as webservice.
Now I have spring bean in the above mentioned ear on which init method I am trying to access the webservice method's which should be exposed as part of deployment of this particular ear.
Will I be able to access the webservice or yet till init method is called the modules are not exposed as webservice
So basically when I do the deployment first the init method of spring bean will be executed or
the modules which should get exposed as webservices will get exposed .

The init method will be executed first during which your web service will be initialized after which the web service will get exposed.

Related

Spring OSGi integration: ClassCastException when getting osgi service in springContext

New to OSGi. I'm trying to get a service from osgi in Spring. Git link for the code : https://github.com/shinevs/SpringIntegrationTest
Getting ClassCastException.
java.lang.ClassCastException: com.bundle.Activator cannot be cast to com.myInterface.BundleInterface
at com.osgi.OSGiLauncher.lambda$0(OSGiLauncher.java:39)
at org.apache.felix.framework.EventDispatcher.invokeBundleListenerCallback(EventDispatcher.java:915)
at org.apache.felix.framework.EventDispatcher.fireEventImmediately(EventDispatcher.java:834)
at org.apache.felix.framework.EventDispatcher.run(EventDispatcher.java:1147)
at org.apache.felix.framework.EventDispatcher.access$000(EventDispatcher.java:54)
at org.apache.felix.framework.EventDispatcher$1.run(EventDispatcher.java:102)
at java.lang.Thread.run(Thread.java:748)
I'm trying to initialize an OSGi bundle jar from spring service. OSGi bundle register a service called Activator, once OSGi bundle initialized and Activator created, trying to access the Activator from Spring.
Note : OSGi bundle is another module, it is added part of this project for testing purpose. Spring App only need a OSGi bundle as a jar.
How to run : curl http://localhost:8080/osgi
Issues facing : Spring App while trying to access Activator, it is throwing ClassCastException
Is this due to Different class loaders in spring and osgi. I tried to implement fragment, but that also didn't work.
It seems to me you are trying to exfiltrate a type (com.myInterface.BundleInterface) from inside the OSGi environment to outside into the Spring environment. But the BundleInterface is loaded by the OSGi loader and it unknown to the Spring environment loader. If you have added the test bundle into Spring Boot environment than it will have different class loader than the OSGi environment instance and thus you get the cast failure.
I suggest you avoid trying to use the OSGi environment inside Spring Boot application. I see only pain and suffering in that direction.

main() method in a spring mvc application

My understanding of a basic Java application is that every application should have a main() method but when I wrote a Spring MVC application (not Spring Boot), I don't think there was ever a main() method that I wrote. Isn't it mandatory? What am I missing here? Or is this implemented in Spring somewhere internally? If yes, where is the main defined?
Spring MVC is just a Servlet based framework which can only be run inside Servlet container like Tomcat or Webligic. The main method is deep in Servlet container that when you start will go and load war file into Java VM and delegate HTTP calls made to it to the appropriate Servlet that in turn will delegate to your Spring controller. For example when you run Tomcat startup.bat or startup.sh scripts they eventually will run Java main method.
SpringMVC applications are typically run within an application server, for example Tomcat, so there is no main method like a traditional java program. SpringMVC has a servlet that is loaded by the application server and starts the webapp.

How to determine if webapp has successfully deployed in Hudson or other CI servers?

I created a maven deployer module that will deploy select artifacts from the maven repo. This deployer module is configured as a job on Hudson. I want this job to pass if the webapp is successfully deployed, i.e. not only is the servlet container started, but the Spring App Context is successfully loaded. How can I achieve that? Right now the job indicates success even though the Spring App Context has errors but the servlet container says "started".
I thought about creating an integration test that tries to load a Spring bean but it will have to be part of my webapp project, and I don't want to merge the deployment module into my webapp module.
Any ideas?
One possibility is to expose a 'health' servlet in your webapp that could (for instance) return 200 if everything's cool (Spring is loaded), or 500 if there's a problem.
You can then have an ant task that uses the 'http' ant condition to pass / fail.

FileNotFoundException in a testing a Maven module

I'm working on a recently mavenized legacy project with following multi-modular structure:
Parent:
Web
Service
Dao
("Service" module is dependent on "Dao" module)
Problem: some tests of Service classes call DAO code that creates beans using Spring's ClassPathXmlApplicationContext (this part is not really DAOs but caching related). Since, ClassPathXmlApplicationContext uses spring config xml of the DAO module - the Service tests fail throwing FileNotFoundException. I think this is because tests run in Service module and the spring config xml being referred lies in Dao module.
Please advise on how can I resolve the above issue in tests referring to code/resources of other modules?
Put a copy of the Spring configuration under src/test/resources in the Service module. Quite often you want a different configuration for testing anyway, but also it means your tests are less dependent on configuration changes in another module.

how to call a method in Stateless EJB during deployment?

How can i tell a application server to call a method in my stateless ejb so that I can print some message during deployment. If I annotate the method with #PostConstruct then it is not working. I also added static block still not working. I dont want to use MBean. Also my ear does not have any web project. So I can not use any servlet and its init method to print such messages.
Server we are using is Jboss.
thanks
There is no such mechanism in EJB 3.0.
Eager / auto loading of EJB / load EJB on startup (on JBoss)

Resources