I have an application, that my JAXB2 annotated classes are possibly stored in multiple JARs, and I am using them in my web app (which use Spring MVC with Spring OXM Jaxb2Marshaller to expose RESTful WS).
I would want to generate the XSD for all classes that is possibly be used in my WS. However, com.sun.tools.jxc.maven2:maven-jaxb-schemagen-plugin and org.codehaus.mojo:jaxb2-maven-plugin are generating by inspecting the source code of the project but I want some similar solution which base on the runtime classes, as my POJOs are stored in other JARs, which APT will not be able to scan my code.
It is fine to generate the XSD in compile time, or by running the web app. Is there any solution?
Use JAXBContext.generateSchema(...) in the runtime.
See this answer:
Is it possible to generate a XSD from a JAXB-annotated class?
Related
Can I make a library with Spring framework, and then include that library in an application that uses the Spring framework?
Yes you can make a library that uses Spring, and then include a dependency on that jar in another application created with Spring. You will want a build tool that handles dependencies, like Maven or Gradle, and probably a repository manager like Nexus or Artifactory to store artifacts that you create.
You have to make sure that the jar gets included in the component scan performed by the hosting application. See the Spring reference documentation on Importing configurations.
If the library has its own Configuration, importing the Confuguration gets it included in the component scan.
Alternatively create a marker interface in your library like this:
#ComponentScan
public interface MyLibrary {}
then in the including application have a Configuration class annotated with
#ComponentScan(basePackageClasses= { MyLibrary.class })
and the including application will scan all Components in the package hierarchy starting from the package of the marker interface.
Spring is open source so you can contribute to it. Refer to https://github.com/spring-projects/spring-framework/blob/master/CONTRIBUTING.md for more details.
Yes, any Java based applicaiton (like spring framework) can be packaged as a JAR file and published to a repository (or store it locally to start simple)
This Jar file can be included as a dependency in another Java based application (like spring framework)
To add dependencies you can either use Maven or place it in a directory and add it to local classpath for the next application to use the library.
Your library becomes a reusable library (usually a JAR file) for all other java based applications
I do not wish to use Spring annotations or xml or any declarative, non-compile-time-safe code or anything that relies on reflection. However, I'm guessing Spring does have traditional libraries (like Apache Commons libraries) that you can call from your code rather than your annotated code getting called by the framework.
What Spring projects are libraries? I'm assuming some of them are.
The core of Spring is the Spring Framework. Which is uses reflection, annotations etc without remorse. Using spring as a library completely misses the point. You could use, say JdbcTemplates with the correct dependency but why?
If you do use Spring as a Library bare in mind you are only getting tiny bits of functionality, most of which is just bootstrap code you avoid writing. Spring is designed to be used within it's framework and built upon. Some do use libraries such as Apache Commons. Using Spring Framework as a library however does just complicate your own project and people would probably ask - "why".
We have several java projects. Most of them are built with Struts 2.0 framework and few built with Spring 3.2. We want to consolidate all the back-end integration service into a separate project using spring 3.2 and import this jar file on all the projects. Here are my questions
What is the best way to initialize spring application-context for a jar based spring project? This jar is utilized by multiple web-project that are built using Struts and other non spring MVC frameworks.
I read How to package spring based library for reuse?. However, this question didn't answer on how to auto-load the application context when you a call a Service from the built spring-example.jar file.
For example. I have a WeatherService.java class in spring-framework.jar file. I want to import the spring-framework.jar file into another Struts-MVC based application and call WeatherService.java from an Action Class. I want the spring bean configuration to initiate automatically when calling the WeatherService.
You can use #Import annotation if using Java configurations or <import> tag if using XMl configs. With this approach you can reuse import one Spring context into another one.
Link to documentation:
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-java-using-import
EDIT:
If you are using maven, place your application-context.xml into src/main/resources. If not, make sure that it's on classpath.
Than if you are using XML config do
<import resource="classpath:application-config.xml"/>
or if you are using Java config do
#ImportResource("classpath:application-config.xml")
In the project our team's working on, we currently have 3 separate Spring projects which utilizes the same services. To avoid redunancy and code copy-pasting, we're planning to create a "common" project wherein all the three projects would be dependent on the common project. In this instance, is it possible to inject these services (perhaps using the #Service annotation) to the Controllers of the Spring projects?
EDIT:
I tried implementing this on my own and what I basically did was I configured the pom.xml to get the Spring Context 3.1.1 dependency (which are also being used by my Spring projects) for my "common" project. With that, I was able to annotate my service with #Service. Afterwards, on my Spring project, I set the component-scan to a level wherein my two projects would meet. On my Spring controller, I #Autowired the service from the "common" project. I ran the Spring project and apparently it worked. Is this the best way to do this?
That's absolutely fine, and standard. Spring (unlike CDI) couldn't care less whether your beans come from the current project or from an imported jar.
Is it possible to configure Jersey via XML rather than annotations? Here's my issue:
I have a maven multi-module project with the following modules:
client
webservice
shared
In the shared module, I would like to put my basic POJO classes, with minimal dependencies in the Maven POM. The webservice module will require the POJOs to be configured for both Hibernate and Jersey (such as with #XmlRoot and #Entity annotations). The client module has no need for the Hibernate- and Jersey-specific configuration, and having the classes annotated would introduce the dependencies into the client POM.
Normally I actually prefer annotations over XML, but in this particular case I'm trying to keep the design modular and at least somewhat clean.
Any suggestions?
You can annotate the classes and mark hibernate and jersey as optional dependencies. Then the classes are annotated appropriately and your client is free from the extra dependencies.