1

目前我正在设置一个 Spring MVC + Hibernate + Maven Web 应用程序。

我与 JUnit 错误堆栈,禁止使用 tomcat 运行我的应用程序

这是 JUnit 故障跟踪:

  java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:105)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:74)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.amadeus.webAppDemo.service.TeamService com.amadeus.webAppDemo.controller.TeamController.teamService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.amadeus.webAppDemo.dao.TeamDao com.amadeus.webAppDemo.service.TeamServiceImpl.teamDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.amadeus.webAppDemo.dao.TeamDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.hibernate4.LocalSessionFactoryBean com.amadeus.webAppDemo.init.WebAppConfig.sessionFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.amadeus.webAppDemo.service.TeamService com.amadeus.webAppDemo.controller.TeamController.teamService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.amadeus.webAppDemo.dao.TeamDao com.amadeus.webAppDemo.service.TeamServiceImpl.teamDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.amadeus.webAppDemo.dao.TeamDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.hibernate4.LocalSessionFactoryBean com.amadeus.webAppDemo.init.WebAppConfig.sessionFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.amadeus.webAppDemo.dao.TeamDao com.amadeus.webAppDemo.service.TeamServiceImpl.teamDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.amadeus.webAppDemo.dao.TeamDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.hibernate4.LocalSessionFactoryBean com.amadeus.webAppDemo.init.WebAppConfig.sessionFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.amadeus.webAppDemo.dao.TeamDao com.amadeus.webAppDemo.service.TeamServiceImpl.teamDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.amadeus.webAppDemo.dao.TeamDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.hibernate4.LocalSessionFactoryBean com.amadeus.webAppDemo.init.WebAppConfig.sessionFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.amadeus.webAppDemo.dao.TeamDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.hibernate4.LocalSessionFactoryBean com.amadeus.webAppDemo.init.WebAppConfig.sessionFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.amadeus.webAppDemo.dao.TeamDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.hibernate4.LocalSessionFactoryBean com.amadeus.webAppDemo.init.WebAppConfig.sessionFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.hibernate4.LocalSessionFactoryBean com.amadeus.webAppDemo.init.WebAppConfig.sessionFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.hibernate4.LocalSessionFactoryBean com.amadeus.webAppDemo.init.WebAppConfig.sessionFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found

Caused by: java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found

对于测试,我有两个课程。第一个是:BaseTestConfig.java:

@Configuration
@ComponentScan("com.mycompany.webAppDemo")
@EnableWebMvc
public class BaseTestConfig {

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

}

第二个是 LinkControllerClasses.java :

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes=BaseTestConfig.class)
public class LinkControllerTest {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void init() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Test
    public void testHomePage() throws Exception {
        mockMvc.perform(get("/"))
            .andExpect(status().isOk())
            .andExpect(view().name("home"));
    }

    @Test
    public void testIndexPage() throws Exception {
        mockMvc.perform(get("/index.html"))
            .andExpect(status().isOk())
            .andExpect(view().name("home"));
    }

}

对于我的应用程序配置,我有两个类: Initializer.java

public class Initializer implements WebApplicationInitializer{

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // TODO Auto-generated method stub
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(WebAppConfig.class);
        servletContext.addListener(new ContextLoaderListener(ctx));

        ctx.setServletContext(servletContext);

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

和 WebAppConfig.java :

@Configuration
@ComponentScan("com.mycompany.webAppDemo")
@EnableWebMvc
@EnableTransactionManagement
@PropertySource("classpath:application.properties")
public class WebAppConfig {

    private static final String PROPERTY_NAME_DATABASE_DRIVER = "com.mysql.jdbc.Driver";
    private static final String PROPERTY_NAME_DATABASE_PASSWORD = "";
    private static final String PROPERTY_NAME_DATABASE_URL = "jdbc:mysql://127.0.0.1:3306/demodb";
    private static final String PROPERTY_NAME_DATABASE_USERNAME = "root";

    private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "org.hibernate.dialect.MySQL5InnoDBDialect";
    private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "true";
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "com.amadeus.webAppDemo.model";

    @Resource
    private Environment env;

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

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

        return dataSource;
    }

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource());
        sessionFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
        sessionFactoryBean.setHibernateProperties(hibProperties());
        return sessionFactoryBean;
    }

    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));
        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/pages/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

}

最后是两个控制器类:

链接控制.java

@Controller
public class LinkController {

     @RequestMapping(value="/")  
        public ModelAndView mainPage() {  
            return new ModelAndView("home");  
        }  

     @RequestMapping(value="/index")  
        public ModelAndView indexPage() {  
            return new ModelAndView("home");  
        } 

}

TeamController.java

@Controller
public class TeamController {

    @Autowired
    private TeamService teamService; 

    @RequestMapping(value="/team/add")
    public ModelAndView addTeamPage(){
        ModelAndView modelAndView = new ModelAndView("add-team-form");
        modelAndView.addObject("team", new Team());
        return modelAndView;
    }

    @RequestMapping(value="/team/add/process")
    public ModelAndView addingTeam(@ModelAttribute Team team){
        ModelAndView modelAndView = new ModelAndView("home");
        teamService.addTeam(team);

        String message ="Team was successfully added";
        modelAndView.addObject("message", message); 

        return modelAndView;  
    }

    @RequestMapping(value="/team/list")  
    public ModelAndView listOfTeams() {  
        ModelAndView modelAndView = new ModelAndView("list-of-teams");  

        List<Team> teams = teamService.getTeams();  
        modelAndView.addObject("teams", teams);  

        return modelAndView;  
    }

      @RequestMapping(value="/team/edit/{id}", method=RequestMethod.GET)  
        public ModelAndView editTeamPage(@PathVariable Integer id) {  
            ModelAndView modelAndView = new ModelAndView("edit-team-form");  
            Team team = teamService.getTeam(id);  
            modelAndView.addObject("team",team);  
            return modelAndView;  
        }  

      @RequestMapping(value="/team/edit/{id}", method=RequestMethod.POST)  
        public ModelAndView edditingTeam(@ModelAttribute Team team, @PathVariable Integer id) {  

            ModelAndView modelAndView = new ModelAndView("home");  

            teamService.updateTeam(team);  

            String message = "Team was successfully edited.";  
            modelAndView.addObject("message", message);  

            return modelAndView;  
        }  

      @RequestMapping(value="/team/delete/{id}", method=RequestMethod.GET)  
        public ModelAndView deleteTeam(@PathVariable Integer id) {  
            ModelAndView modelAndView = new ModelAndView("home");  
            teamService.deleteTeam(id);  
            String message = "Team was successfully deleted.";  
            modelAndView.addObject("message", message);  
            return modelAndView;  
        }  
}

我尝试了对类似问题的建议修改 Failed to load ApplicationContext with @ContextConfiguration(classes={ ... })

但它不工作:(


谢谢你的回答,这里是application.properties

#DB properties:
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/demodb
db.username=root
db.password=*****

#Hibernate Configuration:
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.show_sql=true
entitymanager.packages.to.scan=com.mycompany.webAppDemo.model

我在我的 WebAppConfig.java.For eg.instead 中做了一些更改:

private static final String PROPERTY_NAME_DATABASE_DRIVER = "com.mysql.jdbc.Driver";

我这样做:

private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";

但仍然有同样的错误:(

4

1 回答 1

3

如果Spring 的 Environment 类getRequiredProperty()方法无法解析该属性,则会引发 IllegalStateException。您正在尝试这样做:

dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));

它试图用钥匙获得财产

private static final String PROPERTY_NAME_DATABASE_DRIVER = "com.mysql.jdbc.Driver";

除非您的属性文件有一个条目,否则这没有任何意义:

com.mysql.jdbc.Driver=com.mysql.jdbc.Driver

我认为你想要的是:

private static final String PROPERTY_NAME_DATABASE_DRIVER = "driverClassName";
// in properties file
...
driverClassName=com.mysql.jdbc.Driver
...

或者

dataSource.setDriverClassName(PROPERTY_NAME_DATABASE_DRIVER);

直接设置它而不是通过属性文件。

application.properties如果您需要更多详细信息,我会要求您发布文件的内容。

于 2013-05-06T17:16:27.150 回答