Spring bean injection

3,230 views
Skip to first unread message

David Salvador

unread,
Mar 27, 2014, 11:56:39 AM3/27/14
to kur...@googlegroups.com
Hello guys,

Can you please give more information how Sprinb Bean injection can be done, in order to use the media-api from a standalone application?

what I'm doing is pretty much what you have on Users and Programmers guide, but i always get the same error. 

I only have a class with:

public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext context = new AnnotationConfigApplicationContext("/home/david/Desktop/kmf-media-config.xml");
        MediaPipelineFactory mpf = context.getBean(MediaPipelineFactory.class);
    }
}

my kmf-media-config.xml has:

<?xml version="1.0" encoding="UTF-8"?>

   <context:annotation-config />
   <context:component-scan base-package="com.kurento.kmf" />

    <bean id="mediaApiConfiguration" class="com.kurento.kmf.media.MediaApiConfiguration">
        <property name="handlerAddress" value="127.0.0.1" />
        <property name="handlerPort" value="9191" />
    </bean>
    <bean id="thriftInterfaceConfiguration" class="com.kurento.kmf.thrift.ThriftInterfaceConfiguration">
        <property name="serverAddress" value="127.0.0.1" />
        <property name="serverPort" value="9090" />
    </bean>
</beans>

and when i run the program i always have the following output: 

15:49:18.564 [main] DEBUG o.s.core.env.StandardEnvironment - Initializing new StandardEnvironment
15:49:18.573 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
15:49:18.577 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
15:49:18.578 [main] DEBUG o.s.core.env.StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
15:49:18.640 [main] DEBUG o.s.c.a.ClassPathBeanDefinitionScanner - JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
15:49:18.643 [main] DEBUG o.s.c.i.s.PathMatchingResourcePatternResolver - Resolved location pattern [classpath*:/home/david/Desktop/kmf-media-config/xml/**/*.class] to resources []
15:49:18.647 [main] INFO  o.s.c.a.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5b4975d2: startup date [Thu Mar 27 15:49:18 WET 2014]; root of context hierarchy
15:49:18.650 [main] DEBUG o.s.c.a.AnnotationConfigApplicationContext - Bean factory for org.springframework.context.annotation.AnnotationConfigApplicationContext@5b4975d2: org.springframework.beans.factory.support.DefaultListableBeanFactory@3708ab98: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy
15:49:18.669 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
15:49:18.670 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
15:49:18.703 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' to allow for resolving potential circular references
15:49:18.707 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
15:49:18.745 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
15:49:18.745 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
15:49:18.746 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' to allow for resolving potential circular references
15:49:18.747 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
15:49:18.747 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
15:49:18.747 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
15:49:18.748 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' to allow for resolving potential circular references
15:49:18.748 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
15:49:18.748 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
15:49:18.749 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
15:49:18.765 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor' to allow for resolving potential circular references
15:49:18.766 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
15:49:18.766 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor'
15:49:18.766 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor'
15:49:18.767 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor' to allow for resolving potential circular references
15:49:18.767 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor'
15:49:18.767 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor'
15:49:18.768 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor'
15:49:18.768 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor' to allow for resolving potential circular references
15:49:18.768 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor'
15:49:18.776 [main] DEBUG o.s.c.a.AnnotationConfigApplicationContext - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@437668f]
15:49:18.785 [main] DEBUG o.s.c.a.AnnotationConfigApplicationContext - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@2e2a1a92]
15:49:18.788 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3708ab98: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor]; root of factory hierarchy
15:49:18.789 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
15:49:18.789 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
15:49:18.789 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
15:49:18.789 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
15:49:18.790 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor'
15:49:18.790 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor'
15:49:18.793 [main] DEBUG o.s.c.a.AnnotationConfigApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@3f271f41]
15:49:18.794 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
15:49:18.800 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.liveBeansView.mbeanDomain' in [systemProperties]
15:49:18.801 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.liveBeansView.mbeanDomain' in [systemEnvironment]
15:49:18.802 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source. Returning [null]
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.kurento.kmf.media.MediaPipelineFactory] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:318)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:985)
at com.wit.software.kurento_media_api.App.main(App.java:18)


This error occurs whatever the path i put in 
  ApplicationContext context = new AnnotationConfigApplicationContext("/home/david/Desktop/kmf-media-config.xml");

if i put  ApplicationContext context = new AnnotationConfigApplicationContext("xptoxptoxpto");

the output is the same, so i guess that, i'm doing something terribly wrong, or nothing is retrieved from xml. 

Cheers,
David Salvador


Ivan Gracia

unread,
Mar 27, 2014, 12:46:47 PM3/27/14
to Kurento Public
Hi David,

Sorry for the delay, but we are on a tight deadline right now and couldn't answer you before. About your issue, have you tried putting you xml file in a place where it's accessible to the proccess that is building the spring context? That's the first things that comes out of the top of my head, since I recall that you were using JBoss to run this.

Anyway, if you follow the link that Luis sent you about configuring Spring context in your application, you'll get a better understanding of it, since it's not a particular problem related to Kurento.

Cheers,
Iván.


--
You received this message because you are subscribed to the Google Groups "kurento" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kurento+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

LuLop

unread,
Mar 27, 2014, 1:42:03 PM3/27/14
to kur...@googlegroups.com
David,
I think Spring has problems for finding the .xml file. Just to be sure, do the following
1. Remove this line form your "main" method:
 MediaPipelineFactory mpf = context.getBean(MediaPipelineFactory.class);

2. Add this line
MediaApiConfiguration c = (MediaApiConfiguration)context.getBean("mediaApiConfiguration");

3. Execute

Tell us what happens (basically tell us whether you still have an exception)

L.


David Salvador

unread,
Mar 27, 2014, 2:16:23 PM3/27/14
to kur...@googlegroups.com
Yep... that still gave the same error. 

However it was solved with using: 

ApplicationContext  mediaContext = new ClassPathXmlApplicationContext(new String[] {"kmf-media-config.xml"});
MediaPipelineFactory mpf = mediaContext.getBean(MediaPipelineFactory.class);

this worked. 

Now i have another problem... that is driving me completly insane!! 

Since i got this working localy, i started putting it on my project... 

Now the insane part: 

if i put: 

ApplicationContext  mediaContext = new ClassPathXmlApplicationContext(new String[] {"kmf-media-config.xml"});
MediaPipelineFactory mpf = mediaContext.getBean(MediaPipelineFactory.class);

on my project, when I'm deploying... i get the errors i said before, 

Basicly: 

Caused by: java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2444)
        at java.lang.Class.getDeclaredMethods(Class.java:1808)
        at org.mobicents.slee.container.component.validator.ClassUtils.getAbstractMethodsFromClass(ClassUtils.java:178)
        at org.mobicents.slee.container.component.validator.SbbComponentValidator.validate(SbbComponentValidator.java:139)
        at org.mobicents.slee.container.component.SbbComponentImpl.validate(SbbComponentImpl.java:309)
        at org.mobicents.slee.container.component.deployment.DeployableUnitBuilderImpl.build(DeployableUnitBuilderImpl.java:254)
        ... 37 more
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
...

However... 

If i comment the second line: 

ApplicationContext  mediaContext = new ClassPathXmlApplicationContext(new String[] {"kmf-media-config.xml"});
 // MediaPipelineFactory mpf = mediaContext.getBean(MediaPipelineFactory.class);

the project is deployed without any errors... 

Now my question is... how can in the first case "no class can be found", but in the second case everything is fine... 

This is driving me crazy... 

David

David Salvador

unread,
Apr 3, 2014, 8:01:28 AM4/3/14
to kur...@googlegroups.com
Hi guys,

I've finally accomplished the deployment of a standalone application using Kurento Media API in JBOSS using Spring Injection. 

It was not an "easy" job... so I'll put here my solution, so if in the future anyone has the same problem:

First i would like to tell you that there is probably an easy solution... but i didnt get it.

My solution was: (For JBOSS 5.1GA)

1- In maven, add dependencies for KMF-media-api. 
2- Solve all transitive dependencies if they exist. (If you use Mobicents JainSlee like i do, you'll probably have a lot)
3- For what i got... jboss bean injection using spring is not straightforward, or in other words, isn't just a question of adding dependencies. (Maven uses those dependencies for compiling, but even if you add them in the "output" jar... they will not be used by JBOSS)
4- So... the solution I got was using Jboss Snowdrop (http://www.jboss.org/snowdrop) and the procedure for using it was: 
4.1 - Download spring.deployers and put it in the "deployers" folder of JBOSS (in my case: jboss/server/default/deployers)
4.2 - With spring deployer you will be able to use spring framework... but still you'll have no access to kmf-api libraries, so you need to add them directy to jboss. Add all the dependency libraries of kmf-media-api to "lib" folder of jboss (in my case: jboss/server/default/lib)

Now... at this point I add another problem, for some reason, spring.deployers does not do AnnotationConfigApplicationContext (it does not give any error, so i guess it is supported, but did not work). With AnnotationConfigApplicationContext the only beans i got (from kmf-media-config.xml) were this: 

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
mediaApiConfiguration
thriftInterfaceConfiguration

So... in order to access other beans, you'll need to use ClassPathXmlApplicationContext instead of AnnotationConfigApplicationContext and directly add them to xml. So in the end this was my kmf-media-config.xml:

<?xml version="1.0" encoding="UTF-8"?>

   <context:annotation-config />
   <context:component-scan base-package="com.kurento" />

     <bean id="mediaApiConfiguration" class="com.kurento.kmf.media.MediaApiConfiguration">
        <property name="handlerAddress" value="10.39.40.194" />
        <property name="handlerPort" value="9595" /> 
    </bean> 
    <bean id="thriftInterfaceConfiguration" class="com.kurento.kmf.thrift.ThriftInterfaceConfiguration">
        <property name="serverAddress" value="172.18.3.187" />
        <property name="serverPort" value="9090" />
    </bean>
    
     <bean id="thriftInterfaceExecutorService" class="com.kurento.kmf.thrift.internal.ThriftInterfaceExecutorService"/>
    
    <bean id="mediaServerSyncClientFactory" class="com.kurento.kmf.thrift.pool.MediaServerSyncClientFactory"/>
    
    <bean id="mediaServerSyncClientPool" class="com.kurento.kmf.thrift.pool.MediaServerSyncClientPool"/>
    
<bean id="mediaServerAsyncClientFactory" class="com.kurento.kmf.thrift.pool.MediaServerAsyncClientFactory"/>
    
    <bean id="mediaServerAsyncClientPool" class="com.kurento.kmf.thrift.pool.MediaServerAsyncClientPool"/>
    
    <bean id="mediaServerClientPoolService" class="com.kurento.kmf.thrift.pool.MediaServerClientPoolService"/>

     <bean id="mediaPipelineFactory" class="com.kurento.kmf.media.MediaPipelineFactory"/> 
     
</beans>




LuLop

unread,
Apr 3, 2014, 8:54:26 AM4/3/14
to kur...@googlegroups.com
David,
Thank you for your contribution. The AnnotationConfigApplicationContext problem is quite strange, we will try to investigate.

Best.

L.

David Salvador

unread,
Apr 3, 2014, 9:13:27 AM4/3/14
to kur...@googlegroups.com
No problem.

Like I said, I did not find any easier solution, but maybe there is. Also, i had this problem with Mobicents JainSLEE+JBOSS 5.1.GA, probably with SIPServlets there isnt this problem, and also there is a newer version of Snowdrop for JBOSS 7 that maybe solves this problem. 

Cheers,
David  
Reply all
Reply to author
Forward
0 new messages