Note that there are some explanatory texts on larger screens.

plurals
  1. POAuto-wiring with spring in an init-param class of a servlet
    primarykey
    data
    text
    <p>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.</p> <p>I have tried to just include the key pieces of each of these files, if there is additional configuration information needed, please comment.</p> <p>applicationContext.xml</p> <pre><code> &lt;context:component-scan base-package="com.project.core"/&gt; &lt;context:spring-configured/&gt; &lt;context:load-time-weaver weaver-class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver" /&gt; </code></pre> <p>web.xml</p> <pre><code> &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;!-- read the XmlWebApplicationContext for spring --&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;servlet&gt; &lt;servlet-name&gt;OData&lt;/servlet-name&gt; &lt;servlet-class&gt;com.sun.jersey.spi.container.servlet.ServletContainer&lt;/servlet-class&gt; &lt;init-param&gt; &lt;param-name&gt;com.sun.jersey.config.property.resourceConfigClass&lt;/param-name&gt; &lt;param-value&gt;org.odata4j.producer.resources.ODataResourceConfig&lt;/param-value&gt; &lt;/init-param&gt; &lt;init-param&gt; &lt;param-name&gt;odata4j.producerfactory&lt;/param-name&gt; &lt;param-value&gt;com.wildgigs.core.odata.ExampleProducerFactory&lt;/param-value&gt; &lt;/init-param&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;OData&lt;/servlet-name&gt; &lt;url-pattern&gt;/example.svc/*&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; </code></pre> <p>ExampleProducerFactory.java</p> <pre><code> @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&lt;User&gt; 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&lt;Iterable&lt;User&gt;&gt;() { public Iterable&lt;User&gt; apply() { ThreadGroup tg = Thread.currentThread().getThreadGroup(); while (tg.getParent() != null) tg = tg.getParent(); return Enumerable.create(usersArrayFinal).take(usersArrayFinal.length); } }, "Id"); return producer; } } </code></pre> <p>UserDaoImpl.java</p> <pre><code> @Repository public class UserDaoImpl extends GenericDaoImpl&lt;User, Long&gt; 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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload