I'm trying to set up a local overrides file for some of my bean definitions. Yes, it's a fragile system, but it's just for testing. Basically, I've got one XML file which looks more or less like:
<?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:util="http://www.springframework.org/schema/util"
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.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- so many beaaaannzzz -->
<utils:list id="partnerList" value-type="my.partner.Class">
<ref bean="previouslyDefinedBean"/>
</utils:list>
<!-- include local bean definition overrides -->
<import resource="file://${user.home}/somedirectory/prefix-*.xml"/>
</beans>
This works in general, but it has an unexpected and undesirable result with the utils:list element.
My somedirectory/prefix-*.xml override file defines another list:
<utils:list id="partnerList" value-type="my.partner.class">
<ref bean="otherBean"/>
</utils:list>
When it is picked up however, I get an undesired result: partnerList has two beans in it, previouslyDefinedBean and otherBean, while I want it to only have the latter.
Now, I'm aware Spring offers some weird weird collection merging, so I tried setting <utils:list merge="false"... but that blew up as an unsupported attribute. Is there something I can do to continue using this override system for the util:list, or have I got to take another tack entirely?
Related
I'm trying to have my project's Strings/ messages stored in an external .properties file. I think I have everything wired up OK, but still I get:
org.springframework.context.NoSuchMessageException: No message found under code 'subtype.user.client' for locale 'null'.
Whenever I try:
String string = messageSource.getMessage("subtype.user.client", null, null);
My spring xml config files are as follows. Since the project is really big with lots of beans, I have different spring xml config files defining different types of beans, and a main spring.config.xml file that wires them all together.
Messages file named messages.subtypes
subtype.user.user=User
subtype.user.client=Client props
subtype.user.staff=Staff
subtype.user.clerk=Clerk
subtype.user.secretary=Secretary
Messages beans file called spring.messages.config.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:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<list>
<value>messages.subtypes</value>
</list>
</property>
</bean>
<bean id="myProjectLangs" class="myprojectbase.MyProjectLangs">
<property name="messageSource" ref="messageSource"></property>
</bean>
</beans>
The main spring.config.xml config file that wires all the beans together via <import resource="classpath:filename.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:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy />
<import resource="classpath:spring.messages.config.xml"/>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource" />
<context:annotation-config />
<context:component-scan base-package="myprojectbase"/>
</beans>
You get this error because you pass the Locale parameter as null. Try
String string = messageSource.getMessage("subtype.user.client", null, Locale.ENGLISH);
Even though you have not defined a file messages.subtypes_en.properties defined it should fall back to messages.subtypes.properties
A couple of things come to mind looking at your code any of which might cause the problem:
Your xml config name contains "." as separators. This is against conventions. Consider renaming your config file to spring-messages-config.xml
Your language properties file has no properties suffix, again convention suggests to name this file messages-subtypes.properties
In both your application context xml files you define a bean named messageSource. Consider deleting one of them.
My prime suspicion as to why your code does not work lies with the way you define basename on ReloadableResourceBundleMessageSource. Looking at the JavaDoc for setBasename method there is some form of convention of configuration at work:
Set a single basename, following the basic ResourceBundle convention of not specifying file extension or language codes, but in contrast to {#link ResourceBundleMessageSource} referring to a Spring resource location: e.g. "WEB-INF/messages" for "WEB-INF/messages.properties", "WEB-INF/messages_en.properties", etc. XML properties files are also supported: .g. "WEB-INF/messages" will find and load "WEB-INF/messages.xml", "WEB-INF/messages_en.xml", etc as well.
This suggests that once you have renamed your message properties file to messages-subtypes.properties, you should change your config to <value>classpath:messages-subtypes</value>, make sure that the file is in the classpath and everything should start working.
Try renaming the messages.subtypes file to messages.subtypes.properties.
I am having a weird issue that I can't seem to track down. I have this working with other servers without a problem, but I can't seem to get this one to work. The closest post that I see to my problem was this post The prefix "context" for element "context:component-scan" is not bound
All others really were just because the prefix was not in the xml file. I am hoping someone might be able to point me in the right direction here.
Spring XML 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:context="http://www.springframework.org/schema/context"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
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-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
<context:annotation-config/>
So I have that, but getting this error:
org.xml.sax.SAXParseException: The prefix "context" for element "context:annotation-config" is not bound.
Appreciate any help. Let me know what else I can provide.
Thanks
I was experiencing the same problem until I realized beans tag attribute xmlns:context was missing. Just added the below lines
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
...."
Then rebuilt the project.
It worked well then on.
The following works for me:
test.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:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
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-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
<context:annotation-config/>
</beans>
When I use the following class to run it:
Test.java
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) throws Exception {
new ClassPathXmlApplicationContext("test.xml");
System.out.println("Finished!");
}
}
Can you please see if this runs for you? You will need the following libraries in the classpath: commons-logging, spring-asm, spring-beans, spring-context, spring-core, and spring-expression.
Please let me know if it worked. If it didn't, please post the full stack-trace. Finally, I used Spring 3.1.1 for the above.
This error comes when you have xmlns:context missing from your spring xml file. So add it. Your beans header should look something like the following -
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd" >
<context:annotation-config />
<context:component-scan base-package="controller" />
</beans>
I encountered the same problem but i was able to solve it by moving
from applicationContext.xml to spring-servlet.xml and adding xmlns:context in the spring-servlet.xml
I try to integrate metro web-service with spring. But I get errors in 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:ws="http://jax-ws.dev.java.net/spring/core"
xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://jax-ws.dev.java.net/spring/core
http://jax-ws.dev.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlet
https://jax-ws.dev.java.net/spring/servlet.xsd">
//Some beans
//!!!!!!!!ERROR
<wss:binding url="/ws">
<wss:service>
<ws:service bean="#newsWebService" />
</wss:service>
</wss:binding>
<bean id="newsWebService" class="com.news.webservice.NewsWebService">
<property name="newsBo" ref="newsBo"/>
</bean>
</beans>
Error message:
Multiple annotations found at this line:
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'wss:binding'.
- schema_reference.4: Failed to read schema document 'https://jax-ws.dev.java.net/spring/servlet.xsd', because 1) could
not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
I add lib x-bean-spring-3.7.jar and jaxws-spring-1.8.jar
Also I check spring.schemas in jaxws-spring-1.8.jar and get
# See XBEAN-60. To work around the overwriting issue, this file is maintained manually.
# this file is used by Spring to resolve resources locally.
# the key is the system ID and the value is the actual file location in this jar.
http\://jax-ws.dev.java.net/spring/core.xsd=spring-jax-ws-core.xsd
http\://jax-ws.dev.java.net/spring/servlet.xsd=spring-jax-ws-servlet.xsd
http\://jax-ws.dev.java.net/spring/local-transport.xsd=spring-jax-ws-local-transport.xsd
I included shemas according with this.
#UPDATED
I found something strange. I open in spring-beans-3.2.0.M1.jar spring.shemas
http\://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd
http\://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd
http\://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd
http\://www.springframework.org/schema/beans/spring-beans-3.1.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd
http\://www.springframework.org/schema/beans/spring-beans-3.2.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd
http\://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd
I get applicationContext.xml without error just with http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd next pair
Please, help me find reason.
EDIT: The previous answer was accepted by eclipse, but triggered an exception at runtime. Here is my final working config
Use this, the xsd schemas have been moved :
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ws="http://jax-ws.dev.java.net/spring/core"
xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://jax-ws.dev.java.net/spring/core http://jax-ws.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlet http://jax-ws.java.net/spring/servlet.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
default-lazy-init="true">
The java.net hosting web master administrator has caused problems by changing hosting paths.
Change all jax-ws.dev.java.net to jax-ws.java.net and the error disappears as on 28-Aug-2014.
It appears, thousands of developers time got wasted due to this.
Try replacing the line: http://www.springframework.org/schema/beans/spring-beans.xsd as it conflicts. Had same issue with similar configuration borrowed off an example on the Internet and this resolved it.
The head of the content of your applicationContext.xml should be:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ws="http://jax-ws.dev.java.net/spring/core" xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://jax-ws.dev.java.net/spring/core
http://jax-ws.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlet
http://jax-ws.java.net/spring/servlet.xsd">
The problem is because there isn't exist the URL http://jax-ws.dev.java.net/.
The project has been relocated and probably discontinued. Then, I suggest an alternative solution.
Download to local the files (spring-jax-ws-core.xsd,spring-jax-ws-servlet.xsd) from the source jar in this URL https://repo1.maven.org/maven2/org/jvnet/jax-ws-commons/spring/jaxws-spring/1.9/jaxws-spring-1.9-sources.jar
I have placed in the path "W:/dev/Spring4WebApp/", you can choose another one. I recommend you to upload and publish them in any own server in your company.
Modify the appContext with the following lines:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
xmlns:ws="http://jax-ws.dev.java.net/spring/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://jax-ws.dev.java.net/spring/core
file:///W:/dev/Spring4WebApp/spring-jax-ws-core.xsd
http://jax-ws.dev.java.net/spring/servlet
file:///W:/dev/Spring4WebApp/spring-jax-ws-servlet.xsd">
Note the lines starting with "file:///". These lines reference to your local files.
And voilà!! It runs!
I have the following for the usage of a #Cacheable in spring (3.1):
spring:
<?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:oauth="http://www.springframework.org/schema/security/oauth2"
xmlns:sec="http://www.springframework.org/schema/security"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/security/oauth2
http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache" />
<!-- Ehcache library setup -->
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:ehcache.xml" />
Maven:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.5.3</version>
</dependency>
The to be cached method:
#Cacheable(value="cahceName", key="concat(#param1).concat(‘-’).concat(#param2)")
public String cachedMethod(String param1,String param2)
Alas, when I debug the code, I see that the cached method gets called more than once even when param1 and param2 are the same (i.e the cahce is not used).
Any ideas?
The key does not appear correct -
You may have meant - #Cacheable(value="cacheName", key="#param1.concat(‘-’).concat(#param2)")
Further, if the compilation is done without debug information, the param1, param2 argument names will not be available to expression evaluator. Instead you can refer to them using p0, p1 etc this way:
#Cacheable(value="cahceName", key="#p0.concat('-').concat(#p1)")
Update:
I have a one page test here which demonstrates how this works - https://gist.github.com/3315275
In my case, the problem was caused by using the wrong configuration of the cache provider (Caffeine):
#Bean
public Caffeine<Object, Object> caffeineCacheBuilder() {
return Caffeine.newBuilder()
.initialCapacity(100)
.maximumSize(1000)
.expireAfterAccess(10, TimeUnit.MINUTES)
.weakKeys(); // cause problems when concatenated keys used
}
As the docs says, weakKeys() method:
Specifies that each key (not value) stored in the cache should be wrapped in a WeakReference (by default, strong references are used).
Warning: when this method is used, the resulting cache will use identity ({#code ==})
comparison to determine equality of keys. Its {#link Cache#asMap} view will therefore
technically violate the {#link Map} specification (in the same way that {#link IdentityHashMap}
does).
I need to write a applicationContext.xml for my Spring Framework 3 application, but I cann't find out its XML Declaration. Anyone could show me place to get it?
Likewise, is there a common place defining declarations for all Spring XMLs? (Or other XMLs being around.)
Below is a declaration example for Spring 2.5 I found out by googling:
<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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
</beans>
This is all described in the Spring documentation see 3.2.1 Configuration metadata for the basic structure and Appendix C. XML Schema-based configuration for details of the other declarations you could use.