、IOCのコメント
豆の容器に注入するための1。春:
- @Component:春の豆の容器に注入します
- @Repository:ラベル層ダオ
- @Service:ビジネス層のためのサービスラベル
- @Controller:コントローラクラスラベル
フィッティングアセンブリ豆を達成するためのデータを取得するための2。
- @Autowired:ByTypeのデフォルトモードを、同じ名前のクラスが表示された場合、注入種別に応じて必要とされていない@Qualifier指定されたIDを
- 。@Resourceは: デフォルトの場合、BYNAME道を名前がに従い、デフォルト値を行い、注射ByType方法
IOCのコメントの場合
(1)大型パッケージ注釈プロファイルスキャナ、パッケージを追加しました
< コンテキスト:コンポーネント・スキャンベースパッケージ=「cn.spring.aop」/>
ベース・パッケージが、複数の、パッケージ名パッケージ名を充填されている「」区別するため
(2)定義及び実装クラスに対応するインタフェース
@Repository( "mapperImpl" ) パブリック クラス MapperImplが実装マッパー{ @Override 公共 int型の追加(StudentBean STU){ System.out.printlnは( "123" )。 リターン 0 ; } }
@Service( "StuServiceImpl" ) パブリック クラス StuServiceImplが実装{StuServiceを @Resource プライベートマッパーマッパー。 @Override 公共 int型の追加(StudentBean STU){ 戻りmapper.add(STU)。 } }
(3)試験クラス
@Test 公共 ボイドIocTestBy(){ ApplicationContextのコンテキスト = 新しい ClassPathXmlApplicationContext( "applicationContext.xmlを" )。 StuService stuServiceImpl =(StuService)context.getBean( "StuServiceImpl" )。 stuServiceImpl.add(新しいStudentBean()); }
二、AOPのコメント
- @Aspect:ステートメントセクション
- @Ponitcut:公式声明接点式
- @Before:フロント強化します
- @AfterReturning:背面強化
- @Around:拡張サラウンド
- @AfterThrowing:強化された例外がスローされます
- @After:最終的に向上させます
メモは、拡張機能の多様性を実現します
(1)大規模構成ファイルのオープンAOPのコメント
< AOP:AspectJの-自動プロキシ/>
(2)インタフェース
@Service( "aopServices" ) パブリック クラスAopServices { 公共 ボイド doSome()がスロー例外{ / * int型I = 1/0。* / System.out.printlnは( "サービス层业务" ); } }
(3)強化クラス
@Aspect @Component パブリック クラスアドバイス{ // 設定ポイントカット表現 @Pointcut( "実行(AOP .. *。*。*(..))" ) 公共 ボイド ポイントカット(){ } // 前部補強 @Before( "ポイントカット()" ) 公共 ボイド(前){ するSystem.out.println( "フロント========= =======" ); } // 後部補強 @ afterReturning( "ポイントカット()" ) 公共 ボイドafterReturning(){ System.out.printlnは( "リア======= ==========" ); } //エンハンストサラウンド @Around( "ポイントカット()" ) パブリックオブジェクトの周囲(JP ProceedingJoinPoint)がスローされたThrowable { するSystem.out.println( "サラウンドフロント======= ==========を" )。 進行オブジェクト = jp.proceed(); のSystem.out.println( "サラウンドリア======= ==========" ); 戻り進む; } // の異常な増強 @AfterThrowing(ポイントカット= "実行(AOP .. * * * *(..)..)"、投げ= "EX" ) 公共 ボイドAfterThrowing(例外EX){ するSystem.out.println( "異常" )。 } //最終的に増強 @After( "ポイントカット()" ) 公共 ボイドをした後(){ System.out.printlnは( "======= =========によって最終" ); } }
(4)試験クラス
パブリック クラスAopTest { 公共 静的 ボイドメイン(文字列[]引数){ ApplicationContextのコンテキスト = 新しい ClassPathXmlApplicationContext( "applicationContext.xmlを" )。 AopServices aopServices =(AopServices)context.getBean( "aopServices" )。 試す{ aopServices.doSome()。 } キャッチ(例外e){ e.printStackTrace(); } } }