使用couchbase启动spring boot应用程序时出错创建名为'tomcatEmbeddedServletContainerFactory'的bean时出错

时间:2016-03-16 18:17:33

标签: tomcat spring-boot java-8 couchbase

我是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> {


}

CouchbaseConfiguration

@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>

2 个答案:

答案 0 :(得分:0)

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

@Configuration
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan

可以简化为

@SpringBootApplication

此注释是一个元注释,其中包含其他注释。

答案 1 :(得分:0)

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

我们需要添加注释

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

使用存储库的软件包名称,spring boot会因为无法找到存储库而抱怨。