没有定义 [javax.sql.DataSource] 类型的限定 bean

Posted

技术标签:

【中文标题】没有定义 [javax.sql.DataSource] 类型的限定 bean【英文标题】:No qualifying bean of type [javax.sql.DataSource] is defined 【发布时间】:2016-06-28 06:53:57 【问题描述】:

这是我的 pom

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.0.1.RELEASE</version>
    </parent>


    <dependencies>
        <!-- Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
            <exclusion>
               <groupId>com.fasterxml.jackson.core</groupId>
               <artifactId>jackson-databind</artifactId>
        </exclusion>
            </exclusions>
        </dependency>


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>$guava.version</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
     <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- Spring Boot Maven -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

这是我的主要课程

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application extends SpringBootServletInitializer 

    public static void main(final String[] args) 
        SpringApplication.run(Application.class, args);
    


这是我的 application.properties

#server.contextPath=/SpringBootExamples
server.port=8888
logging.file=F:/new-spring-boot/Test-Spring-Boot.log

# spring security configuration
security.user.name=admin
security.user.password=admin1
management.security.role=SUPERUSER
spring.data.jpa.repositories.enabled=true

spring.datasource.url= jdbc:mysql://localhost:3306/springbootdb
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create-drop

这是我的 UserServiceImpl

@Service
public class UserServiceImpl  implements UserService

     @Autowired
    private UserRepository userRepo;


    @Override
    @Transactional
    public void saveUser(MyUserEntity user) 
        userRepo.save(user);
        //testTransaction();
    

**这是我的 UserRepository **

@Repository
public interface UserRepository extends CrudRepository<MyUserEntity, Long>



java -jar target/example-spring-boot-rest-1.0-SNAPSHOT.war 时我遇到了错误

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.setFilterChainProxySecurityConfigurer(org.springframework.security.config.annotation.ObjectPostProcessor,java.util.List) throws java.lang.Exception; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSecurityConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.security.core.userdetails.UserDetailsService javabeat.net.springboot.config.WebSecurityConfig.userDetailsService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySpringUserDetailsService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private final javabeat.net.springboot.service.UserService javabeat.net.springboot.service.MySpringUserDetailsService.userService; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl' defined in URL [jar:file:F:\new-spring-boot\target\example-spring-boot-rest-1.0-SNAPSHOT.war!/WEB-INF/classes!/javabeat/net/springboot/service/UserServiceImpl.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [javabeat.net.springboot.dao.UserRepository]: : Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#188ad80b' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#188ad80b': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.entityManagerFactory(org.springframework.orm.jpa.JpaVendorAdapter)] threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#188ad80b' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#188ad80b': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.entityManagerFactory(org.springframework.orm.jpa.JpaVendorAdapter)] threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)

【问题讨论】:

您使用的是example-spring-boot-rest-1.0-SNAPSHOT.war,其中声明了UserServiceImpl bean。此 bean 使用 EntitiyManager bean,而后者又使用 DataSource bean。但是DataSource bean 没有定义。 我没有在我的应用程序中明确使用 EntityManager。查看我更新的帖子 你的入门版本已经过时了1.0.1.RELEASE。我有奇怪的行为,然后我的 spring 依赖项之一来自不同的版本。你能更新到最新的spring-boot 1.3.3吗? @emilly 你不是,但 Repository 有:Error creating bean with name 'userRepository'。需要DataSource bean。 @KenBekov 那么我需要做什么? 【参考方案1】:

我升级到了

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>

效果很好。 但这真的很奇怪,因为直到一小时前它还在使用版本 1.0.1.

【讨论】:

这很常见。 Spring Boot 有很多很棒的东西,但是所有的自动魔法让它变得非常脆弱。对配置或注释进行最小的更改可能会导致一切崩溃。【参考方案2】:

我使用的是 1.3.6 版本,然后改回 1.3.3 。它对我有用

【讨论】:

以上是关于没有定义 [javax.sql.DataSource] 类型的限定 bean的主要内容,如果未能解决你的问题,请参考以下文章

Flowable入门系列文章96 - 流程定义列表

MYSQ提示“没有已定义的索引!”怎么回事?

没有为“对象”类型定义运算符“[]”。尝试定义运算符'[]'

Flutter:没有为“Object”类型定义运算符“[]”。尝试定义运算符'[]'

为啥这里没有定义 Vue?

为啥没有参数的函数(与实际函数定义相比)编译?