繁体   English   中英

创建名称为*的bean时出错

[英]Error creating bean with name *

我目前正在做该教程:

https://github.com/ubilife/tutorials/tree/master/spring/tutorial4

当我克隆存储库时,一切工作都与在其中编写的完全一样。

该教程除其他外还有此类:

Config.java

@Configuration
@ComponentScan
public class Config {

    @Bean
    public DataSource datasource() {
        return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScript("schema.sql").build();
    }

    @Bean
    public JdbcOperations jdbcTemplate(DataSource ds) {
        return new JdbcTemplate(ds);        
    }

}

现在,我试图利用我所学到的东西。 我将SpringBoot与Intellij Ultimate 20017一起使用,并且我在该类中写道

@Configuration
@ComponentScan
public class SpringRootConfig {

    @Bean(name = "scriptedDataSource")
    public DataSource datasource() {
        return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScript("schema.sql").build();
    }

    @Bean
    public JdbcOperations jdbcTemplate(@Qualifier("scriptedDataSource") DataSource dataSource) {
        System.out.println("jdbcTemplate()");
        return new JdbcTemplate(dataSource);
    }
}

首先,我遇到了Intellij无法处理的重复Bean问题。 如您所见,我通过使用限定符解决了这一问题。

但是现在我遇到了一个新的错误,我无法用Google解决。

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-05-28 14:38:50.428 ERROR 2020 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scriptedDataSource' defined in class path resource [com/example/demo/SpringRootConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'datasource' threw exception; nested exception is org.springframework.jdbc.datasource.init.CannotReadScriptException: Cannot read SQL script from class path resource [schema.sql]; nested exception is java.io.FileNotFoundException: class path resource [schema.sql] cannot be opened because it does not exist
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at com.example.demo.DemoApplication.main(DemoApplication.java:17) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'datasource' threw exception; nested exception is org.springframework.jdbc.datasource.init.CannotReadScriptException: Cannot read SQL script from class path resource [schema.sql]; nested exception is java.io.FileNotFoundException: class path resource [schema.sql] cannot be opened because it does not exist
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    ... 18 common frames omitted
Caused by: org.springframework.jdbc.datasource.init.CannotReadScriptException: Cannot read SQL script from class path resource [schema.sql]; nested exception is java.io.FileNotFoundException: class path resource [schema.sql] cannot be opened because it does not exist
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:450) ~[spring-jdbc-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:238) ~[spring-jdbc-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:48) ~[spring-jdbc-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory.initDatabase(EmbeddedDatabaseFactory.java:196) ~[spring-jdbc-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory.getDatabase(EmbeddedDatabaseFactory.java:153) ~[spring-jdbc-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder.build(EmbeddedDatabaseBuilder.java:270) ~[spring-jdbc-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at com.example.demo.SpringRootConfig.datasource(SpringRootConfig.java:21) ~[classes/:na]
    at com.example.demo.SpringRootConfig$$EnhancerBySpringCGLIB$$6e8fdc42.CGLIB$datasource$1(<generated>) ~[classes/:na]
    at com.example.demo.SpringRootConfig$$EnhancerBySpringCGLIB$$6e8fdc42$$FastClassBySpringCGLIB$$39d24068.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at com.example.demo.SpringRootConfig$$EnhancerBySpringCGLIB$$6e8fdc42.datasource(<generated>) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    ... 19 common frames omitted
Caused by: java.io.FileNotFoundException: class path resource [schema.sql] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172) ~[spring-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.core.io.support.EncodedResource.getReader(EncodedResource.java:141) ~[spring-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.jdbc.datasource.init.ScriptUtils.readScript(ScriptUtils.java:279) ~[spring-jdbc-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:447) ~[spring-jdbc-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    ... 35 common frames omitted


Process finished with exit code 1

因此,首先,我不明白为什么克隆导师代码能正常工作,并且一旦尝试失败就会失败(出于测试原因,我什至复制了他的config.java,但仍然出错!)

当然,如果您可以帮助我解决这个问题,那就太好了。

非常感谢! 如果您需要更多文件,请询问。 我的仓库在这里https://github.com/realsony/h2TestEnv/tree/master/src

从您发布的异常来看,它似乎找不到您的schema.sql文件:

Caused by: java.io.FileNotFoundException: class path resource [schema.sql] cannot be opened because it does not exist

确保您的schema.sql文件在正确的位置,或者更新代码以指向schema.sql文件的实际位置。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM