Why spring is unable to load beans from the xml - spring

I have a web application, which is based on Spring MVC. I am trying to load Bean definition from a XML using following code snippet;
final AbstractApplicationContext fileContext2 =
new ClassPathXmlApplicationContext("classpath:spring-integration-context_webinf.xml")
But this does not work. File is located in WEB-INF directory, which I assume must be on classpath. If I import the file using #import resources -it works.
I am not stuck on this, but curious why its not working!

Related

Import exisiting Spring Context into Grails 3.0.1 app

I have an exisiting Spring application in Java and I want to add a Grails web app to it and reuse the domain model and my beans from the Spring context. I already created a hibernate.cfg.xml which from what I have read should integrate the domain model. But when trying to integrate the Spring ApplicationContext, I run into the following Problem/Exception:
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: A circular #Import has been detected: Illegal attempt by #Configuration class 'AppContext' to import class 'AppContext' as 'AppContext' is already present in the current import stack [ImportStack: [AppContext]]
My resources.groovy file looks like this:
beans = {
importBeans('file:grails-app/conf/spring/applicationContext-spring.xml')
}
In this xml-file, I just import my Java-config class for my Spring Java application.
I also get this circular import error when trying to define my AppContext class directly in the resources.groovy. (I assume because the code then does exactly the same.)
The error looks to me as if my AppContext is already imported by the Grais context, but when trying to use it, the beans are not present and I get the NoSuchBeanDefinitionException when trying to access them.
My question now: Does anyone know how to properly integrate my Spring context into a new Grails app?
Program version:
Java 8
Spring 4.1.0
Groovy 2.4.3
Grails 3.0.1
Gradle 2.4

Can't get Spring bean

I have a Spring project in which I want to get a specific Spring bean defined in my Spring beans XML File. My Spring bean XML file is located at /WEB-INF/spring/root-context.xml.
Here is my code in the Service class:
ApplicationContext context = new ClassPathXmlApplicationContext("/WEB-INF/spring/root-context.xml");
Here is the error I get when I compile :
parsing XML document from class path resource [WEB-INF/spring/root-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/spring/root-context.xml] cannot be opened because it does not exist
Probably WEB-INF is not inside your classpath. I suggest to move the xml file in the classpath (for example src/main/resources/spring/root-context.xml). Then you can access it with: ApplicationContext context = new ClassPathXmlApplicationContext("/spring/root-context.xml"); If you're inside web application, Spring Mvc looks for th context in WEB-INF/<the name of the dispatcher servlet>-servlet.xml
If you want to access the context from WEB-INF you can load it using
GenericApplicationContext ctx = new GenericApplicationContext();
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
xmlReader.loadBeanDefinitions(new FileSystemResource(path));
A better way is using WebApplicationContextUtils or implement ApplicationContextAware. Not sure what your use case is...

Where to put Groovy bean definitions in a Spring Boot webapp?

Using Spring Boot I can get a jar created with my webapp (allowing me to execute it as java -jar test-0.1.0.jar as I want). I would like to configure the Spring beans using the groovy bean def syntax, with a separate file like beans.groovy that would contain the definitions.
How can I have that groovy file included in my jar and how can I tell Spring to load it?
I've seen a few examples around the web showing how to define beans this way together with spring boot, but none explain how to get that groovy file included in the jar and run as a webapp.
SpringApplication accepts Resource paths as sources, so just include a String ending in ".groovy", e.g.
public static main(String args[]) {
SpringApplication.run("classpath:beans.groovy", args);
}

JUnit: how to access Spring configuration as Spring has intended?

There is a tutorial video that introduces Spring MVC 3.0. In the demo-project they use the following directory structure:
<proj>
src
main
webapp
WEB-INF
spring
appServlet
controllers.xml
servlet-context.xml
root-context.xml
Let's say I have a project with Maven-support and I want to write JUnit tests using Spring's configuration. Currently we use JUnit 4.8.2. This would obviously require to load the three files listed above.
In the test I could use annotations like
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration("classpath*:/WEB-INF/spring/**/*.xml")
However, that doesn't find the XML-files. I took a look at the classpath and noticed, that only the <proj>/target/classes and <proj>/target/test-classes are included by default.
One obvious solution would be to add the proper path to the classpath, but I don't know if that is what the guys at Spring had in mind.
Therefore, my question: What do I need to do to load the configuration files while letting it look as if I'm the total pro-coder using Spring?
Another option is to use file system resource loader:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
You should put the "normal" spring configuration in the resources folder but not in the webapp folder: src\main\ressources\WEB-INF\spring\root/spring-context.xml. Then you can access it without problems from the test.
Put only the web related spring configuration (servlet-context.xml) in the webapp folder.
The structure that you described is the one generated by the STS-Spring-Template:MVC-Template, however Spring-ROO and Spring-Fuse generate the structure that I have described.
For example Spring ROO:
<project>/src/main/resources/META-INF/spring/applicationContext.xml
<project>/src/main/webapp/WEB-INF/spring/webmvc-config.xml
<project>/src/main/webapp/WEB-INF/web.xml
web.xml:
...
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>

Spring, using new ClassPathXmlApplicationContext and getting error being unable to find applicationContext.xml and others?

I am trying to follow this tutorial: http://www.vogella.de/articles/SpringDependencyInjection/article.html to use annotation dependency injection in my application. I set up the bean, etc like in the tutorial and then am trying to get an instance of the bean within my MainController class (a controller class that handles generating a specific page for my spring web mvc app).. I keep getting
SEVERE: Servlet.service() for servlet spring threw exception
java.io.FileNotFoundException: class path resource [WEB-INF/applicationContext.xml] cannot be opened because it does not exist
I am doing this in my MainController:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BeanFactory factory = context;
BeanIRPlus beanirPlus = (BeanIRPlus) factory
.getBean("BeanIRPlus");
IRPlusInterface irPlus = beanirPlus.getIRPlus();
I have searched and searched on this and yet to find an answer that fixes my problem. My applicationContext in in webapp/WEB-INF/ and my spring app seems to be working otherwise as it was handling requests, etc before this. I have tried putting the applicationContext.xml in WEB-INF classes but still nothing. Is there any workaround to make this not search the path this way as I think its doing a relative path search. Thanks for any advice
Not a direct answer, but here goes.
The tutorial you have referred is for dependency injection in a standalone application and not a web application. In case of web application, spring automatically loads the context files and initializes the beans. So you would not need any of the lines specified in the MainController.
Instead, you could do something like this to use beanIRPlus bean in your controller.
#Autowired
private BeanIRPlus beanIRPlus;

Resources