#Controller not working when packaged in external jar - java

I have a spring web application which takes in an external jar which contains some annotated controllers in packakge com.x.y.controller.
I have included com.x.y.controller package in my component scan path in spring-servlet.xml
<context:component-scan base-package="com.x.y.controller" />.
But When I deploy the application to my local tomcat server, those controller bean in my external jar does not get created.
However, when I extract the jar into my class path when I build the application(meaning having my package fully expanded into my web application class path) everything seems working fine. It seems weird to me because we also have some service classes with #component annotation in external jar. And these class seems working fine.
Does anyone know if it is expected behavior of spring? Or am I missing some configuration for spring to find those controllers?
Currently during development, if I change a class, changes are reflected to tomcat without rebuilding even if the class is in an external jar. If I need to extract the jar for those controllers to work, tomcat does not seem to take in changes in those controllers even if I restart the server. A rebuild is required to extract the jar again for any change to take effect. This would be very painful for development. I am relative new to spring. Does anybody see a solution for this if I have to go that way?

Make sure external.jar exist on your classpath (either by adding to maven dependency, eclipse project settings, using -cp jvm command line arguments, etc), and just refer to the package name of the classes inside external .jar you want to include.
And add <context:annotation-config/> to your spring-servlet.xml

Related

Spring Boot : how to show actual classpath on startup or at runtime?

Is there a way to show Spring Boot actual classpath?
I am using STS to start my app and it reports that class XYZ is missing. Yet I checked my pom.xml and the dependency it self, and I can see the .class file.
I would like to show what is the real runtime classpath.
I used this (https://www.mkyong.com/java/how-to-print-out-the-current-project-classpath/), but I was looking for a solution that does not involve changing my app source code. A debug startup option type of thing. Actuator maybe. Whatever :)
Thanks
The classpath depends on the class loaders in use. For example Servlet Containers like Tomcat can load classes from multiple places e.g. shared lib directory or WAR archives. Spring Boot often repackages regular JAR to make it runnable by applying a custom layout. Without checking the class loaders there is really no way to tell what is part of the class path.
It's exactly what you said, debug option:
mvn spring-boot:run --debug

unable to find spring bean xml file in another module of application

My application has two separate maven projects.The first project is core project which has dao and bean classes. The second project is web project which is having spring rest classes. This web project has web.xml and spring-servlet.xml files. The core project has spring.xml file which has jdbctemplate and other bean definition codes like and datasource information.
I have written this code in spring-servlet.xml of web project to import the spring.xml file of core project.
<beans:import resource="classpath:/mycarecore/src/main/resources/spring.xml"/>
Now, when I am building the war file, the war builds fine but when I deploy the war on server and starts the server, it gives file not found exception for spring.xml file. I have given the project reference in maven dependency and I can see the jar of core project inside the war generated war. I have done multiple changes in file path, but it didn't help. Because of this, I am also not able to use jdbctemplate in my dao layer.
Please help.
The url for the classpath seems like an absolute url. This will work only if the spring.xml is located at /mycarecore/src/main/resources/spring.xml path on your computer. Otherwise you should simply use: classpath:mycarecore/src/main/resources/spring.xml.
From Spring documentation:
"You can always use fully qualified resource locations instead of relative paths: for example, "file:C:/config/services.xml" or "classpath:/config/services.xml". However, be aware that you are coupling your application’s configuration to specific absolute locations." (http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html)

Using convention plugin with struts.xml

I'm using convention plugin in my project and everything was fine until I needed to add a custom interceptor.
To add a custom interceptor I need to have struts.xml in my project. But when I add struts.xml, it gives me 404 error for every page.
I have tried this solution but it doesn't seem to work.
The struts.xml configuration file should be in the source folder, such as src or resources. When you build the application the compiled output directed to WEB-INF/classes. You can check this file exists before you deploy or start a server with already deployed web application.
FYI, the convention plugin doesn't replace the XWork configuration, but extend it with possibility to override. The struts.xml is used by the same purpose, but using different configuration provider.
So, configuring interceptors using convention plugin make sure you use the right parent package that contains your custom interceptor.

ClassNotFoundException from jar in Dynamic Web Project

I created a JPA project that I want to use in a dynamic web project.
I made a jar from this JPA project and added it in a lib folder in the dynamic webproject. I then added this to the buildpath of the dynamic web project and also ticked it in the "Order and Export" tab.
First question : I created the jar file with a normal export to jar with these options;
Are these ok or do I have to add something.
Second question: After I added this jar to the buildpath and I use a instance of a class from within this jar I can add it to the imports and the class is know so I don't see any errors.
When I try to run the jsp and servlet where I use the class I get a java.lang.NoClassDefFoundError error in my apache with a java.lang.ClassNotfoundEception at the line where I use the class. So for some reason he doesn' find the class at runtime.
I tried to make the jar over and over again with different options. I tried to import the class I need on different way's (ctrl-T) but I only see one class.
The only thing I find strange is the way the import looks. In my JPA project I have a package structure like :
be.JPA.project6
in my dynamic web project I have a package structure like :
be.project6
when I see the import, I see : be.JPA.project6.data.classname
I would expect that a different structure so he would know that he needs to search outside the "normal" package structure. But also with "ctrl-T" I only can see this class
I don't want to make a reference to the other project but use it like this with a jar.
I'm out of any clues at the moment, so any advise would be more then welcome.
The build path is that: a build path. A path used to build the application.
The runtime classpath of a webapp is composed from the WEB-INF/class directory, and the set of jars in WEB-INF/lib.
Remove the jar from the build path of the dynamic web project. The just drop it into the WebContent/WEB-INF/lib directory. Eclipse will automatically add all the jars in this directory to the build path, and will put them in the WEB-INF/lib directory of the deployed web application, so that they're also part of the runtime classpath.

Spring 2.5 in EJB container

I would like to use the spring framework within an EJB3 project. In detail I would like to use the JDBC template class which should be instantinated from a given data source. When I put the spring.jar to my Jboss lib directoy everything is working fine. But when I put the JAR inside my EAR only there seems to be external dependencies from JDbcTemplate to other libraries. EARs/EJBs classloader try to instantinate the JdbcTemplate and shows me that he can not load the class because of external dependencies. It does not show me which additional JARs I have to put in.
Question: Does some body know which addtional JARs I have to include or even how I can search for depending JARs with external tool. I remember there is a tool which can do this, but I do not know its name anymore. I think something like jarjar etc.
Could anyone help please? Thank you.
This smells like an EAR config problem not an Spring problem. Are you sure that the jar is in the EJB's classpath? You might want to check the MANIFEST.MF file of the EJB's jar to verify this.

Categories

Resources