Skip to main content



      Home
Home » Eclipse Projects » Virgo » @Autowired doesn't work when trying to wire a service
@Autowired doesn't work when trying to wire a service [message #607698] Thu, 19 August 2010 04:11 Go to next message
Eclipse UserFriend
Hi, I can't make @Autowire work. Bean injection only works if I create the bean and inject it using <bean ....><property name="configurationManager" ref="configurationManager">....

I believe I am doing things as done in greenpages-2.3.0.M1 example. I am using virgo 2.3.0M03

Bundle config exports a service:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/osgi"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
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">


<service ref="configurationManager" interface="com.rits.config.IConfigurationManager"/>

</beans:beans>


Then I import the service in bundle gui (web bundle, 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"
xmlns:osgi="http://www.springframework.org/schema/osgi"
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/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context -3.0.xsd
">

<osgi:reference id="configurationManager" interface="com.rits.config.IConfigurationManager"/>

</beans>

and use the reference in gui-servlet.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-3.0 .xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context -3.0.xsd
">
...

<context:component-scan base-package="com.rits.gui.web" />

</beans>


GuiController class:


package com.rits.gui.web;

import org.apache.commons.lang.text.StrSubstitutor;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rits.config.Config;
import com.rits.config.IConfigurationManager;

/**
* @author kostantinos.kougios
*
* 24 Mar 2010
*/
@Controller
public class GuiController
{
private final Logger logger = Logger.getLogger(getClass());

@Autowired
private IConfigurationManager configurationManager;

@RequestMapping("/gui.htm")
public void list(final Model model)
{
final String s = StrSubstitutor.replaceSystemProperties("You are running with java.version = ${java.version} and os.name = ${os.name}.");
model.addAttribute("s", s);
if (configurationManager == null) throw new IllegalStateException("not initialized properly");
final Config config = configurationManager.get("../config");
logger.info("rendering gui . Config " + config);
}
}

though autowired, the variable configurationManager is null when the list() is called.

Any ideas? As said, if I convert the guicontroller to a bean and inject using <property>, it works.

Thanks,

Kostas
Re: @Autowired doesn't work when trying to wire a service [message #607705 is a reply to message #607698] Thu, 19 August 2010 15:36 Go to previous messageGo to next message
Eclipse UserFriend
Make sure that in web.xml for dispatcher servlet configuration you specify correct (osgi aware) version of ApplicationContext

i.e.

<servlet>
<servlet-name>servlet</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.ServerOsgiBundleXmlWebApplicationCo ntext </param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>


What kind of application context hierarchy do you have? that is who is loading applicationContext.xml? Servlet Context listener or you let spring-dm extender pick it up?
Re: @Autowired doesn't work when trying to wire a service [message #607707 is a reply to message #607705] Thu, 19 August 2010 16:41 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

I've tried modifying my web.xml like the one you provided without any luck.

My web xml is as follows and configuration wise it is identical to the web.xml of the greenpages example:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>com.rits.gui.web</display-name>
<context-param>
<param-name>contextClass</param-name>
<param-value> org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationCo ntext </param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
<servlet>
<servlet-name>gui</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>gui</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>gui.htm</welcome-file>
</welcome-file-list>
</web-app>
Re: @Autowired doesn't work when trying to wire a service [message #607708 is a reply to message #607698] Thu, 19 August 2010 16:49 Go to previous messageGo to next message
Eclipse UserFriend
I injected my osgi service bean without using @Autowire just to see what's going on. My service bean is instanceof IConfigurationManager but on the debugger I see it as $Proxy134. Maybe that's why @Autowire doesn't work.

But I wonder why the greenpages example works (I didn't run it myself).
Re: @Autowired doesn't work when trying to wire a service [message #871055 is a reply to message #607698] Thu, 10 May 2012 12:54 Go to previous message
Eclipse UserFriend
Hello,
Sorry to pop this up, but I have exactely the same problem, did you figured a turn around sir ?
Thank you.
Previous Topic:TCCL Headache
Next Topic:Problems running Spring DM application in Virgo
Goto Forum:
  


Current Time: Tue May 20 08:47:49 EDT 2025

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

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

Back to the top