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)
Related
I have a spring web project in eclipse which has folder structure as
src/main/java
src/main/resources
src/test/java
src/test/resources
For some time my eclipse project used to work fine. But recently, when i start the application my eclipse project loads spring annotated beans from
src/test/java
folder.Because of this i am getting
org.springframework.beans.factory.NoUniqueBeanDefinitionException
from spring as there are two implementations of the same spring bean.
When i remove this folder from build path my application works well. Can someone please help with the reason for such behavioral change in my project?
we have two types of ioc containers in spring BeanFactory,ApplicationContext.when a request comes bean factory creates the instance which we declare a bean in xml. but application context creates all bean instances at the time of loading....
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
I have Spring configured to look up a conf/database.properties file to load some configuration.
This works well outside Tomcat, and in Junit tests, but in Tomcat, it never load. Below the images of this problem.
Configurations:
And:
The properties file in project folder:
The parameters to run Tomcat inside Eclipse:
The temp0 Tomcat Folder, where is all the files being generated Ok:
The Tomcat error log:
https://gist.github.com/4060538
I solved the problem. I think I was using spring in the wrong way.
I changed the follow:
In the library I'm importing/referencing, I removed the line where was importing database.properties file.
I created a spring.xml in my main web app, where in this file I imported the database.properties file and the other app-context.xml files I need to reference.
I think Spring spring don't load properties file outside of the jar. You need to load properties file locally in you main application, and so, references another spring-context.xml files needed.
I have been working on migrating our code base onto Glassfish 3.1.2 using Java 7. I have been struck with this issue, where the deployment of EAR fails. I have an EAR, having modules, ejb.jar, couple of war files, along with lib directory having jar files to be shared across other modules within the EAR.
The issue is that ejb.jar refers to some spring bean definitions in lib/abc.jar file and is unable to find the spring file. The structure is like this:
EAR
ejb.jar (some spring files in here refer to lib spring file like, xyz.xml importing spring/abc.context.xml)
couple of war projects.
lib/lot of jar files (one of the jar here contains the spring file being referred from ejb.jar, note the file is inside directory inside the jar, like spring/abc-context.xml)
But the ejb jar cant load the bean definitions from lib/.jar
I have tried using the Manifest.MF inside the ejb.jar to refer to lib/abc.jar, but with no luck....
Not sure if there is some sort of problem with new GF 3.1.2, why its not able to respect the Manifest file. Any help will be much appreciated!
It looks like the JVM doesnt like the manually edited Manifest file and my guess is that windows is adding something like a carriage return etc. I managed to get it working by using the ant to generate the manifest file instead.
Thanks and hope that helps someone.
maven module spring application.
1 module is a spring mvc application, another is a non-web but spring managed application.
In my project root, I have:
/src/main/conf
This folder contains my non-web managed spring xml configuration. I added this folder to my class path in intellij.
IntelliJ doesn't pickup the file correctly, meaning I don't get code completion or anything (allot of the names etc. are in bold red i.e. intellij is telling me something is wrong). Can this be fixed somehow?
I have kept my spring mvc config file inside its module (not in the conf folder) because the code completion doesn't work and it's a pain to work with without the IDE helping. But it makes managing things during deployment harder.
In both of my spring config files (for the web app and non-web app), I have my dataSource settings hard-coded in the file, I want to extract this somehow into a properties file, how can I do this?
1 - You can have different spring configuration files all in the same folder, just you need to use different names form them. So, I would use src/main/resources instead to create a new folder in the maven project structure. You will avoid problems.
2- Datasource in both files? why? If you have already two spring configuration files, I would create a third one (application-context-dao.xml) and share the dataSource. How to move properties to a configuration file? See.