Currently I am creating Maven multi-module project with Spring and Hibernate. I am confused where should I have to put spring-dispature.xml to access bean configuration. Currently there are [core-web][core-service(request mapping)][core-bal(bal layer)][core-dal(implementation layer)] and [core-model(data access layer)].
I have put applicationContextBalUserProfile.xml in core-bal layer.
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!--Scan Merchandising Rest Services for Beans Defined In this Context -->
<context:component-scan base-package="com.hrcs.bal.userProfile.impl">
</context:component-scan>
<bean id="loginDal" class="com.hrcs.dal.userProfile.impl.LoginDalImpl" />
Now where should I have to put View Resolver?
You have an example of an Spring multimodule project here https://github.com/DISID/disid-proofs/tree/master/spring-boot-multimodule
(Don't worry if you are not using Spring Boot... is the same idea but without the provided auto-configuration)
Remember that you will have an "application"/"web" module with war packaging. All the other modules should be .jar packages.
That module will be the deployable one that have dependency with the rest of the modules with .jar packaging. (service-api, service-impl, etc.)
All #Configuration classes, .xml configuration files, spring boot starters (if needed), application.properties, etc. must be included on the .war module.
Regards,
Related
I have two modules in my project which are children of the main app.pom
<modules>
<module>commons</module>
<module>webapp</module>
</modules>
The webapp module refers to commons this way:
<dependency>
<groupId>com.app.whatever</groupId>
<artifactId>commons</artifactId>
<version>${project.version}</version>
</dependency>
The commons module is just packaged in .jar file and that's it. All their dependencies webapp and commons inherit from app.pom.
Both commons and webapp use Spring 3 with .xml files that describe application contexts.
In webapp module I want to use a bean from commons module. Here's applicationContext for commons:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.app.whatever.anything"/>
</beans>
For webapp applicationContext.xml I have:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<import resource="classpath*:/applicationContext-commons.xml"/>
<context:component-scan base-package="com.app.whatever.something.controller.spring"/>
But unfortunately this doesn't work. Constantly get an exception about no candidates for autowiring, though I can use any classes from the second module in the first one after setting the above maven dependency.
When I write this import without asterisk, it says that the file doesn't exist.
I tried without leading slash - the same exception.
This import works perfectly for any file from the same module and even for external libraries I have. But it can't resolve the file from this new commons module I've just created.
If you have any idea, that'd be really helpful, thanks
If it's not under a subdirectory, e.g. spring/applicationContext-commons.xml then the following (which you've already tried) should work:
<import resource="classpath*:applicationContext-commons.xml"/>
With an asterisk, Spring will use all matching files on the classpath, without the asterisk it will use the first one it finds.
Also, with the asterisk Spring will ignore the file if it wasn't found. Without the asterisk Spring will give an error if it can't find the file. Therefore your file is not on the classpath.
I'd check the location of applicationContext-commons.xml under the target folder to make sure <import resource= has the correct path and re-run mvn install from the root pom.
I have a project in grails 3, that has a project spring dependency, in the spring project, xml beans are configured. How should import the bens in grails architecture?
build.gradle
dependencies {
compile (project(':spring-project')) { transitive = false }
}
settings.gradle
includeFlat 'spring-project'
I tried the following ways:
in the resources.groovy load the beans:
beans = {
importBeans('path/to/beans-definition.xml')
}
in the resources.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<import resource="path/to/beans-definition.xml" />
</beans>
Besides that, in the spring project, beans using java annotation are configured. The beans is not working in grails app, even by setting the spring project packages in the conponent scan.
#ComponentScan(basePackages = ["package.spring.project.beans"])
class Application extends GrailsAutoConfiguration {
static void main(String[] args) {
GrailsApp.run(Application, args)
}
}
But, nothing works. Any help would be appreciated..
Beans can also be configured using a grails-app/conf/spring/resources.xml. In earlier versions of Grails this file was automatically generated for you by the run-app script, but the DSL in resources.groovy is the preferred approach now so it isn’t automatically generated now. But it is still supported - you just need to create it yourself.
<bean id="myBean" class="my.company.MyBeanImpl">
<property name="someProperty" value="42" />
<property name="otherProperty" value="blue" />
further check this link
I have 3 modules on project. One of them calls common. In this module i am trying to start Spring container. One of beans live in another module (webapp) and also i have a module testapp. So now, after deploying project on server (webapp and common) it is all OK, but if I try to deploy common and testapp it fails with exception:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class[...]
i whant to know if it is any possibility to ignore unexisting bean and run container? I am using xml configuration, no annotations. Due to business logic it should be exactly xml
Make use of Spring profiles to determine when specific beans should be instantiated. For example you can have two profiles web (for common + webapp) and test (for common + testapp)
Then in your application context xml configuration define your beans as follows
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- define common beans here-->
<beans profile="test">
<!-- define test beans here-->
</beans>
<beans profile="web">
<!-- define web beans here-->
</beans>
</beans>
Then depending on how you launch your application you need to provide a system property named spring.profiles.active to provide the profile to use.
For example -Dspring.profiles.active=web or -Dspring.profiles.active=test
I am noticing a strange issue with respect to spring XML schemas.
I have a standalone java application which uses spring framework. As long as I run this application within eclipse, I do not face any issues. However, when I package this as a jar file (as described in this link), and execute the jar, I get the following exception:
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx]
Offending resource: class path resource [applicationContext.xml]
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:316)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1416)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1409)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:184)
I have the following entry in applicationContext.xml and it works fine inside eclipse:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
Any help is much appreciated.
I even tried changing http://www.springframework.org/schema/tx/spring-tx-3.1.xsd to classpath:/org/springframework/transaction/config/spring-tx-3.1.xsd but it did not help.
It looks like your application contains some jars, like spring-core-3.1.x (because its classes are being used), but it's missing spring-tx-3.1.x.RELEASE.jar (the one that holds Spring Transaction classes).
for my spring-mvc application I created several types of configuration (unittest, integration, qa, production). All the configs are in one war-file, so there is only one type of application I create. Which configuration to take should be decided by the server, where the application is running.
To decide what kind of configuration should be used, I have to look into a file. After that I can decide which configuration should be used by spring mvc.
For now by convention there is always the -servlet.xml used. Is there a way how to decide dynamically which config to take?
Regards,
Michael
Here is a solution that I use. It works very well:
Put the configuration differences in property files.
Keep a single Spring xml with placeholders.
Use PropertyPlaceholderConfigurer to set the properties.
PropertyPlaceholderConfigurer can use system properties to resolve the name of the property file to load.
Set a system property with the name of your environment before initilizing the PropertyPlaceholderConfigurer (you can do this in a bean that reads the value out of your file).
And there you go! The environment will be cleanly detected, and the relevant properties will be loaded!
No need to wait for Spring 3.1, you can use this solution today with 3.0.
I have the same setup, but I use maven to build the WARs differently. I use a PropertyPlaceholderConfigurer in the context:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:datasource.properties" ignore-unresolvable="true" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driver}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<!--other beans-->
</beans>
then I setup an environments folder:
src
--main
----environments
------dev
--------datasource.properties
------cert
--------datasource.properties
------prod
--------datasource.properties
Then in my Maven pom, I use a build profile to copy anything in the environment folder based on a parameter flag in the maven command:
<profiles>
<profile>
<id>environment</id>
<activation>
<property>
<name>environment</name>
</property>
</activation>
<build>
<resources>
<resource>
<directory>
src/main/environments/${environment}
</directory>
</resource>
</resources>
<!-- other build config and plugins -->
so the following command:
mvn clean -Denvironment=dev install
would copy the dev datasource.properties to the war
After all I'm using PropertyPlaceholderConfigurer but slightly differnt than Axel mentioned: I load just one property from my configuration and use it to determine which import to use. Because of https://jira.springframework.org/browse/SPR-1332 I cant use a file to store the instance-type, but have to use environment-variables.
<bean id="propertyConfigurerOne" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>
<import resource="classpath:/web${vabse.Environment}.xml"/>