Spring MVC에서 작은 웹 애플리케이션을 개발 중입니다. 이 클래스가 다른 사용자 정의 클래스를 사용하는 경우 모든 컨트롤러에서 사용자 정의 클래스를 가져 오려고 할 때마다 예외가 발생합니다. 이 예에 표시하는 것이 더 쉽습니다 :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
콘솔 앱에서도 문제없이 이러한 클래스를 사용할 수 있습니다. 연화, 그래서 그것은 봄 바깥에서 작동합니다.
내 스프링 구성 :
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
: 그 중요한 경우 내가 서버로 건물 메이븐과 톰캣 7을 사용하고
<?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>
. 봄 버전은 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
아니요. DBhelper와 WireTest 클래스 간의 이러한 관점과 유일한 차이점은 서로 다른 패키지에 위치한다는 것입니다. WireTest는 한 단계 더 깊습니다. – ars
war 파일의 압축을 풀고이 클래스가 war의 일부로 maven에 패키지되었는지 확인할 수 있습니까? – zerocool