以下のようにシンプルなサービスクラスがありますが、
内部で処理1と処理2を1つのトランザクションに制御したいです。
@Transactional(rollbackFor = Exception.class)
を宣言したら自動でトランザクション管理をしてくれるとあったのですが、
この宣言により、
@Inject private CmpyService cmpyService;
がnullになってしまいます。
クラスに宣言したり、メソッドに宣言してみても結果は
同じでした。
不思議なのは、@Transactional宣言を外せばcmpyService
はnullにならずに登録処理は正常に動作します。
@Injectと@Transactionalに何らかの因果関係が
あるのでしょうか?
ご教授お願いします。
Spring Framework 4.2.7.RELEASE
Java
1@Service 2public class SampleService { 3 4 /** 会社マスタサービス. */ 5 @Inject 6 private CmpyService cmpyService; 7 8 /** 9 * Webコントローラから呼び出される業務サービスメソッド 10 */ 11 @Transactional(rollbackFor = Exception.class) 12 public final List<Map<String, Object>> execute( 13 final String cmpyCd 14 ) { 15 // 省略 16 17 /* 登録処理 トランザクション制御したい */ 18 // 処理1 19 cmpyService.updateA(condition); 20 // 処理2 21 cmpyService.updateB(condition); 22 }
java
1/**会社マスタ. */ 2@Service 3public class CmpyService { 4 /**会社マスタリポジトリ. */ 5 @Inject 6 private CmpyRepository cmpyRepository; 7 8 /** 9 * 登録処理A 10 */ 11 public final void updateA(final Condition condition) { 12 13 cmpyRepository.updateA(condition); 14 } 15 /** 16 * 登録処理B 17 */ 18 public final void updateB(final Condition condition) { 19 20 cmpyRepository.updateB(condition); 21 } 22}
以下追記します***********************
Java
1@Controller 2public class Mstm003Controller { 3 4 @Inject 5 private sampleService sampleService; 6 7 /** * 初期表示. */ 8 @RequestMapping 9 public final String sample( 10 final Model model, 11 final SampleForm samplForm 12 ) { 13 /* SampleFormからEntityの生成 */ 14 /* 登録処理 */ 15 sampleService.execute(condition); 16 return view; 17 }
補足で、WebコントローラがSampleServiceをDIした時点で
内包する、cmpyServiceがやはり既にNullでした。
回答1件
あなたの回答
tips
プレビュー