开发者

Spring MVC, injection of Hibernate Service into Spring bean is failing, not sure why

I have a spring bean that I have configured in applicationContext like below:

<bean id="beanIRPlus" class="org.jadefalcon.demo.server.Spring.beans.BeanIRPlus" />

Then I have a Hibernate Service like below that I a开发者_运维问答m trying to inject into the Spring bean. Normally, for example, if I use a prototype bean thats injected into my controller and that has an injected Hibernate service it works fine, however for this particular bean it is a singleton so its created when the application starts up. I made sure to even put the bean declaration at the very end of the applicationContext.xml file figuring maybe it has to be put after anything Hibernate related but the issue is still persisting. Its giving a null pointer exception, that the CasesService object doesn't exist. Any advice on what I'm doing wrong is greatly appreciated:

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.jadefalcon.demo.domain.Cases;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("casesservice")
@Transactional
public class CasesService {

    protected static Logger logger = Logger.getLogger("service");

    @Resource(name = "sessionFactory")
    private SessionFactory sessionFactory;

    public void add(Cases cases) {
        logger.debug("Saving new search");

        // Retrieve session from Hibernate
        Session session = sessionFactory.getCurrentSession();

        // Save
        session.save(cases);
    }
}


I didn't see how you are trying to inject it. You have at least two options:

  • xml. Define a <property name=".." ref="casesservice"> in your controller bean definition
  • annotations. Use @Autowired private CaseService service (or @Inject)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜