2012-11-30 61 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 –