0

I am trying to autowire a class into a WebSocketServlet in the following way:

@Configurable(autowire=Autowire.BY_TYPE)
public class MyServlet extends WebSocketServlet {
    @Autowired
    public MyClass field;

    // etc...
}

Here's what my configuration looks like:

<context:annotation-config />
<context:component-scan base-package="org.*" />

<bean id="config" class="org.*.MyClass">
   <!-- a bunch of properties -->
</bean>

Note that autowire used to work just fine as long as I was in a Spring @Controller. I had to step out of that because i don't know how to map a WebSocketsServlet to a method of the @Controller as you do with regular servlets.

Any idea what I might be missing?

JohnIdol
  • 48,899
  • 61
  • 158
  • 242
  • 1
    @Configurable requires AspectJ load time or compile time weaving to work, it will not work with Spring AOP alone. Can you confirm that you have Aspectj With load/compile time weaving enabled – Biju Kunjummen Oct 05 '12 at 12:28

3 Answers3

2

In order to use @Configurable, you need to have these line in tour context:

<context:load-time-weaver aspectj-weaving="true"/>
<context:spring-configured/>    
<context:annotation-config />
<context:component-scan base-package="org.*" />

In addition, I think you must reference spring-aspect in the Import-Library section of your Manifest.

I didn't succeed to make it work, there is a post on this in the Virgo forum at Eclipse. If you succeed, let me know how ;)

Tcharl
  • 341
  • 3
  • 16
  • check my answer below - I had to drop @Configurable and use the SpringBeanAutowiringSupport stuff :) – JohnIdol Oct 11 '12 at 14:09
1

Getting rid of @Configurable and doing the following in the servlet init method does the trick:

@Override
public void init() throws ServletException {
   super.init();
   SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
JohnIdol
  • 48,899
  • 61
  • 158
  • 242
-4

As per the spring documentation

Externalized values may be looked up by injecting the Spring Environment into a @Configuration class using the @Autowired or the @Inject annotation:

 @Configuration
 public class AppConfig {
     @Inject Environment env;

     @Bean
     public MyBean myBean() {
         MyBean myBean = new MyBean();
         myBean.setName(env.getProperty("bean.name"));
         return myBean;
     }
 }
Anshu
  • 7,783
  • 5
  • 31
  • 41