【问题标题】:spring boot build a war file deploy into an external container, error:NoSuchBeanDefinitionException: No qualifying bean of typespring boot build a war file deploy into an external container, error:NoSuchBeanDefinitionException: No qualifying bean of type
【发布时间】:2016-11-07 05:27:39
【问题描述】:

尝试构建一个既可执行又可部署到外部容器的 war 文件,但出现此错误:

org.springframework.beans.factory.BeanCreationException:创建名为“testController”的bean时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 com.jusfoun.test.service.TestService com.jusfoun.test.controller.TestController.testService;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“testServiceImpl”的 bean 时出错:注入资源依赖项失败;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖找到 [com.jusfoun.test.dao.TestMapper] 类型的合格 bean:预计至少有 1 个 bean 有资格作为此依赖的自动装配候选者。依赖注解:{@javax.annotation.Resource(shareable=true,lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}

通过java -jar test.war启动时有效

项目:

pom.xml:

<project>
    <properties>
        <java.version>1.8</java.version>
    </properties>

   <dependencies>

        <!--Quartz setting -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>
        <!--junit setting -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>

        <!-- datasource heroku -->
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP-java6</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>tools</artifactId>
                    <groupId>com.sun</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- aspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
        </dependency>
        <!-- Spring boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Provided (for embedded war support) -->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-tomcat</artifactId>
         <scope>provided</scope>
      </dependency>

        <!-- myBatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
             <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
             <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- mybatis pagehelper -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
             <version>3.6.3</version>
        </dependency>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.hateoas</groupId>
            <artifactId>spring-hateoas</artifactId>
        </dependency>
        <!-- javax -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.53</version>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <classifier>jdk15</classifier>
             <version>2.4</version>
        </dependency>

        <!-- other service dependency package of jusfoun -->

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

     </dependencies>

     <build>
        <sourceDirectory>src/main/java</sourceDirectory>
       <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
               <execution>
                  <goals>
                     <goal>repackage</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
               <useSystemClassLoader>true</useSystemClassLoader>
            </configuration>
         </plugin>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
               <source>1.8</source>
               <target>1.8</target>
            </configuration>
         </plugin>
         <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
      </plugins>

    </build>

    <packaging>war</packaging>
</project>

Application.java:

@Configuration
@ComponentScan({"com.jusfoun.test"})
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(applicationClass, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(applicationClass);
    }

    private static Class<Application> applicationClass = Application.class;
}

TestController.java

package com.jusfoun.test.controller;
    @RestController
@RequestMapping(value = "/test")
public class TestController {

    @Autowired
    private TestService testService;

    /**
     * GET /test  -> show test
     */
    @RequestMapping(value = "/get", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public String  test() {
        System.out.println("teststestestsetstsetest");
        return testService.test();
    }
}

TestService.java

package com.jusfoun.test.service;
public interface TestService {  
    String test();
}

TestServiceImpl.java

package com.jusfoun.test.service.impl;
@Service
public class TestServiceImpl implements TestService {

    @Resource
    private TestMapper testMapper;

    @Override
    public String test() {
        List<Test> tlist = testMapper.selectByExample(null);
        JSONArray array = JSONArray.fromObject(tlist);          
        return array.toString();
    }

}

TestMapper.java

package com.jusfoun.test.dao;
@Component
public interface TestMapper {
    ...
}

TestMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.jusfoun.test.dao.TestMapper" >
  ...

MapperClassName插件

package com.jusfoun.test.dao.mybatis.plugin;

import java.util.List;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
public class MapperClassNamePlugin extends PluginAdapter {

    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public void initialized(IntrospectedTable table) {
        super.initialized(table);

        table.setMyBatis3JavaMapperType(table.getMyBatis3JavaMapperType()
                .replaceAll("Mapper$", "Dao"));
    }
}

MapperXmlNamePlugin

package com.jusfoun.test.dao.mybatis.plugin;

import java.util.List;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
public class MapperXmlNamePlugin extends PluginAdapter {

    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public void initialized(IntrospectedTable table) {
        super.initialized(table);

        table.setMyBatis3JavaMapperType(table.getMyBatis3JavaMapperType()
                .replaceAll("Mapper", ""));
    }
}

DataBaseConfiguration.java

@Configuration
@EnableTransactionManagement
public class DataBaseConfiguration implements EnvironmentAware {

    private RelaxedPropertyResolver propertyResolver;

    private static Logger log = LoggerFactory
            .getLogger(DataBaseConfiguration.class);

    private Environment env;

    @Override
    public void setEnvironment(Environment env) {
        this.env = env;
        this.propertyResolver = new RelaxedPropertyResolver(env, "jdbc.");
    }

    @Bean(destroyMethod = "shutdown")
    public DataSource dataSource() {
        log.debug("Configruing DataSource");
        if (propertyResolver.getProperty("url") == null
                && propertyResolver.getProperty("databaseName") == null) {
            log.error("Your database conncetion pool configuration is incorrct ! The application "
                    + "cannot start . Please check your jdbc");
            Arrays.toString(env.getActiveProfiles());
            throw new ApplicationContextException(
                    "DataBase connection pool is not configured correctly");
        }
        HikariConfig config = new HikariConfig();
        config.setDataSourceClassName(propertyResolver
                .getProperty("dataSourceClassName"));
        if (propertyResolver.getProperty("url") == null
                || "".equals(propertyResolver.getProperty("url"))) {
            config.addDataSourceProperty("databaseName",
                    propertyResolver.getProperty("databaseName"));
            config.addDataSourceProperty("serverName",
                    propertyResolver.getProperty("serverName"));
        } else {
            config.addDataSourceProperty("url",
                    propertyResolver.getProperty("url"));
        }
        config.setUsername(propertyResolver.getProperty("username"));
        config.setPassword(propertyResolver.getProperty("password"));
        if ("com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
                .equals(propertyResolver.getProperty("dataSourceName"))) {
            config.addDataSourceProperty("cachePrepStmts",
                    propertyResolver.getProperty("cachePrepStmts"));
            config.addDataSourceProperty("prepStmtCacheSize",
                    propertyResolver.getProperty("prepStmtsCacheSize"));
            config.addDataSourceProperty("prepStmtCacheSqlLimit",
                    propertyResolver.getProperty("prepStmtCacheSqlLimit"));
            config.addDataSourceProperty("userServerPrepStmts",
                    propertyResolver.getProperty("userServerPrepStmts"));
        }
        return new HikariDataSource(config);
    }

}

MybatisConfiguration.java

@Configuration
@ConditionalOnClass({ EnableTransactionManagement.class, EntityManager.class })
@AutoConfigureAfter({ DataBaseConfiguration.class })
@MapperScan(basePackages={"com.jusfoun.test.dao"})
public class MybatisConfiguration implements EnvironmentAware {

    private static Log logger = LogFactory.getLog(MybatisConfiguration.class);

    private RelaxedPropertyResolver propertyResolver;

    @Inject
    private DataSource dataSource;

    @Override
    public void setEnvironment(Environment environment) {
        this.propertyResolver = new RelaxedPropertyResolver(environment,"mybatis.");
    }

    @Bean
    @ConditionalOnMissingBean
    public SqlSessionFactory sqlSessionFactory() {
        try {
            SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
            sessionFactory.setDataSource(dataSource);
            sessionFactory.setTypeAliasesPackage(propertyResolver
                    .getProperty("typeAliasesPackage"));
            sessionFactory
                    .setMapperLocations(new PathMatchingResourcePatternResolver()
                            .getResources(propertyResolver
                                    .getProperty("mapperLocations")));
            sessionFactory
                    .setConfigLocation(new DefaultResourceLoader()
                            .getResource(propertyResolver
                                    .getProperty("configLocation")));

            return sessionFactory.getObject();
        } catch (Exception e) {
            logger.warn("Could not confiure mybatis session factory");
            return null;
        }
    }

    @Bean
    @ConditionalOnMissingBean
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }
}

【问题讨论】:

  • 在 pom.xml 中,有 war
  • 是什么让您在问题中包含这些特定文件?错误消息提到“TestController”、“testServiceImpl”、“TestMapper”,这些都没有出现在您包含的代码中。
  • 因为 "TestController"、"testServiceImpl"、"TestMapper" 在 IDE 和 java -jar 方法中运行良好。我认为这可能是 pom 或应用程序错误。现在我在问题中添加这些文件。当我删除 TestMapper 时,它在外部 tomcat 中工作,我不知道为什么。请帮忙,谢谢。
  • 显然@Component public interface TestMapper {/* ... */} 不足以实例化 bean 并将其添加到 Spring 的上下文中。你确定这是集成 MyBatis 和 Spring 的推荐方式吗?
  • 感谢您的帮助。我不确定我是否使用推荐的方式来集成 Mybatis 和 Spring。我看 Spring Boot 参考指南(docs.spring.io/spring-boot/docs/current/reference/htmlsingle)。我的项目在 Eclipse 和 java -jar test.jar(或 test.war)中启动良好,我想它也应该在外部 tomcat 中工作,不幸的是它不是。现在我添加DataBaseConfiguration和MybatisConfiguration文件,以及问题中的项目结构图片。这是我在 stackoverflow.com 上的第一个问题,很抱歉不熟悉。

标签: spring-boot war mybatis


【解决方案1】:

使用 mybatis-spring-boot-starter (https://github.com/mybatis/spring-boot-starter),您的设置可以简化为:

移除 DataBaseConfiguration 和 MybatisConfiguration(这将由 mybatis-spring-boot-starter 完成。

修改TestMapper:

package com.jusfoun.test.dao;
@Mapper
public interface TestMapper {
    ...
}

【讨论】:

  • 我添加依赖项(org.mybatis.spring.bootmybatis-spring-boot-starter1.1.1 ),删除了DataBaseConfiguration和MybatisConfiguration。但是TestMapper添加@Mapper有错误(无法解析符号'Mapper')。
  • 导入 org.apache.ibatis.annotations.Mapper; ?
  • 'Mapper' 去掉依赖后就可以了 (org.mybatismybatis3.2.8依赖> org.mybatismybatis-spring1.2.2).
  • 运行得到这个错误:原因:org.springframework.beans.factory.BeanCreationException:在类路径资源[org/springframework/boot/autoconfigure/jdbc中定义名称为“dataSource”的bean创建错误/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]:
  • 请阅读docs.spring.io/spring-boot/docs/current/reference/html/… 了解如何定义数据源,数据源定义也适用于 spring-jdbc、spring-data-jpa 等
【解决方案2】:

我在 MybatisConfiguration 中删除了@ConditionalOnClass 的注解。 当战争部署到外部容器(tomcat8)中时,该项目有效。 在删除注释之前,它适用于 java -jar xxx.war 但不适用于部署到外部 tomcat。

@Configuration
@AutoConfigureAfter({ DataBaseConfiguration.class })
@MapperScan(basePackages={"com.jusfoun.test.dao"})
public class MybatisConfiguration implements EnvironmentAware {

【讨论】:

    猜你喜欢
    • 2022-12-12
    • 2022-12-27
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 2018-01-02
    • 2022-12-01
    • 2022-12-02
    • 1970-01-01
    相关资源
    最近更新 更多