I'm training about the combo Restlet/Spring but there are some things i still don't understand... I hope you can help me.
In fact, i'm trying to the use Inject dependancies systeme of Spring with Restlet (Like in this tutorial : http://restlet.com/technical-resources/restlet-framework/guide/2.3/introduction/getting-started/maven-spring). So I tried to do it myself but that didn't work. My code returns this exception :
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'basecampComponent' defined in ServletContext resource [/WEB-INF/applicationContext.xml]:
Cannot resolve reference to bean 'basecampAppliction' while setting bean property 'defaultTarget'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'basecampAppliction' defined in ServletContext resource [/WEB-INF/applicationContext.xml]:
Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'root' of bean class[com.mycompany.restlet.basecamp.application.BaseCampApplication]:
Bean property 'root' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
So i was looking for the file "ApplicationContext.xml" and this is his content :
<bean id="basecampComponent" class="org.restlet.ext.spring.SpringComponent">
<property name="defaultTarget" ref="basecampAppliction" />
</bean>
<bean id="basecampAppliction">class="com.mycompany.restlet.basecamp.application.BaseCampApplication">
<property name="root" ref="router" />
</bean>
<!-- Define the router -->
<bean name="router" class="org.restlet.ext.spring.SpringBeanRouter" />
Someone has an idea where could I look for a way to debug this ?
By the way, i'm in Java 1.8.0_60.
Thanks for all your helps.
Benjamin
After looking for some informations over the web, i've a hypothesis how I solved this.
On this link (Spring & Restlet : 100% XML configuration?), he's binding the router with the property "inboundroot" of Application. So I think there's a minimal changed (not noticed in the tutorial). In fact, i tried the project proposed in the archive (doesn't work) and the way you code yourself the tutorial. This is again two solutions.
The final solution consists so by changing the name of the property to "inboundroot" to "root".
"Never trusts the tuto"
Thanks for the time you took to help me.
I think that there is no root attribute in the Application class. You should add one in your BaseCampApplication class and use it to configure your application (see createInboundRoot method), as described below:
public class BaseCampApplication extends Application {
private Restlet root;
public Restlet createInboundRoot() {
return root;
}
public void setRoot(Restlet root) {
this.root = root;
}
}
Hope it helps you,
Thierry
Related
Im trying to follow the guide for Hybris123 version 19.05, but when creating new services I get a problem where the spring framework does not recognize my service.
I tried to change the neme of the variable but honestly Im not sure what to do.
The errorIm getting is "org.springframework.beans.FatalBeanException: Context hybris Global Context Factory couldn't be created correctly due to, Error creating bean with name 'applicationEventMulticaster': Unsatisfied dependency expressed through method 'setAllDecorators' parameter 0; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [concerttours.service.impl.DefaultBandService] for bean with name 'defaultBandService' defined in class path resource [concerttours-spring.xml]; nested exception is java.lang.ClassNotFoundException"
The code Im using in spring is:
<alias name = "defaultBandService" alias = "DefaultBandService"/>
<bean id = "defaultBandService" class = "concerttours.service.impl.DefaultBandService" >
<property name = "bandDAO" ref = "bandDAO" />
</bean>
And when I use the IDE for looking a file with the name DefaultBandService it recognizethe service that Im trying to use but the spring framework does not. In the service tried to use a spring Tag like component but it didnt work.
I expect spring to recgonize the bean and let me run the hybris server
Use your IDE to look for the class(i.e DefaultBandService).
Check the package of the class(most probably the first line of the file).
Make sure that the package is specified correctly in your spring xml File.
Make sure that the class is in the same extension as the Spring file. If it is not, then a dependency needs to be added in extensioninfo.xml.
Run "ant clean all" and start the Server again.
I have a spring configuration with a property placeholder directive attribute that is not being recognised. I have verified that the actual property can be resolved by creating a simple class that outputs the property:
<bean id="propTest" class="com.johnsands.unite.PropertyLogger">
<constructor-arg value="security.configuration"/>
<constructor-arg value="${security.configuration}"/>
</bean>
This simple bean outputs the given parameters, which it does just fine. But when I try to use that same property in an import, as in the following:
<import resource="${security.configuration}"/>
I get the following error.
org.springframework.beans.factory.BeanDefinitionStoreException:
Unexpected exception parsing XML document from ServletContext resource
[/WEB-INF/spring/root-context.xml]; nested exception is
java.lang.IllegalArgumentException: Could not resolve placeholder
'security.configuration' in string value "${security.configuration}"
This does not make sense as I have verified the property can be resolved. It also appears that the directive should be supported as spring is trying to resolve the placeholder but is not able to.
I have got a SPRING application. When I run
mvn jetty:run
everything is ok.
I would like to use JMX in my project.
I created another project, I tried tutorial for beginners and I was able to see some changes with jconsole.
Now, I want to use JMX in my real project and I would like to use SPRING libraries which manages JMX - following this post
How to integrate JMX with Spring?
I have got a class:
public class MyMainClass {
private int var1;
private int var2;
private TimeUnit var3;
// public getters and setters
public static MyXXXClass<String, Object> getInstance();
}
and in config.xml
<!-- other beans -->
<bean id="myid" class="com.my.package.MyMainClass">
<property name="var1" value.../>
<property name="var2" value... />
<property name="var3" value.../>
</bean>
<!-- other beans -->
I changed few things to make it works with JMX.
I added an interface:
import java.util.concurrent.TimeUnit;
public interface IMyMainClassBean {
public int getVar1();
public void setVar1(int var1);
public int getVar2();
public void setVar2(int var2);
public TimeUnit getVar3();
public void setVar3(TimeUnit var3);
}
I added implements to my class:
public class MyMainClassBean implements IMyMainClassBean {...}
Last thing, I edited my xml file:
<!-- other beans -->
<bean id="myid" class="com.my.package.MyMainClassBean">
<property name="var1" value.../>
<property name="var2" value... />
<property name="var3" value.../>
</bean>
<!-- this bean must not be lazily initialized if the exporting is to happen -->
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"
lazy-init="false">
<property name="beans">
<map>
<entry key="bean:name=testBean1" value-ref="myid" />
</map>
</property>
</bean>
<!-- other beans -->
Now, when I start my server, it gives me a lot of exceptions (log is really long, so I copied just a part which I think is the most important).
Caused by: org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [com.my.package
.MyMainClassBean#3d4395fb] with key 'bean:name=testBean1'; nested exception is javax.management.InstanceAlreadyExistsExcep
tion: bean:name=testBean1
at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:602)
at org.springframework.jmx.export.MBeanExporter.registerBeans(MBeanExporter.java:527)
at org.springframework.jmx.export.MBeanExporter.afterPropertiesSet(MBeanExporter.java:413)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableB
eanFactory.java:1571)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBean
Factory.java:1509)
... 163 more
Caused by: javax.management.InstanceAlreadyExistsException: bean:name=testBean1
at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:453)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.internal_addObject(DefaultMBeanServerInterceptor.java:1484)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:963)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:917)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:312)
at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:483)
at org.springframework.jmx.support.MBeanRegistrationSupport.doRegister(MBeanRegistrationSupport.java:195)
at org.springframework.jmx.export.MBeanExporter.registerBeanInstance(MBeanExporter.java:655)
at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:592)
... 167 more
[WARNING] Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myTask' defined
in class path resource [anotherconfigfile.xml]: Cannot resolve reference to bean 'anotherimport' while setting bean property 'targetObj
ect'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'anotherimport' defin
ed in class path resource [anotherconfigfile.xml]: Cannot create inner bean 'myTotallyAnotherClass' of type [com.my.package.another.MyTotallyAnotherClass]
while setting bean property 'myTotallyAnotherClass'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creatin
g bean with name 'myTotallyAnotherClass' defined in class path resource [anotherconfigfile.xml]: Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.my.package.another.MyTotallyAnotherClass]: Co
nstructor threw exception; nested exception is java.lang.ExceptionInInitializerError:
javax.management.InstanceAlreadyExistsException: bean:name=testBean1
at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:453)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.internal_addObject(DefaultMBeanServerInterceptor.java:1484)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:963)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:917)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:312)
at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:483)
at org.springframework.jmx.support.MBeanRegistrationSupport.doRegister(MBeanRegistrationSupport.java:195)
at org.springframework.jmx.export.MBeanExporter.registerBeanInstance(MBeanExporter.java:655)
at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:592)
at org.springframework.jmx.export.MBeanExporter.registerBeans(MBeanExporter.java:527)
at org.springframework.jmx.export.MBeanExporter.afterPropertiesSet(MBeanExporter.java:413)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableB
eanFactory.java:1571)
I have no even idea how to debug it.
Thank you for all your hints.
Caused by: javax.management.InstanceAlreadyExistsException: bean:name=testBean1
This is trying to tell you that you have 2 beans with the same name ObjectName of bean:name=testBean1 being register with the MBeanExporter. However, unless there are other XML files or more entries in your beans map then there should not be.
I have no even idea how to debug it.
You could put a breakpoint in the JmxMBeanServer.registerMBean(...) method to see what beans are being registered to see if you can figure out why you are getting a duplicate.
As an aside, my SimpleJMX library is an easy way to export your beans via JMX. There is pretty good Spring support as well. Here are the documentation about using with Spring. There is also a Spring test program which demonstrates what you need to do to get it working. Here's the Spring XML file.
This is happening as the bean is trying to initialize even after its initialized once.
I was facing the same issue with RabbitMQ configuration in Spring TestNG testcases.
Use #DirtiesContext as a class level annotation for every testcase class.
This will create applicationcontext and kill it once the testcase class is run and a new applicationcontext will be created for the next testcase class.
Example -
#Test
#ContextConfiguration(classes = { ApplicationConfig.class })
#DirtiesContext
#WebAppConfiguration
public class ATest extends AbstractTestNGSpringContextTests{
#BeforeSuite
public void setup() throws Throwable {
}
#AfterSuite
public void teardown() {
}
}
I am trying to export an interface (ITestRunable) I am sure I have linked everything correctly in the context.xml and osgi.xml files but it just wont work. The error is:
Application context initialization for 'me.overlaymanagement' has timed out waiting for (objectClass=me.overlaymanagement.testing.ITestRunable)
Any got any idea?
Code: Spring files for ITestRunable
context.xml
<bean id="testingRunable" class="me.overlaymanagement.testing.Testing"
init-method="startUp" destroy-method="shutDown">
</bean>
osgi.xml
<osgi:service id="testingRunable" ref="testingRunable">
<osgi:interfaces>
<value>me.overlaymanagement.testing.ITestRunable</value>
</osgi:interfaces>
</osgi:service>
Spring files for import into main system
context.xml
<bean id="overlaymanagementsystem"
class="me.overlaymanagementsystem.OverlayManagementSystem"
init-method="startUp" destroy-method="shutDown">
<property name="testingRunable" ref="testingRunable"/>
</bean>
osgi.xml
<osgi:reference id="testingRunable" cardinality="1..1"
interface="me.overlaymanagement.testing.ITestRunable">
</osgi:reference>
Gettors and Settors for ITestRunable within main system
public class OverlayManagementSystem{
...
protected ITestRunable testingRunable;
....
public ITestRunable getTestRunable() {
return testingRunable;
}
public void setTestRunable(ITestRunable testingRunable) {
this.testingRunable = testingRunable;
}
}
can anyone see a problem?
Ok so i managed to find the poblem. It was two things actually:
It didnt like that i had set the bean id and the service id where the ITestRunable code is located. Once i changed the service id name to something different it gave me a new error about invalid gettors and settors, which is the next point.
after some research i found the your gettor and setter for the imported service must be that same as the beans property name for that service. notice in my bean its:
< property name="testingRunable" .../>
and the get/set are defined as: "getTestRunable/setTestRunable". Changing these to getTestingRunable and setTestingRunable solved the problem for me.
I hope this helps others.
I am working on an application where I have two classes both implementing a common interface. So in time of bean declaration, I am going to mark one of them primary in my app-context.xml file. I can achieve this by simply declaring the primary bean like this:
<bean id="oracleImpl" class="com.me.dao.OracleImpl" primary="true">
</bean>
Now I don't want to hard code which of the beans is going to be the primary bean, rather want to read the true/false value from a properties file. So I went like this:
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="oracleImpl" class="com.me.dao.OracleImpl" primary="${oracle.primary}">
</bean>
<bean id="pgsqlImpl" class="com.me.dao.PgsqlImpl" primary="${pgsql.primary}">
</bean>
The values of oracle.primary and pgsql.primary are defined in the file jdbc.properties along with other jdbc (non-boolean) properties.
But it doesn't work and says, "'${oracle.primary}' is not a valid value for 'boolean'"
I have a feeling it is something to do with the xsd validators. Browsing through this site and google gave me this much idea, but got no real solution. Can any body help?
This will not work.
As of 3.2.5.RELEASE only the following bean definition elemets support property placeholder:
parent name
bean class name
factory bean name
factory method name
scope
property values
indexed constructor arguments
generic constructor arguments
See the BeanDefinitionVisitor's visitBeanDefinition method for the details. This method is used by the PlaceholderConfigurerSupport.
I would recommend you to create a feature request in the spring issue management system.
PS: if you create an issue please add a comment to the issues url.