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....
Related
I have the below configuration in xml of an maven dependency jar.
context:property-placeholder location="file:/dir/project.properties"/>
Unfortunately I can't create that folder in my machine and can't modify dependency code too. So when I am running the parent app, I want to override the location of properties file or provide external properties file.
Both parent and child are developed using Spring Core. (It's not Spring Boot). Parent is War and child is jar.
According to Spring Context lifecycle, if it uses Spring Cloud Context, the Spring application first loads configuration from bootstrap.properties, then starts and then loads application.properties (in your case project.properties).
You can add to the project dependencies the Spring Cloud dependency. Specifying classpath (or using default config folder) you can have properties in another place.
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)
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'm facing issue, integrating Spring in my eclipse rcp project. Basically, to avoid confusion, I've created a simple Hello RCP With a view, created a Plugin from existing jars(for spring Jars), added this project to Hello RCP with a view as a dependency, I've placed applicationContext.xml file in my src folder of the RCP project.
It always throws FileNotFoundException when I place applicationContext.xml in the src folder. As a workaround I've also tried using absolute path
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("E:/applicationContext.xml");
This approach thorws ClassNotFoundException: Cannot find class with bean Name .....
for a normal Java application it works for me. How can I do that, where to place applicationContext.xml in Eclipse RCP, are there any simple tutorials for integrating spring with eclipse rcp. How to get rid of this issue and use spring in my application.
Spring DM is an option for integrating spring and osgi. expose spring beans as services in osgi.
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.