Search code examples
javaspringservletsodata4j

Auto-wiring with spring in an init-param class of a servlet


I am having some difficulty auto-wiring beans into a class which is loaded as an init-param in a servlet for OData4j. I have tried many solutions including load-time-weaving but cannot seem to get it to work correctly because as I understand it, the class which is being passed as an init-param to the servlet is being loaded prior to Spring context being loaded at all. Below is the current state of my configuration, is there a way to have dependency injection in a class loaded in such a way? The end goal is to have the ExampleProducerFactory.java (which is an init-param of the OData servlet) having the UserDao bean autowired.

I have tried to just include the key pieces of each of these files, if there is additional configuration information needed, please comment.

applicationContext.xml

 <context:component-scan base-package="com.project.core"/>
 <context:spring-configured/>
 <context:load-time-weaver weaver-class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver" />

web.xml

 <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- read the XmlWebApplicationContext for spring -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 <servlet>
  <servlet-name>OData</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
     <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
     <param-value>org.odata4j.producer.resources.ODataResourceConfig</param-value>
    </init-param>
   <init-param>
       <param-name>odata4j.producerfactory</param-name>
       <param-value>com.wildgigs.core.odata.ExampleProducerFactory</param-value>
   </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>OData</servlet-name>
    <url-pattern>/example.svc/*</url-pattern>
</servlet-mapping>

ExampleProducerFactory.java

 @Configurable(autowire= Autowire.BY_NAME)
 public class ExampleProducerFactory implements ODataProducerFactory {

@Autowired
private UserDao userDao;

@Override
public ODataProducer create(Properties arg0) {
    InMemoryProducer producer = new InMemoryProducer("example");
    List<User> users = userDao.findAll();
    User[] usersArray = new User[users.size()];
    users.toArray(usersArray);
    final User[] usersArrayFinal = usersArray;
     producer.register(User.class, Long.class, "Users", new Func<Iterable<User>>() {
         public Iterable<User> apply() {
             ThreadGroup tg = Thread.currentThread().getThreadGroup();
             while (tg.getParent() != null)
                 tg = tg.getParent();
             return Enumerable.create(usersArrayFinal).take(usersArrayFinal.length);
         }
     }, "Id");


     return producer;
}
}

UserDaoImpl.java

 @Repository
 public class UserDaoImpl extends GenericDaoImpl<User, Long> implements UserDao,      Serializable {

 @Transactional(readOnly = true)
 public User getByUserName(String userName) {
      Query query = getSession().createQuery("FROM User where upper(userName) = :name");
      query.setString("name", userName.toUpperCase());
      return (User) query.uniqueResult();
 }

 }

Solution

  • You should use the jersey SpringServlet instead of the ServletContrainer. With that instead of specifying resourceConfig in the init param you can define it as a spring bean.