我使用Spring 报的错误
一::::这是我报的错误 我用的是 Eclipse 自动创建的, 结果 出错!!!!!!
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
%%%% Error Creating SessionFactory %%%%
org.hibernate.HibernateException: /SSH/WebRoot/WEB-INF/applicationContext.xml not found
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1087)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1111)
at com.hibernate.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:48)
at com.hibernate.HibernateSessionFactory.main(HibernateSessionFactory.java:86)
null
二::::
这是我的 com.hibernate.HibernateSessionFactory.java
public class HibernateSessionFactory
{
private static String CONFIG_FILE_LOCATION = "/SSH/WebRoot/WEB-INF/applicationContext.xml";
private static final ThreadLocal threadLocal = new ThreadLocal();
private static final Configuration cfg = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
private HibernateSessionFactory() {
}
public static void main(String jjj[])
{
System.out.println(“我将 Session 打印”+currentSession()); ////结果为空 null
}
三::::我的 /WEB-INF/applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="MySQL" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/marriage</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>3221058</value>
</property>
</bean>
<bean id="SessionFactoryID"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="MySQL" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/hibernate/HelpTopic.hbm.xml</value>
</list>
</property>
</bean>
<bean id="HelpTopicDAO" class="com.hibernate.HelpTopicDAO">
<property name="sessionFactory">
<ref bean="SessionFactoryID" />
</property>
</bean>
</beans>