1。ノート
注釈は、注釈と同様のメカニズムです。コードに注釈を追加すると、この情報を後で使用できます。コメントとは異なり、コメントは私たちが見るべきです。 Java仮想マシンはコンパイルできず、注釈はコンパイルされませんが、反射メカニズムを介して注釈の情報を読み取ることができます。 Annotationはキーワード@interfaceを使用し、java.lang.annotition.annotationを継承します
1。Javaseの注釈
Javaseで注釈が何であるかを確認するための例を挙げましょう。キーは2つのポイントです。注釈の定義と、反射を通じて注釈の上の情報を取得する方法です。
1.最初に2つの注釈を定義します。 1つはクラスに注釈付きのclassInfoで、もう1つはメソッドの注釈付きMethodInfoです。
ClassInfo
パッケージcom.itheima10.Annotation; Import java.lang.annotation.documented; Import java.lang.annotation.elementType; Import java.lang.annotation.retention; import java.lang.annotation.RetentionPolicy; Import Java.lang.notation.target.target(theTarget(theTarget)//priste) class @retention(retentionPolicy.runtime)// Javaで使用されます。クラスファイルとランタイムアノテーション@documented //は、ヘルプ文書で生成できます@interface classinfo { /** *このアノテーションには、文字列タイプ * @return * /文字列名() string値()default "";}MethodInfo
パッケージcom.itheima10.annotation; Import java.lang.annotation.documented; import java.lang.annotation.elementType; Import java.lang.annotation.retention; import java.lang.Annotation.RetentionPolicy; Import Java.lang.Notation.Annotation.target.target(TheTarget)メソッド@retention(retentionPolicy.runtime)// Javaで使用する、クラスファイルとランタイムアノテーション@documented //を生成することができます。 string値()default "";}
2。上記で定義されたアノテーションを使用するために、annotationuseクラスを書く
パッケージcom.itheima10.annotation; @classinfo(name = "xiaopingguo118"、value = "niu")public class annotationuse {@methodinfo(name = "java"、value = "springフレームワークは重要です")パブリックボイドjava()3.テストクラスのアノテーションテストを書き、2つの注釈の上にプロパティを解析します
パッケージcom.itheima10.Annotation; import java.lang.refllect.method; import org.junit.test; public class annotationtest {public static void test(){ / ** *クラスのアノテーションを解析する場合、クラスを取得する場合、メソッドを取得します。 //クラスにclassInfoアノテーションがあるかどうかを判断しますif(class1.isannotationpresent(classinfo.class)){// classclassinfo classinfo =(classinfo)class1.getannotation(classinfo.class)で注釈を取得します。 system.out.println(classinfo.value()); system.out.println(classinfo.name()); } method [] methods = class1.getMethods(); for(method method:method){//トラバースされているメソッドのmethodinfo annotationがあります(method.isannotationpresent(methodinfo.class)){methodinfo methodinfo = method.getannotation(methodinfo.class); system.out.println(methodinfo.name()); system.out.println(methodinfo.value()); }}} @test public void test(){annotationtest.test(); }}2。春の注釈
Springフレームワークは、注釈機能を提供します。
注釈プログラミングの使用は、主にXMLファイルを置き換え、開発をより速くすることです。ただし、XMLファイルの使用は、プログラムのソースコードを変更する問題を解決することです。 XMLファイルを使用していないので、開閉の原則に違反しませんか?それは本当です。ただし、注釈も優れているため、注釈を使用するときにそれほど多くのXMLファイルを構成する必要はありません。最も重要なことは、開発効率が高いことです。 。
注釈が使用されない場合、クラスオブジェクトを宣言するために、Spring Frameworkの構成ファイルApplicationContext.xmlファイルで多くの<bean>タグを構成する必要があります。注釈を使用して、構成ファイルにタグプルを追加する必要はありません。対応する説明は、対応するクラスの「コメント」位置に命令を追加することです。具体的な紹介は次のとおりです。
•1。 @Resourceオブジェクト間の関係の組み合わせ、デフォルトは名前で組み立てることです。名前に基づいて関連するオブジェクトが見つからない場合、タイプごとの検索が継続されます。名前属性が指定されていない場合、
•アノテーションがフィールドにマークされている場合、デフォルトはフィールドの名前をBean名として取得して、依存関係オブジェクトを見つけることです。
•アノテーションが属性のセッターメソッドにマークされている場合、デフォルトの属性名はbean名として取得され、依存関係オブジェクトを見つけます。
•注:名前属性が指定されておらず、デフォルトで依存関係オブジェクトを見つけることができない場合、@Resourceアノテーションはタイプごとにアセンブリに戻ります。ただし、名前属性が指定されると、名前でのみ組み立てることができます。
•2。 @Autowired
@Autowiredはタイプごとにデフォルトで組み立てられ、@Resourceはデフォルトで名前で組み立てられ、名前を一致させる豆が見つからない場合にのみ、タイプごとに組み立てられます。解決策は、タイプごとに従属オブジェクトを組み立てることです。デフォルトでは、依存するオブジェクトが存在する必要があります。 null値が許可されている場合、falseに設定できます。
•3。 @qualifier
名前でアセンブリを使用する場合は、 @qualifierアノテーションと組み合わせて使用できます。
1。注釈を使用するには、構成ファイルに名前空間と制約ファイルの手順を追加する必要があります。
コンテキストネームスペースの導入
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance"
xmlns:context = "http://www.springframework.org/schema/context"
...
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
2。コンテキストの追加:Annotation-Configタグは構成ファイルにタグを付けます
<コンテキスト:annotation-config> </context:annotation-config>
デモの例:
学生の属性とsay()メソッドを使用して、人クラスを書いてください。コードは次のとおりです
パッケージcom.itheima10.spring.di.annotation; import javax.annotation.resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.qualifier; * */public class person {@Resource(name = "学生")私的学生学生; public void says(){this.student.say(); }}学生クラスのコードは次のとおりです
パッケージcom.itheima10.spring.di.annotation; public class entudent {public void say(){system.out.println( "desute"); }}ApplicationContext.xmlファイルを構成します
<?xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:context = "http://www.springframework.org/schema/context" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/スプリングコンテナへの学生 - > <bean id = "person"> </bean> <bean id = "desudent"> </bean> <! - 紹介Xmlns:context = "http://www.springframework.org/schema/context" http://www.springframework. http://www.springframework.org/schema/context/spring-context-2.5.xsd " - > <! - 開始してから注入されたアノテーションパーサー - > <> <annotation-config> </context:annotation-config> </beans> </beans> <
テストクラスのアノテーションテストを書き込みます
パッケージcom.itheima10.spring.di.annotation; import org.junit.test; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext;スプリングコンテナは* <コンテキスト:annotation-config> </context:annotation-config>*に解析されます。依存関係注入の注釈パーサーは開始されます* 4。注釈の属性は「「 *」の場合、「 *」です。注釈が配置されている属性の名前と、スプリングコンテナのIDの値と一致します。試合が成功した場合、割り当て *マッチが失敗した場合、割り当てはタイプに従って実行されます。試合が成功した場合、割り当てが割り当てられます *試合に失敗した場合、割り当てが報告されます *試合が失敗した場合、割り当てが直接報告されます *試合が成功しない場合、試合が成功しない場合、割り当てが直接報告されます *試合が報告されない場合、譲渡は成功しません *。参照型XMLおよび注釈。効率は比較的高いです。書くことはもっと面倒です。注釈の作成は比較的単純で効率が低い * * */public class annotationTest {@test public void testannotation(){applicationContext context = new classpathxmlapplicationContext( "applicationContext.xml"); Person person =(person)context.getBean( "person"); person.say(); }}アノテーションを使用する場合、構成ファイルの構成ファイルに人と学生をロードする必要はありません。これにより、構成ファイルの書き込みが簡素化できます。
3。スキャン
前の例では、XML Bean定義を使用してコンポーネントを構成しました。わずかに大きなプロジェクトでは、通常、数百のコンポーネントがあります。これらのコンポーネントがXML Beanの定義を使用して構成されている場合、構成ファイルのサイズが明らかに増加し、見つけて維持するのがあまり便利ではありません。 Spring2.5は、@Component、@Service、@Controller、および@Repositoryアノテーションで注釈が付けられたクラスを見つけることができ、これらのクラスを管理するスプリングコンテナに組み込んだクラスを見つけることができます。その機能は、XMLファイルでBeanノード構成コンポーネントを使用するのと同じです。自動スキャンメカニズムを使用するには、次の構成情報を開く必要があります。
1.コンテキスト名空間を紹介します
コンテキストの追加:XML構成ファイルのコンポーネントスキャンタグ
ベースパッケージは、スキャンする必要があるパッケージ(サブパッケージ)です。
例:
上記の例は、次のようにスキャンモードで記述されています
@componentPublic Class Person {@Resource(name = "学生")プライベート学生学生; public void says(){this.student.say(); }}@componentpublic class sustent {public void say(){system.out.println( "desute"); }}ApplicationContext.xmlは、1つの文で構成するだけです
<! - コンポーネントコンポーネントはクラスをスプリングコンテナに配置し、クラスはパッケージの下でコンポーネントをスキャンし、ベースパッケージで指定されたサブパッケージと呼ばれます - > <コンテキスト:Component-scanベースパッケージ= "com.itheima10.spring"> </context:component-scan>
テストクラスのアノテーションテストを書き込みます
/****1。スプリングコンテナの起動*2。スプリングコンテナ解析* <コンテキスト:コンポーネント-scanベースパッケージ= "com.itheima10.spring.scan"> </context:component-scan> 3。ベースパッケージで指定されたパッケージとサブパッケージのスキャンは、 @compontent = equibent = class @class @class = Annotation = Annotation = Annotation = Annotation 4。 to <bean id = "person"> @component( "aa")public class person {} == <bean id = "aa"> 5に相当します。@resourceの分析ステップに従ってください。プロセス全体が2回スキャンされ、効率が低下し、ライティングがより簡単になり、簡単になりやすくなります * * * * * */public vioid vioid vioid vioid vioid vioid vioid annoteat( classPathXMLApplicationContext( "ApplicationContext.xml"); Person person =(person)context.getBean( "person"); person.say(); }}インスタンスの再登場
注釈でitem51の最後のドキュメント管理システムを変更します。ドキュメントインターフェイスは、読み取りメソッドと書き込み方法で変更されていません。実装クラスは、次のように、Exceldocument、PDFDocument、およびWordDocumentです。
@Component( "ExcelDocument")Public Class ExcelDocument Implments document {public void read(){system.out.println( "excel read"); } public void write(){system.out.println( "excel write"); }}@component( "pdfdocument")public class pdfdocument emplments document {public void read(){system.out.println( "pdf read"); } public void write(){system.out.println( "pdf write"); }}@component( "worddocument")public class worddocument emplments document {public void read(){system.out.println( "word read"); } public void write(){system.out.println( "word write"); }}DocumentManager
@component( "documentmanager")public class documentmanager {@resource(name = "exceldocument")private document document; public void read(){this.document.read(); } public void write(){this.document.write(); }}構成ファイル
<コンテキスト:component-scanベースパッケージ= "com.itheima10.spring.iocdi.document">
</context:component-scan>
テストクラスのドキュメントテストを書きます
public class documentTest {@test public void testdocument(){applicationContext context = new classpathxmlapplicationContext( "applicationContext.xml"); documentManager documentmanager =(documentmanager)context.getBean( "DocumentManager"); documentmanager.read(); documentmanager.write(); }} 2。他の注釈関数の紹介
@Serviceは、ビジネスレイヤーコンポーネントとサービスレイヤー注釈の注釈に使用されます
@Controllerは、制御層コンポーネント(ストラットでのアクションなど)およびコントロールレイヤーアノテーションの注釈に使用されます
@Repositoryは、データアクセスコンポーネント、つまりDAOコンポーネントの注釈に使用されます。持続層注釈
@Componentはコンポーネントを指します。コンポーネントの分類が簡単でない場合は、この注釈を使用して注釈を付けます。
例は、MVCケースを再現します
Item51のMVCケースを再度レビューし、それぞれPersonDaoimplの注釈を追加するために、PersonDaoimpl、Personact、およびPersonServiceImplのDAO、サービス、およびアクションレイヤーを追加します。
@Repository( "PersonDao")Public Class PersonDaoimpl PersonDao {@Override public void saveSerson(){system.out.println( "save person"); }} @service( "personservice")public class personserviceimpl personservice {@resource(name = "persondao")private persondao persondao; public void setpersondao(pershdao persondao){this.persondao = persondao; } @Override public void saveperson(){this.persondao.saveperson(); }} @Controller( "Personaction")Public Class Personaction {@Resource(name = "PersonService")Private PersonService PersonService; public void setPersOnservice(personservice personservice){this.personservice = personService; } public void saveperson(){this.personservice.saveperson(); }}テストmvctestを書き込みます
public class mvctest {@test public void testmvc(){applicationContext context = new classpathxmlapplicationContext( "applicationContext.xml"); PersonAction Personaction =(Personaction)Context.getBean( "Personaction"); personact.saveperson(); }}4。春の継承
Springは継承をサポートします。これは、クラスの継承と属性継承に分けることができます。
1。クラスの継承
春のプロパティ:
(1)要約:Trueに設定した場合、それは定義された豆が抽象的であることを意味し、Beanをインスタンス化しないようにSpringに伝えます。
質問:それは抽象的なクラスでなければなりませんか?それは抽象的なクラスでしょうか?
(2)親:Bean上のBeanのIDの機能を示します。これは、Javaクラスの拡張機能に相当します。
シーン:3つの豆があります:
<bean id = "bean1" class = "…testbean"> <プロパティ名= "sex" value = "MALE"/> </bean> <bean id = "bean2" class = "…testbean"> <プロパティ名 "=" sex "value =" male "/> </bean> <bean id =" bean3 "="
変更:Spring Parent Beanを定義します
<bean id = "basebean" class = "…testbean"> <プロパティname = "sex" value = "male"/> </bean>
サブベーンを定義します
<bean id = "bean1" parent = "basebean"/>親のプロパティを継承します<bean id = "bean2" parent = "basebean"/> <bean id = "bean3" parent = "basebean">上書き
子供は、親豆の特性を継承したり、親豆の特性を上書きしたりできます。
2。属性継承
いくつかの異なる豆の間に同じ属性があり、シーンを抽出できます。
<bean id = "bean1" class = "…atestbean"> <プロパティ名= "sex" value = "MALE"/> <プロパティ名= "ref =" task "/> </bean> <bean id =" bean2 "class ="…btestbean "> <プロパティname =" sex "" mal "
変更:(1)パブリック属性を抽出します
<bean id = "basesex" abstract = "true"> <プロパティname = "sex" value = "male"/> </bean>
(2)Beanの変更
<bean id = "bean1" class = "…atestbean" parent = "basesex"> <プロパティ名= "ref =" task "/> </bean> <bean id =" bean2 "class ="…btestbean "parent =" basesex "/>>
ここでは、Beanには親とクラスの両方の属性があり、親によって指摘されたバセックスは、異なる豆が同じ属性値を共有できるようにすることです。 TransactionProxyFactoryBeanがサービスを宣言すると、Bean属性の継承は冗長なXML構成を大幅に削減できます。
注釈ベースの継承は、親属性を必要としません。
最後に小さな要約写真を見てみましょう
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。