how to load spring without a web container - java

I have a RESTful service using Jersey/Spring/Tomcat. My question is - is it possible to access the classes within my war from command line (without the web interface)?
The problem is the JDBC connections and some other beans are managed by spring. So how do I load all these beans without the web container?

You can easily initialize application context and load all the classes manually. Here is an example:
ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext("/applicationContext.xml");
YourBean yourBean = applicationContext.getBean("yourBean", YourBean.class);
applicationContext.close();

Classes in a WAR archive are tricky, because you'll have to tell the ClassLoader to look inside WEB-INF/classes and WEB-INF/lib/*.jar . Sounds non-trivial to me...

Related

How to bind a spring bean to JNDI so that it can be accessed from a different war?

I have a WAR and an EAR deployed in wildfly. The ear has a spring boot application which spins up a particular bean. I want that instance of the bean to available in jndi something like
java:/mySpringBean
so that I can do a lookup from the WAR (which again has a spring context).
The goal is to reuse the instance which was instantiated from the EAR spring boot.
First of all, I'm not sure if that's even possible,
I tried
https://konstantinpavlov.net/blog/2009/01/01/how-to-export-spring-managed-bean-to-jndi/
but it doesnt seem to be working, after I added the code from above I do not see a an entry in wildfly management console under the JNDI view.
Pick one of the classes where your spring application is bootStrapping. In my case there was a starupClass which would run upon application start. Then I #Autowired the bean to that class. Got the IntialContext and bind the bean.
Context ctx = new InitialContext();
ctx.bind("mySpringBean", mySpringBean);

Need some clarification on Spring Configuration and Initializer

I have been following this tutorial to build my Spring REST API for a project.
http://websystique.com/springmvc/spring-4-mvc-rest-service-example-using-restcontroller/
Now, I have a question. Do i have to create <class_name>Configuration and <class_name>Initializer class for every rest controller that I am going to make ?
The answer is no. Configuration and Initializer classes are application context wide. Those are just another way of configuring the Spring application context (the other is using xml configuration files)

Pluggable Spring context files

What would be the best way to handle a situation, where new Spring context is introduced to application during a runtime, and server restart is not an option?
I have an application where some beans are registered to other bean called "manager" when context is created. Now, should there be new functionality needed later, I have a Spring configuration file defining beans for the new functionality, and these beans should register them selves to "manager", which is already created. This works fine if the new file is added to classpath and server is restarted, but how to achieve it without restarting?
I guess I should make Application container somehow notice new context files and process them like during startup. But is that possible?
Have you considered combining spring with a framework like Apache Felix?
http://felix.apache.org/

Can a non spring web application use a jar which is built using spring + hibernate

We are building a java application using spring hibernate maven. The jar will be used in a non spring web application that uses ejb and plain jdbc. Ejb will access the jar interface methods which in turn makes hibernate calls.
What changes are needed in the web application to load the spring context during start up? Is it feasible?
If you want to keep your jar decoupled from web application you should do this:
Copy your jar and all dependant jars to webapp WEB-INF\lib directory (or pom if maven)
Your Spring Context should be loaded in a static variable, in a safe thread manner; or if you want to integrate it to the webapp lifecycle you can do it in several ways: web.xml, web-fragment or using a servlet initializer. There are lot of documentation in Internet
If you continue to use hibernate or change to JDBC, the DataSource should be provided generally by the web container, so you must use InitialContext.lookup("") to get the actual DataSource.

Use of ClassPathXmlApplicationContext in Standalone Java class

I am not exposed to Spring as yet. I saw the below code in one of the standalone java projects that I have in my system. Can you please help me understand the below code.I am unable to see spring.xml in the project - is it something that must be there and is missing?
appContext = new ClassPathXmlApplicationContext(new String[] {
"classpath*:/META-INF/spring.xml",
"classpath*:myapplication-application-context.xml"
});
The classpath* syntax means that Spring will search the classpath for all resources called /META-INF/spring.xml and myapplication-application-context.xml, and will amalgamate them into the context. This includes looking through JAR files inside the project, so there may not be any visible within your main project files.
The core functionality of Spring revolves around the ApplicationContext which is the "Central interface to provide configuration for an application. " This interface is implemented by the ClassPathXmlApplicationContext which helps you take the context definitins from your classpath .Hence you specify classpath* .
As #skaffman explains , your application get loaded from the context definitions in the above mentioned files . i.e, all the Spring beans are initialized and Dependency Injection is performed as required .
If you deal with web applications , Spring has got a corresponding web application context loaded by XmlWebApplicationContext

Categories