differences in the Introspection for applicationContext xmls between windows and linux? - spring

I'm currently developing a modularized application with spring. currently we have a core with a main method that looks for the application context xml files of any modules using the following line:
ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("META-INF/spring/core.xml", "META-INF/spring/*-module.xml");
the annoying part is the server deployment. on our windows based developer machines all modules are found at startup, meaning we get multiple lines like:
o.s.b.f.xml.XmlBeanDefinitionReader - Loading XML bean definitions from file [...\META-INF\spring\legacy-module.xml]
on linux (debian lenny), there is only the entry for the core context xml.
my guess is I'm doing something wrong, so how do i configure my application context to properly look for all *-module.xml files in my classpath?

I changed the pattern to
classpath*:META-INF/**/*-module.xml
now it works. Be warned: */-module.xml does not work. the Spring documentation states that the first folder is required otherwise the classloader will not find the resource.

Related

Log4J2 with Websphere 8.5

I am trying to do a log4j migration for a legacy application. I made the changes to the configuration files and tested it locally with Tomcat, where the logs were displayed correctly. However when I deploy on WAS, the logging stops.
I checked a lot of pages about the necessary dependencies and the possible problems, but now I feel I'm stuck. Here is what I already tried:
Added the log4j jars manually to the lib/ext directory in WAS (together with commons-logging, I read an article where it helped, although all these jar-s were already present in the ear)
Double checked, that the configuration file is on the classpath and that it's syntax is correct
Added logj42-web dependency
Added the necessary filters in web.xml (Spring version is 3.8, so also the config listener with the config name context-param)
Checked classloader in WAS - it's parent first
Am I missing something?
Any ideas are appreciated. Thank you in advance!
WAS includes its own commons-logging API (that does not utilize Log4j), so bringing your own logger requires a bit of extra class-loader-related config. Typically, that would go something like this:
Put your commons-logging and log4j jars, along with your logging properties files, in some directory (not WAS_HOME/lib/ext) readable by the user running the server.
Create a shared library on the server, with that directory as its class path, and select the "use an isolated class loader" option. Associate that shared library with your application or web module.
Ideally, that should be it. Isolated shared libraries search themselves before delegating to the server's loaders, so your application will "see" the commons-logging/log4j classes in the shared library instead of in the server, and likewise, it should pick up the configuration files from that directory instead of the ones found in the server.
You can also accomplish this same basic thing by leaving all the logging stuff in your WAR or EAR and setting its class loader to parent-last (which causes the class loader to search locally before delegating to the server-level loaders), but that is a bit riskier configuration - if your application includes APIs that are also provided by the server, parent-last class loading increases the possibility of ClassCastExceptions or LinkageErrors.

SpringBoot creating a framework starter library

I am creating a library using spring-boot (v2.1.6.RELEASE) as a starter project that will facilitate as base extension jar responsible for configuring and starting up some of the components based on client project properties file.
The issue I am facing is that if the client project's SpringBoot Application class contains the same package path as library everything works like charm! but when client project contains different package path and includes ComponentScan, it is not able to load or start components from the library.
Did anyone encounter this issue? how to make client application to auto-configure some of the components from library jar?
Note: I am following the library creation example from here: https://www.baeldung.com/spring-boot-custom-starter
There are many things that can go wrong here, without seeing relevant parts of actual code its hard to tell something concrete. Out of my head, here are a couple of points for consideration that can hopefully lead to the solution:
Since we use starters in our applications (and sometimes people use explicit component scanning in there spring applications) and this obviously works, probably the issue is with the starter module itself. Don't think that the fact that the component scan is used alone prevents the starter from being loaded ;)
Make sure the starter is a: regular library and not packaged as a spring boot application (read you don't use spring boot plugin) and have <packaging>jar</packaging> in your pom.xml or whatever you use to build.
Make sure you have: src/main/resources/META-INF/spring.factories file
(case sensitive and everything)
Make sure that this spring.factories file indeed contains a valid reference on your configuration (java class annotated with #Configuration). If you use component scanning in the same package, it will find and load this configuration even without spring factories, in this case, its just kind of another portion of your code just packaged as a separate jar. So this looks especially "suspicious" to me.
Make sure that #Configuration doesn't have #Conditional-something - maybe this condition is not obeyed and the configuration doesn't start. For debugging purposes maybe you even should remove these #Conditional annotations just to ensure that the Configuration starts. You can also provide some logging inside the #Configuration class, like: "loading my cool library".

Write custom View Resolver to read a JSP from file system and not inside war

I am new to Spring framework . I am trying to extend any one of the current viewResolver and trying to override the buildView() method to have a custom logic of my own to read a file from file system (on the server) and display it .
This is a prototype and is investigating on how can i implement it with spring .
Problem i am facing is that i am not able to convert the File to an AbstractView to be returned from buildView .
Any ideas ?
Not really an answer, but it does give hints and is too long for a comment.
The problem here is that a JSP needs to be compiled to java file then to a class file to be executed. This is normally done by the servlet container for files located in the war (exploded or not). I do not know a public API to do that conversion outside of the web application folder.
Possible hints:
tie the application to a specific servlet containers and use explicitely methods or tools of that servlet container. I think Tomcat's JSP compiler is documented, and at least as it is free software you can look in the source how to use it.
forget JSP and use a template solution such as Thymeleaf, Freemarker or Velocity. As there is no compilation phase, it will be much easier and portable. At least Velocity includes a loader that loads files from an absolute folder in file system.

Browse spring context

Currently, I'm working on a project which has a large number of dependencies.
During development, I sometimes have issue with some spring bean.
Normally, the stack trace can tell me the name of the bean. However, it does not shows me which context.xml file declares the bean. Moreover, it does not tell me which .jar file contains the context file and how that context file is imported into my project (I means the chain of import).
As there are many depedencies, not all context file are uses, many beans are override....
It takes me a lot of time to search the correct context file. One simple solution is to import all related project into my eclipse workspace and everything becomes horribly slow.
I'm thinking about writing a tools to speed up the process:
Given a name of spring bean / the class of bean, the tool will search in the whole class path of application and returns:
The spring context file which declares the bean.
The .jar file that contains the spring context file.
Extract the content spring context file and show it directly to user
If user provides the root spring context file, the tool will shows the import chain to the destination spring context file.
It does not take lots of time to implement this but I just wonder whether somebody has already implement it? I just don't want to reinvent the wheel.
I found the project https://github.com/julior/spring-inspector. It's interesting, but it's not what I need.
If you know a tool like the one above, I'd be very happy to know about it.
You totally should look at Intellij IDEA's great support for Spring Framework.
More information available here: http://www.jetbrains.com/idea/features/spring_framework.html
To find interesting bean in xml config you just need to click on green bean image.
Intellij IDEA is definitely worth its price.

Does anybody use Jersey in embedded mode(not servlet env but standalone Grizzly instance) successfully with a template engine?

Velocity, Freemarker, Japid, Rythm, any other?
What i am looking for is a solution similar to what JSP provides in servlet environment. It should be a form of ViewProcessor implementation, but working on standalone embedded Grizzly deployment(NIO architecture).
Jersey has support for freemarker templates - see jersey-freemarker module on maven. It works with any container (i.e. is not depending on servlet). There is also freemarker sample in Jersey workspace that shows how to use it - see here.
I think all the template engines you listed could be used in an independent environment. Specifically for Rythm (coz I am the author of this stuff), it loads template files from different sources:
from the String content supplied:
String result = Rythm.render("hello #who", "world");
from a file found from the rythm.root setting:
String result = Rythm.render("helloWorld.html", "world");
from a file found from the class path if you have no rythm.root setup
It just doesn't depend on any servlet container, so you are free to use it as long as you have JRE. Be sure to set rythm.noFileWrite to true if you want to use it in GAE where no file write is allowed.
The Open Source Project
http://www.bitplan.com/index.php/SimpleRest
supplies a TemplateResource base class which will do most of the "heavy lifting" to make Jersey, Grizzly and the Rythm template engine work together nicely.

Resources