Apache MQ transportConnectors uri value issue - java

Consider the following code:
<amq:transportConnectors>
<amq:transportConnector uri="${esb.endpoint}"/>
</amq:transportConnectors>
It is not able to resolve the uri value. The error I am getting is :
STACKTRACE:
at com.mincom.util.gadget.Starter.run(Starter.java:40)
THROWABLE: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean
definition with name 'org.apache.activemq.xbean.XBeanBrokerService#0' defined in class
path resource [minestar/esbadapter/service/EsbEmbeddedApacheContext.xml]: Could not
resolve placeholder 'esb.endpoint' in string value "${esb.endpoint}"; nested exception
is java.lang.IllegalArgumentException: Could not resolve placeholder 'esb.endpoint' in
string value "${esb.endpoint}"
Am I doing anything wrong here?

It is quite evident that spring is unable to find the properties file
easiest way is to see the configuration of properties file
my way is to do it like this:
<context:property-placeholder location="required.properties" />
for this you will need to add following in your spring namespace
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
hope it helps,
Good luck!

Related

Context file getting over written in spring

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<import resource="classpath:context2.xml"/>
<import resource="classpath:context3.xml" />
Here Context1.xml is getting over written by context2.xml, context 1.xml and context2.xml are both MDB's. context3.xml is not having issues.
COntext 1 and context2 have some same Bean ID's but have different properties and references.
I found the solution . I was using same Bean ID and thought that it was a pre defined bean ID. I did not have much knowledge on spring . So i studied a bit and found out that u can give any bean ID u want for the class which you are referring to.

java spring context:property-placeholder - set properties paths from placeholder

<context:property-placeholder
location="a.properties,b.properties"
ignore-unresolvable="true"/>
result: both properties file are loaded
<context:property-placeholder
location="${properties_location}"
ignore-unresolvable="true"/>
where properties_location is "a.properties,b.properties"
result: Exception in thread "main" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [a.properties,b.properties] cannot be opened because it does not exist
edit: ${properties_location} is set the following way:
System.getProperties().setProperty("properties_location", "a.properties,b.properties");
ApplicationContext ctx = new GenericXmlApplicationContext("context.xml");
...
How can I initialize my application the 2nd way? to have all the properties file's path defined in a placeholder.
You have to change this to:
<context:property-placeholder
location="classpath:a.properties,
classpath:b.properties"
ignore-unresolvable="true"/>
From the source of the parser for the property-placeholder element.
String location = element.getAttribute("location");
if (StringUtils.hasLength(location)) {
String[] locations = StringUtils.commaDelimitedListToStringArray(location);
builder.addPropertyValue("locations", locations);
}
First the location is retrieved, if that has a value it is converted to a String[]. Springs conversion service takes care of replacing any placeholders in the String[]. But at that moment the properties_location placeholder is just a single element in the array and that gets resolved to a.properties,b.properties without further processing.
So at the moment this isn't possible with placeholders I'm afraid.
One thing that might work is using SpEL if it is always going to be a system property you can use #{systemProperties['properties_location']} to resolve the value. That should be resolved before anything else.
You cant use a property placeholder as a value in a placeholder placeholder resolver. Its like saying, "hey, resolve the placeholder for the location of the all the properties, and then you can start resolving properties!".
Logically it just dosent make sense. I was experimenting with spring property placeholder resolution recently, and stumbled upon this same question. I attempted to use two property placeholder configurers, one to resolve the location of the properties for the second, and the second to resolve the rest of the properties. Of course this dosent work due to the way in which spring initialises its beans.
Initialise bean post processors
Construct them
Construct all other beans
Since the property placeholder configurer is a bean post processor, if you have more than one of them, they get initialised and constructed at the same time, so know nothing of each others properties at construction
Edit
Given that the property location is a system property you could have:
System.getProperties().setProperty("properties_location_a", "classpath:/a.properties");
System.getProperties().setProperty("properties_location_b", "classpath:/b.properties");
And then in your spring content.xml:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>${properties_location_a}</value>
<value>${properties_location_b}</value>
</list>
</property>
</bean>

whats is wrong with AOP using in Spring based project?

It seems that I searched for information from various sources over google to solve my problem, but I am still in trouble.
To clarify my problem I will give you as much information as I can.
I am using NetBeans IDE 7.3.1 and Spring 3.1.1.
What I have now:
I created 6 classes and 1 xml file.
These classes are working fine (because these classes are based from book which I am currently reading).
I suppose something is wrong with my jar files (maybe somthing is wrong with versions compatibility or something (idk, i am new in this scope)).
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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
**http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">**
<bean id="knight" class="castle.BraveKnight">
<constructor-arg ref = "quest"/>
</bean>
<bean id="quest"
class="castle.SlayedDragonQuest"/>
<bean id="mistreal"
class="castle.Mistreal"/>
<aop:config>
<aop:aspect ref="mistreal">
<aop:pointcut id="embark"
expression="execution(* *.embarkQuest(..))" />
<aop:before pointcut-ref="embark"
method="singBeforeQuest"/>
<aop:after pointcut-ref="embark"
method="signAfterQuest"/>
</aop:aspect>
</aop:config>
</beans>
After compiling my code, I am getting this:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [knight.xml]; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice
It is obvious that classloader cannot find my aop class which is called Advice. But why?
I added jars to my project like this:
Did I miss something? Can anyone give me some information. (I read related resources but result is the same).
Thank you
EDITED:
I added aopalliance.jar file, now I am getting this:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'knight' defined in class path resource [knight.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#1': Cannot create inner bean '(inner bean)' of type [org.springframework.aop.aspectj.AspectJAfterAdvice] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#2': Cannot create inner bean '(inner bean)' of type [org.springframework.aop.config.MethodLocatingFactoryBean] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#2': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Unable to locate method [signAfterQuest] on bean [mistreal]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:452)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
It seems that one solution rises another problem. How can I fix it?
P.s. I am not using a Marven. And also this is a mistake in my picture, i fixed it in my project, but it didn't change anything.
Mistreal class:
package castle;
public class Mistreal {
public void singBeforeQuest(){
System.out.println("Singing before quest");
}
public void singAfterQuest(){
System.out.println("Singing after quest");
}
}
Yes, you need the aopalliance library which contains, among others, the org.aopalliance.aop.Advice class. You can get it for maven (or download the jar), here.
If you are using Maven, then spring-aop should bring it in, since it is one of its dependencies. If not, you will have to download it (and anything else) and add it (them) manually to your classpath/build path.
As others have noted, use a single spring-aop jar.
You have a typo
<aop:after pointcut-ref="embark"
method="signAfterQuest"/>
In the above pointcut, you use signAfterQuest but your method is called
public void singAfterQuest(){
System.out.println("Singing after quest");
}
singAfterQuest. The hint is in the logs
Unable to locate method [signAfterQuest] on bean [mistreal] at
For the new error, Spring, by default, uses JDK proxies, but those only work on interfaces. You therefore cannot inject a Proxy into a variable of a class type. Instead you will need to use CGLIB proxies with this change to your XML configuration
<aop:config proxy-target-class="true">
This will also require you add CGLIB and related libraries to your classpath. More details in related answers like here.
I notice that you have both:
spring-aop-3.0.7.RELEASE.jar and spring-aop-3.0.0.RELEASE.jar
Is this intended? Maybe there is some sort of conflict.
Edit: as #SotiriosDelimanolis pointed out, you need the aopalliance jar located here. Maven is definitely a good resource for pulling in required dependencies, and easy to use. I would recommend looking into it.

Set a public variable of bean in spring without using a setter

I read on a few websites that with Spring its possible to do setter based Dependency Injection without having to create a setter for the injected variable it. Would nicely tidy up the code. I read this on another site and also here on stackoverflow.
I've tried it but in my case it does not work. I'm using 3.2.0.RELEASE. I'm getting the following error.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'DI_without_setter' defined in class path resource [SpringBeans.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'url' of bean class [net.comsys.springpropstest.DiWithoutSetter]: Bean property 'url' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
my Java test code
package net.xxx.springpropstest;
public class DiWithoutSetter {
private String url;
}
I've just added it to my main code. Not displayed here. I don't even use DiWithoutSetter in the main code.
SpringBeans.xml
<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">
<bean id="DI_without_setter" class="net.xxx.springpropstest.DiWithoutSetter">
<property name="url" value="jan" />
</bean>
If someone could shed some light on his issue that would be appreciated. Is it possible to set the value of a (public) variable in a Spring bean without using a setter method?
You can't do that (you need a setter), and that tutorial appears to be wrong (note in the example source zip, the classes have getters and setters).
Either add a setter or #Inject the field implicitly using autowiring.
You would need to annotate the field with #Autowired and enable component scanning for this injection to work without a setter method.

Combinating context:property-override and PropertyPlaceholderConfigurer

I have a problem with the followingspring context configuration file:
...
<context:property-override location="classpath:query_1.properties" />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:query_2.properties" />
</bean>
....
The problem is that the properties in the file "query_2.properties" cannot be found. The exception I get ist he following one:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException. Could not resolve placeholder...
Now my question: is it possible that the combination of context:property-override and PropertyPlaceholderConfigurer does make no sense? Can anyone explain me in simple words what is the difference between both? Any help would be appreciated.
Thx. Horace
Property placeholders, normally defined using a <context:property-placeholder location=../> resolves the placeholders in bean definitions:
for eg.
<bean name="myclass" class="MyClass">
<property name="prop1" value="${prop1val}/>
</bean>
if the location specified with property placeholder has a property with name prop1val:
prop1val=aval
then it will be replaced in the bean myclass.
PrpertyOverrideConfigurer defined using <context:property-override location="classpath:query_1.properties" /> on the other is like a push mechanism, the property is of the form beanname.property and it would push this property into the bean with name beanname.
For eg. for the above case if the location had a property of:
myclass.prop1=aval
then it would inject in prop1 of myclass bean
The exception you are getting simply indicates that it is not able to find query_2.properties file, I doubt if it is any other configuration issue.
On which one will take effect if both are defined, I think the last one will the one to take effect.

Categories

Resources