可恶的sessionfactory,题目长长,回复多多,该如何处理

   阅读
可恶的sessionfactory,题目长长,回复多多
小弟,日前遇到一个问题,webapp在web.xml里面加载spring配置文件代码如下:
<!-- 在web中配置spring 
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
-->
然后在程序中使用注解生成sessionfactory
@Resource
protect SessionFactory sessionFactory;
却无法拿到对象,sessionFactory为null。

于是乎就换了一种方式,代码如下:
ApplicationContext cxt = new ClassPathXmlApplicationContext(
"applicationContext.xml");

protected SessionFactory sessionFactory = (SessionFactory) cxt
.getBean("sessionFactory");
运行发现这样就能获得sessionFactory了
求教有经验的大哥大姐,这是为啥呢?

------解决方案--------------------
protect SessionFactory sessionFactory;
有SET方法吗?
------解决方案--------------------
@Component(value="CSH100DAO")
public class CSH100DAOImpl implements CSH100DAO{
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource(name="sessionFactory")
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;


要想依赖注入,那就必需将类交给SPRING管理,加上@Component(value="name")

------解决方案--------------------
探讨
@Resource
protect SessionFactory sessionFactory;
却无法拿到对象,sessionFactory为null。

------解决方案--------------------
注解要添加set。
阅读
上一篇:初学者struts2入门 下一篇:返回列表