Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Virgo » Error @Autowired Service in Virgo
Error @Autowired Service in Virgo [message #786595] Mon, 30 January 2012 16:04 Go to next message
Gabriel Afonso is currently offline Gabriel Afonso
Messages: 23
Registered: December 2011
Junior Member
Hi, I have 2 bundles:
Bundle 1. Export a service. This is the code
services-spring.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:osgi="http://www.springframework.org/schema/osgi"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">
    <!--<osgi:service interface="com.divux.onex.comunication.Login" ref="login"/> -->
    
    <osgi:service id="comunicationTemplateService" interface="com.company.admin.templateone.inter.ComunicationTemplate" ref="comunicationTemplate"/>
</beans>


bundle-context-spring.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:beans="http://www.springframework.org/schema/beans"
        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
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  
   <context:component-scan base-package="com.company.admin.templateone.impl" />
</beans>

I scan the package com.company.admin.templateone.impl because there are the Class:

@Component("comunicationTemplate")
public class ComunicationTemplateImpl implements ComunicationTemplate {

    @Override
    public String getName() {
        return "TemplateOne";
    }

    }


Fine.

The Code of the WAB Service Consumer is this:

ApplicationContext.xml
....
<osgi:reference id="comunicationTemplate" interface="com.company.admin.templateone.inter.ComunicationTemplate"/>
     


company-servlet.xml
 <context:component-scan  base-package="com.company.admin.core.controller"></context:component-scan>   
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
   
    <mvc:annotation-driven/>   
    <mvc:resources mapping="/resources/**" location="/resources/"/>


And the class where I have the @Autowired is this:

@Controller
public class IndexController {
    @Autowired
     ComunicationTemplate comunicationTemplate;


    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String showIndex(HttpServletRequest request) {

        return "index";
    }
}



But when I go to the index page of the Consumer Web Bundle (WAB) the application show an error message:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.company.admin.templateone.inter.ComunicationTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:920)
	org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:789)
	org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
	org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
	org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
	org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
	org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
	org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
	org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
	org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
	org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
	org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
	org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
	org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
	org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
	org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
	javax.servlet.GenericServlet.init(GenericServlet.java:244)
	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
	org.eclipse.virgo.web.tomcat.support.ApplicationNameTrackingValve.invoke(ApplicationNameTrackingValve.java:33)
	org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
	org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
	org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
	org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
	java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	java.lang.Thread.run(Thread.java:662)

[/code]

I'm using Virgo 3.0.2 Release. The service bundle provider export the package of the Interface and the WAB consumer import the same package.

Sorry for the long post, but I dont know what am I doind wrong. Why am I having this error?

Also this WAB is using Spring security and Snaps libraries.

[Updated on: Mon, 30 January 2012 16:10]

Report message to a moderator

Re: Error @Autowired Service in Virgo [message #786605 is a reply to message #786595] Mon, 30 January 2012 16:23 Go to previous messageGo to next message
Dmitry Sklyut is currently offline Dmitry Sklyut
Messages: 272
Registered: January 2010
Senior Member
Do you use Osgi Aware Application Context in your wab?

 <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationContext</param-value>
    </context-param>
Re: Error @Autowired Service in Virgo [message #786608 is a reply to message #786605] Mon, 30 January 2012 16:27 Go to previous messageGo to next message
Gabriel Afonso is currently offline Gabriel Afonso
Messages: 23
Registered: December 2011
Junior Member
Hi Dmitry thanks for the quick answer.
I solve the problem.

The problem was this: In the web.xml I had this

web.xml
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext-business.xml
            /WEB-INF/security-app-context.xml
          
        </param-value>
    </context-param>


but I have the Reference configuration in the applicationContext.xml file and in the web.xml I don't mention this file. I solved this problem adding this file in the web.xml

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext-business.xml
            /WEB-INF/security-app-context.xml
            /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>


Thanks.

Gabriel
Re: Error @Autowired Service in Virgo [message #786620 is a reply to message #786608] Mon, 30 January 2012 16:48 Go to previous messageGo to next message
Gabriel Afonso is currently offline Gabriel Afonso
Messages: 23
Registered: December 2011
Junior Member
Sorry Dmitry I have other problem. The bundle consumer is a HOST in Snaps Virgo, the other is a normal bundle. And the host can consume very well the service of the normal bundle. But when I traying to consume a service from a snap bundle they can't do it if the osgi:reference is not declared in the ApplicationContext.xml file of the Host.

Why does it happend?
Re: Error @Autowired Service in Virgo [message #786739 is a reply to message #786620] Mon, 30 January 2012 19:20 Go to previous messageGo to next message
Dmitry Sklyut is currently offline Dmitry Sklyut
Messages: 272
Registered: January 2010
Senior Member
Sorry - I am not sure I fully understand an issue.

You have a service provider bundle (A), snap host (H) and a snap child bundle (C). When A publishes service - H consumes it without an issue, but when you are publishing service from C - H does not see it.

Is this correct?

If so - yes this will so, because C is started after H by its definition. Host must be fully active for child to come up. You can't have Host depend on or require service published by a Snap child. Timing issue. You could configure a reference collection (list/set) in the host vs. a reference and deal with dynamics that way.

I am not sure what C is publishing that you need in H, but you better be sure that service interface that C publishes is not part of the C bundle/wab. Move it out to a shared bundle.

Let me know if I understood the problem correctly.

Dmitry
Re: Error @Autowired Service in Virgo [message #786750 is a reply to message #786739] Mon, 30 January 2012 19:35 Go to previous messageGo to next message
Gabriel Afonso is currently offline Gabriel Afonso
Messages: 23
Registered: December 2011
Junior Member
Hi Dmitry,

That is not the problem. I have the service provider (A), snap Host(H) and a snap child bundle (C). C wants to consume a service from A. But when I declare
<osgi:list id="comunicationTemplate" interface="com.company.admin.templateone.inter.ComunicationTemplate"/>
in my ApplicationContext.xml to receive the service and autowired in an Object in a Class of C there is a problem, because the application doesn't find a bean for Autowired that object.

But when I put
<osgi:list id="comunicationTemplate" interface="com.company.admin.templateone.inter.ComunicationTemplate"/>
in ApplicationContext.xml of the Host this work fine. Why can't I consume a service from a Child??

[Updated on: Mon, 30 January 2012 19:45]

Report message to a moderator

Re: Error @Autowired Service in Virgo [message #786763 is a reply to message #786750] Mon, 30 January 2012 19:53 Go to previous messageGo to next message
Dmitry Sklyut is currently offline Dmitry Sklyut
Messages: 272
Registered: January 2010
Senior Member
Can you include your Child web.xml configuration?

I think it might be because you are trying to load applicationContext in the child as a ROOT context. Host and Child share serveltContext. You might need to scope your application context for the child to the servlet and remove context-param and ContextLoadListener from web.xml of the child snap.

<servlet>
        <servlet-name>snap</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring/servlet/*.xml
            </param-value>
        </init-param>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationContext</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

Re: Error @Autowired Service in Virgo [message #786777 is a reply to message #786763] Mon, 30 January 2012 20:09 Go to previous messageGo to next message
Gabriel Afonso is currently offline Gabriel Afonso
Messages: 23
Registered: December 2011
Junior Member
this is mi web.xml child

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

    <display-name>template</display-name>

<!-- CONFIGURE A PARENT APPLICATION CONTEXT -->

    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationContext</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

  <!-- DISPATCHER SERVLET CONFIG -->
    <servlet>
        <servlet-name>template</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>template</servlet-name>
        <url-pattern>/web/*</url-pattern>
    </servlet-mapping>

</web-app>


I don't what I need to change in web.xml child to load the applicationContext of child, not applicationContext of the Host

[Updated on: Mon, 30 January 2012 20:22]

Report message to a moderator

Re: Error @Autowired Service in Virgo [message #786784 is a reply to message #786777] Mon, 30 January 2012 20:16 Go to previous messageGo to next message
Dmitry Sklyut is currently offline Dmitry Sklyut
Messages: 272
Registered: January 2010
Senior Member
Try to scope your app context to a servlet:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

    <display-name>template</display-name>


  <!-- DISPATCHER SERVLET CONFIG -->
    <servlet>
        <servlet-name>template</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                <path to your spring config here>
            </param-value>
        </init-param>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationContext</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>template</servlet-name>
        <url-pattern>/web/*</url-pattern>
    </servlet-mapping>

</web-app>

Re: Error @Autowired Service in Virgo [message #786794 is a reply to message #786784] Mon, 30 January 2012 20:32 Go to previous messageGo to next message
Gabriel Afonso is currently offline Gabriel Afonso
Messages: 23
Registered: December 2011
Junior Member
Thanks for your help Dmitry. It Works Fine!!!!!!

Thanks!!
Re: Error @Autowired Service in Virgo [message #786818 is a reply to message #786794] Mon, 30 January 2012 21:08 Go to previous message
Dmitry Sklyut is currently offline Dmitry Sklyut
Messages: 272
Registered: January 2010
Senior Member
Glad you got it working!
Previous Topic:Virgo Tooling - Third party bundles
Next Topic:Existing Tomcat + OSGI + Spring
Goto Forum:
  


Current Time: Thu Oct 23 11:22:42 GMT 2014

Powered by FUDForum. Page generated in 0.03439 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software