spring hibernate 整合获取session问题
publicclassILoginDAOImplextendsHibernateDaoSupport{publicStringlogin(Stringa)throwsEx...
public class ILoginDAOImpl extends HibernateDaoSupport {
public String login(String a) throws Exception {
super.getSession();
return "123";
}
}
测试类:
public class test {
public static void main(String[] args){
ILoginDAOImpl asd= new ILoginDAOImpl();
try {
String a =asd.login("1");
System.out.println(a);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
applicationContext.xml配置
<beans>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:orcl">
</property>
<property name="username" value="orc"></property>
<property name="password value="orc"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
</props>
</property>
</bean>
</beans>
报错:
java.lang.NullPointerException
at org.springframework.orm.hibernate3.support.HibernateDaoSupport.getSession(HibernateDaoSupport.java:140)
at com.yourcompany.test.ILoginDAOImpl.login(ILoginDAOImpl.java:8)
at com.yourcompany.test.test.main(test.java:10)
高手帮分析下 怎么回事啊 郁闷死我拉...............\
如能解决必有重谢!!!!!!!!!!!!!!!!! 展开
public String login(String a) throws Exception {
super.getSession();
return "123";
}
}
测试类:
public class test {
public static void main(String[] args){
ILoginDAOImpl asd= new ILoginDAOImpl();
try {
String a =asd.login("1");
System.out.println(a);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
applicationContext.xml配置
<beans>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:orcl">
</property>
<property name="username" value="orc"></property>
<property name="password value="orc"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
</props>
</property>
</bean>
</beans>
报错:
java.lang.NullPointerException
at org.springframework.orm.hibernate3.support.HibernateDaoSupport.getSession(HibernateDaoSupport.java:140)
at com.yourcompany.test.ILoginDAOImpl.login(ILoginDAOImpl.java:8)
at com.yourcompany.test.test.main(test.java:10)
高手帮分析下 怎么回事啊 郁闷死我拉...............\
如能解决必有重谢!!!!!!!!!!!!!!!!! 展开
3个回答
展开全部
你的问题,错在两个地方:
1,ILoginDAOImpl 类,应该要配置到spring bean 容器中,并且要注入sessionFactory
如:
<bean id="loginDAOImpl" class="your.classpath.ILoginDAOImpl"> <property name="sessionFactory"><refbean="sessionFactory"/></property>
</bean>
但是你没有配置;
2,你在测试类里面获得 ILoginDAOImpl 实例用的是new,
正确的做法是在spring bean容器中取。
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ILoginDAOImpl asd= (ILoginDAOImpl)context.getBean("loginDAOImpl");
然后再试试?
呵呵
1,ILoginDAOImpl 类,应该要配置到spring bean 容器中,并且要注入sessionFactory
如:
<bean id="loginDAOImpl" class="your.classpath.ILoginDAOImpl"> <property name="sessionFactory"><refbean="sessionFactory"/></property>
</bean>
但是你没有配置;
2,你在测试类里面获得 ILoginDAOImpl 实例用的是new,
正确的做法是在spring bean容器中取。
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ILoginDAOImpl asd= (ILoginDAOImpl)context.getBean("loginDAOImpl");
然后再试试?
呵呵
展开全部
<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="proxyTargetClass"><value>true</value></property>
<property name="transactionManager"><ref local="transactionManager"/></property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="proxyTargetClass"><value>true</value></property>
<property name="transactionManager"><ref local="transactionManager"/></property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼上的是百度知道7级啊!不能乱说啊!
你说的和人家问的没关系啊!
你说的和人家问的没关系啊!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询