2012-11-30 85 views
0

我已經看過使用Maven,Tomcat,Spring MVC和Hibernate做樣本。我選擇@配置方法。存檔我的項目,你可以download here使用@配置方法的Hibernate Spring MVC

但是,當我宣佈我的配置類的所有必需的豆類和點擊簡單的鏈接從我的主頁,顯示在控制檯中的許多錯誤,如:

ClassNotFoundException: org.springframework.orm.hibernate4.LocalSessionFactoryBean 

請給我意見,我該如何解決這個。

這裏是我的配置類:

package com.onet.init; 

import java.util.Properties; 

import javax.annotation.Resource; 
import javax.sql.DataSource; 

import org.apache.commons.dbcp.BasicDataSource; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.core.env.Environment; 
import org.springframework.orm.hibernate4.HibernateTransactionManager; 
import org.springframework.orm.hibernate4.LocalSessionFactoryBean; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
import org.springframework.web.servlet.view.JstlView; 
import org.springframework.web.servlet.view.UrlBasedViewResolver; 

@Configuration 
@ComponentScan("com.onet") 
@EnableWebMvc 
@PropertySource("classpath:application.properties") 
public class BaseConfig { 

    private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver"; 
    private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password"; 
    private static final String PROPERTY_NAME_DATABASE_URL = "db.url"; 
    private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username"; 

    private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect"; 
    private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql"; 
    private static final String PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY = "hibernate.ejb.naming_strategy"; 
    private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql"; 
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan"; 

    @Resource 
    private Environment env; 

    @Bean 
    public DataSource dataSource() { 
     BasicDataSource ds = new BasicDataSource(); 

     ds.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER)); 
     ds.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL)); 
     ds.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME)); 
     ds.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD)); 

     return ds; 
    } 

    @Bean 
    public LocalSessionFactoryBean sessionFactory() {  
     LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); 

     sessionFactory.setDataSource(dataSource()); 
     sessionFactory.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN)); 
     sessionFactory.setHibernateProperties(hibProperties()); 

     return sessionFactory; 
    } 

    private Properties hibProperties() { 
     Properties properties = new Properties(); 
     properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT)); 
     properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL)); 
     properties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL)); 
     properties.put(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY)); 
     return properties; 
    } 

    @Bean 
    public HibernateTransactionManager transactionManager() { 
     HibernateTransactionManager transactionManager = new HibernateTransactionManager(); 
     transactionManager.setSessionFactory(sessionFactory().getObject()); 
     return transactionManager; 
    } 

    @Bean 
    public UrlBasedViewResolver setupViewResolver() { 
     UrlBasedViewResolver resolver = new UrlBasedViewResolver(); 
     resolver.setPrefix("/WEB-INF/views/"); 
     resolver.setSuffix(".jsp"); 
     resolver.setViewClass(JstlView.class); 
     return resolver; 
    } 

} 

這裏是我的類,它實現WebApplicationInitializer:

package com.onet.init; 

import javax.servlet.ServletContext; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRegistration.Dynamic; 

import org.springframework.web.WebApplicationInitializer; 
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; 
import org.springframework.web.servlet.DispatcherServlet; 

public class Initializer implements WebApplicationInitializer { 

    @Override 
    public void onStartup(ServletContext servletContext) throws ServletException { 
     AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); 
     ctx.register(BaseConfig.class); 

     Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); 
     servlet.addMapping("*.html"); 
     servlet.setLoadOnStartup(1); 
    } 

} 

回答

1

ClassNotFoundException的異常意味着你不必一定在運行時所需的類/庫。

如果您使用任何IDE,請搜索LocalSessionFactoryBean類的位置。

當你找到這個類的圖書館(spring-orm我想)只要將它放到類路徑,你會沒事的。

+0

下面是這個問題的解決方案:http://stackoverflow.com/questions/4777026/classnotfoundexception-dispatcherservlet-when-launching-tomcat-maven-dependenci –