我正在Spring MVC中開發一個小型的Web應用程序。每次我嘗試在任何控制器中獲取自定義類時,我都會遇到異常,以防該類使用另一個自定義類。這是比較容易顯示在例如:Spring:java.lang.NoClassDefFoundError:無法初始化類
控制器,在那裏我試圖讓自定義類的對象WireTest:
@Controller
@RequestMapping("/offices")
public class OfficesController {
@Autowired
private WireTest wt;
@RequestMapping("")
public String offices(Model model) {
model.addAttribute("test", wt.getString());
return "offices";
}
}
的問題總是會發生,我是否直接或使用創建對象@Autowired。在代碼中,我展示了@Autowired的情況,但這並不重要 - 我可以寫private WireTest wt = new WireTest()
,例外情況也是如此。
WireTest.java
類:
@Service
public class WireTest {
public String getString() {return (DBhelper.getString());}
}
DBhelper.java
類(還有其他的靜態成員,完整代碼如下)的部分:
public class DBhelper {
public static String getString() {return "Hi!";}
}
和異常:
HTTP Status 500 - Handler processing failed; nested exception is
java.lang.NoClassDefFoundError: Could not initialize class org.sher.wtpractice.dao.DBhelper
我也可以使用這些類在控制檯應用程序中沒有任何問題因此它在Spring之外工作。
我的Spring配置:
web.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Processes application requests -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
dispatcher-servlet.xml
:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="org.sher.wtpractice.spring, org.sher.wtpractice.dao" />
<mvc:annotation-driven />
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
我使用Maven建築和Tomcat 7的服務器,如果該事項。 春季版4.0.1.RELEASE
更新:DBhelper.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import java.util.Calendar;
import java.util.Date;
public class DBhelper {
private static final SessionFactory sessionFactory = createSessionFactory();
private static SessionFactory createSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
return configuration.buildSessionFactory(serviceRegistry);
}
public static Object queryWrapper(DBoperation op) {
Session session = sessionFactory.openSession();
Transaction transaction = null;
Object result = null;
try {
transaction = session.beginTransaction();
result = op.operation(session);
session.getTransaction().commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
// throw e;
} finally {
if (session != null && session.isOpen())
session.close();
}
return result;
}
public static Date normalizeCal(Calendar cal) {
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
public static String getString() {return "Hi!";}
}
的完整代碼任何幫助將不勝感激!
DBhelper類是否是任何jar文件的一部分? – zerocool 2014-09-06 14:44:02
不。從這個角度來看,DBhelper和WireTest類的唯一區別在於它們位於不同的包中 - WireTest更深一層。 – ars 2014-09-06 14:47:09
你能否解開戰爭文件並檢查這個類是否被maven打包成戰爭的一部分? – zerocool 2014-09-06 14:52:05