【问题标题】:Bean method 'dataSource' not loaded Spring Data JPABean 方法“dataSource”未加载 Spring Data JPA
【发布时间】:2017-02-26 11:04:02
【问题描述】:

我正在尝试使用 Spring Data JPA 实现 Spring Boot 应用程序。我已经创建了所有内容,但是在运行应用程序时出现了波纹错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration required a bean of type 'javax.sql.DataSource' that could not be found.
- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
- Bean method 'dataSource' not loaded because @ConditionalOnBean (types: org.springframework.boot.jta.XADataSourceWrapper; SearchStrategy: all) did not find any beans

Action:

Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.

这是我的代码片段

Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples</groupId>
<artifactId>Spring_JPA_Example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
    <!-- Generic properties -->
    <java.version>1.8</java.version>
</properties>

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

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.11.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
    <repository>
        <id>org.jboss.repository.releases</id>
        <name>JBoss Maven Release Repository</name>
        <url>https://repository.jboss.org/nexus/content/repositories/releases</url>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>
</project>

数据源创建:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "com.otp.entity" },
entityManagerFactoryRef="otpEntityManager",
transactionManagerRef = "transactionManager")
public class DBDataSource {

@Bean(name="otpDataSource")
@Primary
@ConfigurationProperties(prefix="spring.datasource.otp")
public DataSource otpDataSource() throws SQLException {
    try {
        System.out.println("Preparing DataSource ::");
        return DataSourceBuilder.create().build();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
@PersistenceContext(unitName="otpPersistance")
@Bean(name="otpEntityManager")
@Primary
public LocalContainerEntityManagerFactoryBean   otpEntityManager(EntityManagerFactoryBuilder builder) throws SQLException   {
    return builder.dataSource(otpDataSource())
            .persistenceUnit("otpPersistance")
            .packages("com.otp.entity").build();
}

@Bean(name="transactionManager")
@Primary
public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) throws SQLException{
    JpaTransactionManager tm = new JpaTransactionManager();
    tm.setEntityManagerFactory(otpEntityManager(builder).getObject());
    tm.setDataSource(otpDataSource());
    return tm;
}
}

PersonService.java

@Service      
@Transactional(value="transactionManager", propagation= Propagation.SUPPORTS )
public class PersonService {
@Autowired
PersonRepository personRepo;
public void savePersonDetails(PersonDTO personDto){
    try{
    Person person = new Person();
    person.setpCity(personDto.getpCity());
    person.setpName(personDto.getpName());
    person.setPid(personDto.getPid());
    System.out.println("I am saving person Details");
    personRepo.save(person);
    }catch(Exception e){
        e.printStackTrace();
    }
}
}

SpringBoot 应用程序:

@SpringBootApplication
@PropertySource("classpath:application.properties")
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
@ComponentScan({"com.otp.repository","com.otp.service"})
public class Application {
public static void main(String[] args) {
    SpringApplication.run(Application.class);
}

@Autowired
PersonService personService;

@Bean
public CommandLineRunner demo(PersonRepository repository) {
    return (args) -> {
        PersonDTO person = new PersonDTO();
        person.setPid(1009L);
        person.setpCity("Sys");
        person.setpName("John");
        personService.savePersonDetails(person);


    };
}
}

应用程序属性

spring.datasource.otp.driver-class-name:com.mysql.jdbc.Driver
spring.datasource.otp.url:jdbc:mysql://localhost:3306/otp
spring.datasource.otp.username:root
spring.datasource.otp.password:123456

spring.jpa.show-sql=true
spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none

谁能帮我解决这个问题。我哪里做错了?

【问题讨论】:

  • 你试过把@Bean(name="otpDataSource")改成@Beanpublic DataSource otpDataSource() throws SQLException改成public DataSource dataSource()吗?
  • 感谢您的回复。我已经相应地改变了,但问题是一样的。
  • @ChandrashekharGoka 基本上你想要内存(H2 数据库)和外部数据库(mysql)?
  • 嗨@KarthikPrasad,不,我只想要mySql

标签: spring jpa spring-boot spring-data-jpa


【解决方案1】:

我认为您需要将application.properties中的属性设置为

= 而不是:

spring.datasource.otp.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.otp.url:jdbc=mysql://localhost:3306/otp
spring.datasource.otp.username=root
spring.datasource.otp.password=123456

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-04
    • 2017-08-20
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    • 2019-01-08
    相关资源
    最近更新 更多