【问题标题】:Error while starting spring boot application with couchbase Error creating bean with name 'tomcatEmbeddedServletContainerFactory'使用 couchbase 启动 Spring Boot 应用程序时出错 创建名为“tomcatEmbeddedServletContainerFactory”的 bean 时出错
【发布时间】:2016-03-16 18:17:33
【问题描述】:

我是 spring-boot 和 couchbase 的新手。我写了一个简单的应用程序,我试图连接到我的 couchbase 存储桶,但是在启动时它会抛出错误。

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$$EnhancerBySpringCGLIB$$6f4ff8c5]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$$EnhancerBySpringCGLIB$$6f4ff8c5.<init>()
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) ~[spring-boot-1.4.0.BUILD-SNAPSHOT.jar:1.4.0.BUILD-SNAPSHOT]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.4.0.BUILD-SNAPSHOT.jar:1.4.0.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:773) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar:1.4.0.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:368) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar:1.4.0.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar:1.4.0.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1190) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar:1.4.0.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1179) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar:1.4.0.BUILD-SNAPSHOT]

我的班级是这样的

@Configuration
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan
public class ShippingFeesApplication {

    @Autowired
    public CouchBaseBasicRepository<ShippingFeesParams> repo;

    @Autowired
    public CouchBaseConfiguration config;
    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(ShippingFeesApplication.class, args);
        ctx.close();
    }

    @Bean
    CommandLineRunner commandLineRunner(){
       return args -> { ShippingFeesParams singleObject = new ShippingFeesParams();
        String key = "GUS/ANDROID/10001";
        singleObject.setId(key);
        singleObject.setAddOnThreshold(25.00);
        singleObject.setCreatedDate(Calendar.getInstance().getTime());
        singleObject.setDeliveryType(1);
        singleObject.setFreeShippingThreshold(49.99);
        singleObject.setLastModifiedDate(Calendar.getInstance().getTime());
        AdditionShippingRules rules = new AdditionShippingRules();
        rules.setBaseShippingPrice(9.95);
        rules.setHeavyWeightShippingPrice(14.99);
        rules.setStoreId("10001");
        singleObject.setAdditionRules(rules);
        repo.save(singleObject);
        repo.findOne(key);
        Iterable<ShippingFeesParams> shippingFees = repo.findAll();
        shippingFees.forEach((shipping)-> log.info(shipping.toString()));
     };
    }
}

CouchbaseBasicRepository

@Component
public interface CouchBaseBasicRepository<BaseDataType extends CouchBaseDocument> extends CrudRepository<BaseDataType, String> {


}

沙发底座配置

@Configuration
@EnableAutoConfiguration
@EnableCouchbaseRepositories
public class CouchBaseConfiguration extends AbstractCouchbaseConfiguration {

    @Value("${couchbase.bucket.name:shiping_fees_config}")
    private String bucketName;

    @Value("${couchbase.bucket.password}")
    private String password;

    @Value("${couchbase.bootstrap-hosts:127.0.0.1}")
    private String ip;


    @Override
    protected List<String> bootstrapHosts() {
        return Arrays.asList(ip);
    }

    @Override
    public String getBucketName() {
        return bucketName;
    }

    @Override
    protected String getBucketPassword() {
        return password;
    }

}

这是我的 pom 文件

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-couchbase</artifactId>
        </dependency>
        <dependency>
            <groupId>com.staples.gp</groupId>
            <artifactId>domain-shipping</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.staples.gp</groupId>
            <artifactId>api-common</artifactId>
            <version>0.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.restdocs</groupId>
            <artifactId>spring-restdocs-mockmvc</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.janino</groupId>
            <artifactId>commons-compiler</artifactId>
            <version>2.7.8</version>
        </dependency>
    </dependencies>

【问题讨论】:

  • 这个错误是关于嵌入式tomcat的,所以它至少不太可能与couchbase集成有关
  • 您确定 couchbase 正在运行并且可以访问吗?
  • 是的,沙发底座可以使用。

标签: tomcat spring-boot java-8 couchbase


【解决方案1】:

尝试删除应用类上的冗余注释。

@Configuration
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan

可以简化为

@SpringBootApplication

这个注解是一个包含其他注解的元注解。

【讨论】:

  • 这并不能解决问题,但它是有用的建议
  • 是的!但这取决于,例如默认模式哪个组件扫描只是看一下相同的包!
【解决方案2】:

我找到了解决方案。当您的 couchbase 存储库是另一个项目的一部分并且您只是将该项目添加为 maven 依赖项的一部分时。

我们需要添加注解为

@EnableCouchbaseRepositories(basePackages = {"com.staples.gp.api.common.repo.couchbase" })

使用仓库的包名,否则spring boot会抱怨找不到仓库。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-13
    • 2021-03-18
    • 1970-01-01
    • 2023-02-09
    • 2019-02-12
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多