Spring FileSystemXmlApplicationContext getBeanDefinitionNames returns empty array - java

I have an active profile problem.
Decided to look in debug mode on my context
(profile indepenent contexts works in such way to in my case -
getBeanDefinitionNames returns empty array).
new FileSystemXmlApplicationContext with mentioned file works without exceptions in code fragment mode.
Code
new FileSystemXmlApplicationContext(
"D:\\Projects\\opti\dao\\src\\main\\resources\\contexts\\" +
"\\profiles\\test.xml")
This context with
getBeanDefinitionNames()
returns empty array.
D:\Projects\opti\dao\src\main\resources\contexts\profiles\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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<beans profile="test">
<context:property-placeholder properties-ref="properties" ignore-resource-not-found="false"/>
<bean id="properties" lazy-init="false"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:properties/test-database.properties</value>
</list>
</property>
</bean>
</beans>
</beans>
Spring 4.2.5
D:\Projects\opti\dao\src\main\resources\properties\test-database.properties
connection.driverClassName=org.hsqldb.jdbc.JDBCDriver
connection.url=jdbc:hsqldb:mem:test
connection.userName=sa
connection.password=

For use beans of a specific profile you should start the jvm with -Dspring.profiles.active=test, or setting spring.profiles.active=test as env variable System.setProperties(props) could be a way for configure it.
I hope that this can help you

Related

getting error in simple spring example

My spring library : 3.2.0
Xml file :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<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-3.2.xsd">
<bean id = "res_Bean" class = "Restaurant"
<property name="welcomeNote" value="welcome to my restaurant"/>
</bean>
</beans>
Im trying to solve , but im not getting correct ans.
Exception in thread "main" java.lang.IllegalStateException:BeanFactory
not initialized or already closed - call 'refresh' before accessing
beans via the ApplicationContext at
org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:172)
at
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1117)
at TestSpring.main(TestSpring.java:12)
When you define bean attribute class should be equals to full package path of the class.
For example
<bean id="restaurant" class="com.models.Restaurant">
<property <property name="welcomeNote" value="welcome to my restaurant"/>>
</bean>

Error loading messages from .properties file

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.

Linking endpoint in RestTemplate

I have written an application that calls a service through Spring's RestTemplate.
It's working fine when I call the URL http://localhost:8081/index/myservice directly.
I tried configuring an endpoint using application.properties, how do I link it to the method calling the URL ? As it is now, It doesn't have any effect.
application.properties
app.endpoint = http://localhost:8081/index/
I want to directly call app.endpoint instead of writing the URL.
Update : 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Rest Template -->
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
</bean>
<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list><value>classpath:application.properties</value></list>
</property>
So to call the URL from the method : You can use the #Value annotation like this :
#Value("${app.endpoint}")
private String appEndpoint;
The configuration in your XML :
<context:property-placeholder location="classpath:placeholder.properties"/>
HTH,
Gyan

Why is the ${} variable not interpreted as a constructor-arg value, in my context file?

I have a Spring 3.1 application, and I try to use a system variable in the context files. The variable "JAVA_MY_ENV" is defined on my System (on Windows, it is in the "System variables", from the control panel).
In web.xml, I can use it as a variable and it works, it is successfully replaced by the actual value of the variable (let's say "electrotype") :
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log/${JAVA_MY_ENV}.log4j.properties</param-value>
</context-param>
I can also use it in my main "bean" context, to do an import, and it also works :
<?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: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/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- (...) -->
<import resource="classpath:spring/app-config.xml" />
<import resource="classpath:spring/env/context-env-${JAVA_MY_ENV}.xml" />
</beans>
But In "app-config.xml", one of my other context file, I try this and it doesn't work :
<?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: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/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<bean id="appConfiguration" class="com.xxx.app.AppConfiguration">
<constructor-arg value="${JAVA_MY_ENV}" />
</bean>
</beans>
The com.xxx.app.AppConfiguration receives the String "${JAVA_MY_ENV}" as the constructor parameter, not the interpreted value of it!
I'm not sure to understand where the ${} variables are interpreted and where they are not.
Is there a way I can pass the interpreted ${JAVA_MY_ENV} value to my com.xxx.app.AppConfiguration constructor?
As of 3.0 in Spring you should be to inject values into properties
#Value("#{ systemProperties['JAVA_MY_ENV'] }")
private String myVar;
or
<property name ="myVar" value="#{systemProperties['JAVA_MY_ENV']}"/>
Alternatively you can look into using the PropertySourcesPlaceholderConfigurer or similar class. Creating this will tell spring how to look for variables. Often i make a number of property files as well so that environment and internal property file values are available to the app. e.g.
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:someprops.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="true" />
<property name="searchSystemEnvironment" value="true" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
A key element in the above example is the "searchSystemEnvironment" being set to true. This tells spring to use env variables (which is what you want)
Possible duplicate of <context:property-placeholder> properties not accessible to the child (web) context
As I understand this is expected behavior. You should inject beans only in servlet context or to include configuration bean into servlet-context.xml
To use a PropertyPlaceholderConfigurer, suggested by BruceLowe, works. Another way (maybe more complicated) I found to resolve a specific property is using :
<bean id="JAVA_MY_ENV" class="org.springframework.util.SystemPropertyUtils" factory-method="resolvePlaceholders">
<constructor-arg value="${JAVA_MY_ENV}" />
</bean>
This creates a String bean containing the resolved value of ${JAVA_MY_ENV}!
Then I can use this bean anywhere a bean ref can be used. For example as a constructor-arg:
<bean id="appConfiguration" class="com.xxx.app.AppConfiguration">
<constructor-arg ref="JAVA_MY_ENV" />
</bean>
So now I use ${JAVA_MY_ENV} in places where it is interpreted, without adding PropertyPlaceholderConfigurer, and the JAVA_MY_ENV bean otherwise.

how to configure properties for mvc spring bean controller?

Is there a special way for doing this?
What i got is:
config.properties with param.key=value
web.xml with ContextLoaderListener that reads the configuration
pages-servlet.xml that defines servlet beans.
What I want is to configure one of the beans in pages-servlet.xml with param.key.
I'm using <property name="myField" value="${param.key}"/> in the xml but I see that the field is configured with ${param.key} instead of 'value'.
What is the right way to configure the bean?
Ok, I solved it by importing application context file that defines configuration bean into pages-servlet.xml.
It works, but seems very wrong.
Property placeholder is what you want.
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:property-placeholder location="classpath:/config.properties" />
<bean id="mybean" class="...">
<property name="xxx" value="${prop.value}" />
</bean>
</beans>

Categories

Resources