简体   繁体   English

在基于Java的Spring配置中创建applicationContext bean

[英]Creating applicationContext bean in java based spring configuration

I'm trying to build from scratch spring-based application using Java configurations, but I'm getting warning which I completely don't understand... Can someone tell me what is wrong there? 我正在尝试使用Java配置从头开始构建基于Spring的应用程序,但是我收到警告,我完全不明白...有人可以告诉我那里出了什么问题吗?

Error: WARNING: Exception encountered during context initialization - cancelling refresh attempt org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applicationContext': Instantiation of bean failed; 错误:警告:上下文初始化期间遇到异常-取消刷新尝试org.springframework.beans.factory.BeanCreationException:创建名称为'applicationContext'的bean时出错:bean的实例化失败; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.context.ApplicationContext]: Specified class is an interface 嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.context.ApplicationContext]:指定的类是一个接口

Aplication initializer: 应用初始化器:

public class SpringWebAppInitializer  implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

    AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
    appContext.register(ApplicationContext.class);

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", new DispatcherServlet(appContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");

    servletContext.addListener(new ContextLoaderListener(appContext));
}
}

Application context configuration: 应用程序上下文配置:

@Configuration
@ComponentScan("pl.wybornie.entity.*")
@EnableTransactionManagement
public class ApplicationContextConfig {

@Bean(name = "viewResolver")
public InternalResourceViewResolver getViewResolver() {

    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/pages/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
}

@Bean(name = "dataSource")
public DataSource getDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/wyborniedb");
    dataSource.setUsername("root");
    dataSource.setPassword("root123");

    return dataSource;
}

@Autowired
@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(DataSource dataSource) {

    LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);

    //sessionBuilder.addAnnotatedClasses(User.class);
    sessionBuilder.scanPackages("pl.wybornie.entity", "pl.wybornie.entity.cookBook");

    return sessionBuilder.buildSessionFactory();
}

@Autowired
@Bean(name = "transactionManager")
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {

    HibernateTransactionManager transactionManager = new   HibernateTransactionManager(sessionFactory);
    return transactionManager;
}
  }

These are only 2 configuration classes for whole project. 这些只是整个项目的2个配置类。

Whole code can be found at: https://github.com/annweg/wybornie.pl/tree/new_build/project_workspace/wybornie 完整的代码可以在以下位置找到: https : //github.com/annweg/wybornie.pl/tree/new_build/project_workspace/wybornie

I'm quite newbie with spring and maybe there is some configuration missing there... I'm using Spring 4.1.6, Tomcat 7 and Java 7. 我是Spring的新手,也许那里缺少一些配置...我正在使用Spring 4.1.6,Tomcat 7和Java 7。

edit: I have tried to register both ApplicationContext from Spring and my own configuration class - ApplicationContextConfig - the error is the same. 编辑:我已经尝试从Spring和我自己的配置类-ApplicationContextConfig-注册ApplicationContext-错误是相同的。 I've deleted Tomcat server and created new one, reopened Eclipse and still the same. 我已经删除了Tomcat服务器并创建了一个新服务器,重新打开了Eclipse,并且仍然相同。 Either importing project as new (deleting all settings and .project file) didn't help, so maybe the error is somewhere in configuration...? 将项目导入为新项目(删除所有设置和.project文件)都无济于事,所以也许错误出在配置中?

The problem was with my Eclipse settings. 问题出在我的Eclipse设置上。 I've delete all project content, change workspace and just copy/paste all classes and libs and the error is gone. 我已经删除了所有项目内容,更改了工作区,仅复制/粘贴了所有类和库,错误消失了。

Well, this type of error is often related to missing jars and/or classpath error. 好吧,这种类型的错误通常与缺少的jar和/或类路径错误有关。 Check if you have some libraries not added to classpath, or the project is requiring some libs that are previously present but then removed. 检查是否有一些库未添加到类路径中,或者项目是否需要一些先前存在但已删除的库。 Sometimes I add jars to classpath and remove them and it works. 有时我将jar添加到classpath并将其删除,并且可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Spring ApplicationContext配置 - Java Spring ApplicationContext Configuration 在Spring @Configuration中引用applicationContext.xml bean - Referring applicationContext.xml bean in Spring @Configuration 使用基于Spring Java的配置注入Bean依赖项 - Injecting bean dependencies with Spring Java based configuration Java Spring-启动ApplicationContext时出错。 创建名称为'entityManagerFactory'的bean时出错 - Java Spring - Error starting ApplicationContext. Error creating bean with name 'entityManagerFactory' Spring ApplicationContext Bean 作用域 - Spring ApplicationContext Bean Scope 创建参数化的Spring bean配置 - Creating parameterized Spring bean configuration Spring 启动 ApplicationContext 启动错误/创建名称为“entityManagerFactory”的 bean 时出错 - Spring Boot Error starting ApplicationContext / Error creating bean with name 'entityManagerFactory' 如何根据 java spring 中的请求参数实例化 @configuration bean? - How to Instantiate @configuration bean based on request parameters in java spring? 基于 Java 的 Spring 配置中具有多个构造函数的 Bean - Bean with multiple constructors in Java-based Spring configuration 在基于java的配置中覆盖xml定义的spring bean - Override xml-defined spring bean in java-based configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM