Spring启动到Tomcat部署数据源问题

时间:2017-04-28 01:02:40

标签: spring tomcat

我正在使用Spring Boot,Neo4j和MongoDB构建应用程序。我已经使用@Configuration批注从application.properties加载数据库属性,如下所示:

@Bean(name = "neoDriver")
@Profile("local")
public Driver getNeoDriver() {

    return GraphDatabase.driver(
      env.getProperty("datasource.neo4j.uri"),
      AuthTokens.basic(env.getProperty("datasource.neo4j.username"), env.getProperty("datasource.neo4j.password")),
      Config.build().toConfig());
}

autowire代码是

@Autowired
@Qualifier("neoDriver")
private Driver neoDriver;

当我从IntelliJ运行应用程序时,它运行良好;但是当我尝试在Tomcat 8.5上部署war文件时,它会出错。

Field neoDriver in com......repository.PositionRepositoryImpl required a bean of type 'org.neo4j.driver.v1.Driver' that could not be found.

Caused by:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'Controller': Unsatisfied dependency expressed through field 'positionService'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'Service': Unsatisfied dependency expressed through field 'positionRepository'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'RepositoryImpl': Unsatisfied dependency expressed through field 'neoDriver'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.neo4j.driver.v1.Driver' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=neoDriver)}

请让我知道在Tomcat上部署时我做错了什么。

1 个答案:

答案 0 :(得分:0)

我打赌你没有在Tomcat部署中将spring.profiles.active设置为local。因为您的bean标有@Profile注释,所以只有在该配置文件处于活动状态时才会创建它。