It is useful to have different property sets for different users.
<?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-3.0.xsd">
<context:property-placeholder
location="classpath:/path/to/package/default.properties,
classpath:/path/to/package/#{ systemProperties['user.name'] }.properties"/>
</beans>
When executing the application, spring does not recognize the expression. The context does not start and spring says: class path resource [path/to/package/#{ systemProperties['user.name'] }.properties] cannot be opened
When I replace the expression manually with a string resulting in a valid resource then the behaviour is as expected. The manual states it should work.
The spring-context and spring-core (3.1.2-RELEASE) are in classpath.
How come spring does not pick up the environment variable?
I'm open to alternate solutions solving the same functional problem.
SpEL expressions are not allowed there; you can do what you want indirectly, though...
<context:property-placeholder properties-ref="props"/>
<util:properties id="props" location="classpath:#{systemProperties['foo']}"/>
Here is the complete answer to the question. Keeping the override of user properties over default properties. My edit of the accepted answer got rejected.
<context:property-placeholder properties-ref="springContextCongifurationProperties"
location="classpath:/path/to/package/default.properties"
local-override="true"/>
<util:properties id="springContextCongifurationProperties"
location="classpath:/path/to/package/#{ systemProperties['user.name'] }.properties"/>
Related
I have a problem with Environment env.getproperty, env not find local properties, but it find system properties. I dont know about this so much and I need to solve it. Please, help me.
Attached my code and its configuration.
Controllers.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: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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- Scans within the base package of the application for #Components to
configure as beans -->
<mvc:annotation-driven />
<context:component-scan base-package="com.mret.client.controller" />
<context:component-scan base-package="com.mret.client.security" />
<context:property-placeholder location="classpath*:paremeters.properties" />
Paremeters.properties:
url.services.search=http://localhost:8080/mretcore/search
url.services.orderdetail=http://localhost:8080/mretcore/orderdetail?orderid=
Controller:
#Controller
public class OrdersController {
RestClient restClient = new RestClientImpl();
#Autowired
private Environment env;
String url = env.getProperty("url.services.search");
etc....}
enter image description here
property-placeholder does not place properties into env. It is for variables set on OS level.
better use System.getProperty("property_name") which includes env, JVM properties, -D properties passed into java command line and yes - those from property-placeholder.
Also you may consider to use your url as bean property and do not get it explicitly, but by spring bean property definition like ${url.services.search}
instead of having property name in code. After time it may hard to find where what property loaded. It is better approach...
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'm trying to implement Spring's repositories in my DAL. I'm following this guide.
At the third step of "1.2 Query methods" paragraph we need to activate the repository package scanning with the following XML configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<repositories base-package="com.acme.repositories" />
</beans>
In my project I'm using only java config so this declaration is a little problematic for me. What is the right way to make things done in java conf in this case?
Thank you
You need to add the following annotation to your configuration class:
#EnableJpaRepositories("com.acme.repositories")
I have defined the following transaction manager:
<tx:annotation-driven transaction-manager="txManager" mode="aspectj" />
and have the following root element:
<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-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
Everything works fine, but IntelliJ gives me a error marker on
mode="aspectj"
saying it is not allowed. I have followed where it gets the xsd from, and it links to the tx 2.0 xsd - which explains the error message, as I need 2.5 to use the mode annotation.
Is it possible to somehow give IntelliJ a hint that i should validate toward 2.5 rather than 2.0?
If you open up the jar file that the schemaLocation should point to the xsd according to this screen shot:
Then you'll see that IntelliJ has a bunch of xsd files for different versions of Spring:
This means that you really have all the schemas you need.
If your bean definitions file has problems then your schemaLocation must point to the wrong version in the Spring jar file:
Check the Settings | Schemas and DTDs and verify that you haven't accidentally manually set it to point to the wrong xsd file:
If it is wrong then you'll have to remove that line using the minus sign. This will cause IntelliJ to go back to it's default values:
After that you should be seeing the same thing as in the first screen shot.
I was having a similar problem trying to use spring security and beans in the same xml config. IntelliJ was insisting on validating using the security xsd version 2.0 despite everything telling it to use version 3.1.
I was able get IntelliJ to figure it out by changing the default namespace between security and beans.
I originally had:
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
So I switched it to:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
After that IntelliJ was validating using the correct xsd. I'm not sure if this was a bug with IntelliJ or something I was doing wrong, but it works now.
Hopefully this helps someone in the future.
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.