SpringBootの永続性

小さな

SpringBootはどのようにしてapplication.propertiesファイルからデータソース構成をフェッチしますか。

以下の構成はエンティティを永続化しますか?

モジュール1には、構成ファイルとapplication.propertiesファイルが含まれています

モジュール2には、リポジトリとサービスファイルが含まれています

現在、@ Repositoryアノテーションを使用してファイルを構成していません。

contextRepository.saveAndFlush(test);

以下のSpringBoot構成クラス:

@EnableSwagger2
@SpringBootApplication
@ComponentScan(basePackages={"ch.service"})
public class MyCodeConfiguration extends SpringBootServletInitializer {

//Module1
package ch.service.config;

@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource realDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean
@ConditionalOnMissingBean(PlatformTransactionManager.class)
public DataSourceTransactionManager transactionManager() {
    DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(realDataSource());
    return transactionManager;
}


}

以下のサービスクラス。

 //Module2
 package ch.service.config;

@Service
public class CodeServiceImpl implements CodeService {

@Autowired
private ContextRepository contextRepository;


@Transactional
public void persistValues(Testbean test){
      contextRepository.saveAndFlush(test);
  }

}

以下のリポジトリクラス

 //Module2
 package ch.service.config.dao;

public interface ContextRepository extends JpaRepository<MyContext, Long> {

}

以下のエラー:

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

Description:

Parameter 0 of method transactionManager in org.springframework.boot.autoconfigure.transaction.jta.BitronixJtaConfiguration required a bean of type 'javax.transaction.TransactionManager' that could not be found.
    - Bean method 'narayanaTransactionManager' not loaded because @ConditionalOnClass did not find required classes 'com.arjuna.ats.jta.UserTransaction', 'org.jboss.tm.XAResourceRecoveryRegistry'

アクション:

Consider revisiting the conditions above or defining a bean of type 'javax.transaction.TransactionManager' in your configuration.
Avinash

Spring Bootを使用する場合application.propertiesで次の事前定義されたDB構成キーを使用して、データベースを自動的に構成できます。

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

残りのコードは良さそうです。データを永続化する必要があります

発生する例外を修正するには、構成にトランザクションマネージャーが必要です。

@Bean
public PlatformTransactionManager transactionManager() {
    return new JpaTransactionManager();
}

参照:https//docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事