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
Related
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,
I am interested to know the classes used in spring integration tag so that I can get more details of the tags by going through the javadoc of the classes.
I have two basic questions:
Do the spring integration xml tags (for example stdin-channel-adapter) convert to <bean class=".." /> tags?
how to figure out the bean class associated with the spring integration tags?
Here is a simple example of spring integration xml context file:
<?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:int="http://www.springframework.org/schema/integration"
xmlns:int-stream="http://www.springframework.org/schema/integration/stream"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.0.xsd
http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream-4.0.xsd">
<int-stream:stdin-channel-adapter id="producer" channel="messageChannel" />
<int:poller id="defaultPoller" default="true" max-messages-per-poll="2" fixed-rate="100" />
<int-stream:stdout-channel-adapter id="consumer" channel="messageChannel" append-newline="true" />
<int:channel id="messageChannel" />
</beans>
Thanks
I think you have some mistake in your first question. See Andreas' comment.
Anyway the answer for you is like.
Any custom tags in Spring are handler by the particular NamespaceHandler. Typically you can find the target impl in file like META-INF/spring.handlers in the particular Spring jar, e.g.:
http\://www.springframework.org/schema/integration/stream=org.springframework.integration.stream.config.StreamNamespaceHandler
With that in hands you can find the code like:
this.registerBeanDefinitionParser("stdin-channel-adapter", new ConsoleInboundChannelAdapterParser());
Where you can determine that a ConsoleInboundChannelAdapterParser is responsible for parsing and instantiation beans for <stdin-channel-adapter> tag.
And there you can find the code like:
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
"org.springframework.integration.stream.CharacterStreamReadingMessageSource");
So, the real class for target bean instance is CharacterStreamReadingMessageSource. But that's not all.
Please, look here for the design and model: http://docs.spring.io/spring-integration/docs/4.3.0.RELEASE/reference/html/overview.html#programming-tips
I am using some third-part jars that PARTLY migrated to spring. The pain point is that there are lots of initialization module that mot migrated to spring. There initialization module need to be executed first before lots' of beans creation.
I also read Spring 3 bean instantiation sequence, the problem here is, the third part library uses #Component for bean creation (which depends on initialization module that not migrated to spring, wired right...?).
Now I can write a spring bean to wrap all initialization modules. And create the bean before beans that need it.
So is there a way to specify the bean creation sequence to create the initialization bean first?
Also I checked some documents, spring bean creation is in single thread so this would work.
You can use BeanPostProcessor and add your initizaling module as the dependency
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean class="com.foo.CustomBeanPostProcessor" depends-on="com.foo.InitModuleBean"/>
<bean class="com.foo.BarBean" />
<bean id="com.foo.InitModuleBean" class="com.foo.InitModuleBean" />
</beans>
you put the wraperBean that initialize as the first Bean in your xmlConfig file then spring will process the initialization by initializing that Wraper An other solution is to add depends-on attribute to all your beans that depend on that Wrapper and you specify the id of that wrapper bean inside the attribute depends-on like this
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="modules" class="com.mycompany.ModulesWrapper" />
<bean class="com.foo.ClientBean" depends-on="modules" />
</beans>
I am inheriting a project from a developer who left, and I am trying to understand GWT and Spring Framework.
The original problem that lead me to this path: GWT had one module where I loaded ALL third party javascripts... that could result in conflicts. Example, I would include chart drawing libraries, etc. all in one page.
Possible solutions: Have the chart drawing library in an iframe so that it would not conflict with other third party libraries of javascript... OR open the page in a new window.
I decided to go with a new window.
So I did this:
Window.Location.assign(GWT.getHostPageBaseURL()
+ "chartModule.html?gwt.codesvr=127.0.0.1:9997/");
However, in my new chartModule.java (GWT) the problem I have is I do not have the beans/classses defined in (Spring framework) applicationContext.xml anymore:
#Autowired
ApplicationContext applicationContext;
And applicationContext is null after I have changed the host page url... so I do not have any beans that I tried autowiring...
Is it possible to reload the beans from applicationContext.xml??
Here is my applicationContext.xml:
<?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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="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">
<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />
<!-- This file has properties that are used by other XML files loaded via ${var name} syntax -->
<context:property-placeholder location="/WEB-INF/classes/environment.properties" />
<import resource="spring-security-cas.xml" />
<!-- Scans the classpath for annotated components that will be auto-registered
as Spring beans. For example #Controller and #Service. Make sure to set the
correct base-package -->
<context:component-scan base-package="com.javamango.sixtydegrees" />
<import resource="mongo-config.xml" />
<import resource="rabbitmq-context.xml" />
<import resource="spring-mail.xml" />
</beans>
You cannot use spring beans on client side. If you want retriewe some data from spring in gwt, you can do this at two ways:
1) use server side library like gwt-sl to inject spring beans in gwt servlet
#Service("greetingService")
public class GreetingServiceImpl extends RemoteServiceServlet implements GreetingService
{
#SuppressWarnings("unused")
private static final Logger LOGGER = LoggerFactory.getLogger(GreetingServiceImpl.class);
#Autowired
UserFileService userFileService;
#Autowired
UserService userService;
}
now you can autowire spring beans and obtain data via gwt-rpc
2) put data via jsp in hidden html form fields and retriewe data from it
<input type="hidden" value="7" id="documentid"/>
String id = (InputElement) (Element) DOM.getElementById("documentid").value
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).