構成ファイル
前の例では、XML Bean定義を使用してコンポーネントを構成しました。わずかに大きなプロジェクトでは、通常、数百のコンポーネントがあります。これらのコンポーネントがXML Beanの定義を使用して構成されている場合、構成ファイルのサイズが明らかに増加し、見つけて維持するのがあまり便利ではありません。
Spring 2.5では、私たちに自動コンポーネントスキャンメカニズムを紹介します。 @Component、@Service、@Controller、および@Repositoryアノテーションで注釈が付けられたクラスを見つけて、これらのクラスをSpringコンテナに組み込み、管理することができます。
その機能は、XMLファイルでBeanノード構成コンポーネントを使用するのと同じです。自動スキャンメカニズムを使用するには、次の構成情報を開く必要があります。
<?xml version = "1.0" encoding = "utf-8"?> <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" xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.orgwork.org/schems.spring/ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd "> <コンテキスト:コンポーネント---scanベースパッケージ=" cn.it "/> </beans
<context:component-scan base-package="cn.itcast" /> <context:component-scan base-package="cn.itcast" /> <コンテキスト:annotation-config />プロセッサを含むアノテーションを解析する複数のプロセッサを暗黙的に登録<context:annotation-config/> 。さらに、ベースパッケージは、スキャンする必要があるパッケージ(サブパッケージ)です。
注釈
@Serviceは、ビジネスレイヤーコンポーネントの注釈に使用され、@Controllerは制御レイヤーコンポーネント(Struts2のアクションなど)に注釈を付け、 @Repositoryを使用してデータアクセスコンポーネント、つまりDAOコンポーネントに注釈を付けます。 @Componentはコンポーネントを指します。コンポーネントの分類が簡単でない場合は、この注釈を使用して注釈を付けます。
この記事は、@Autowire Annotationと自動アセンブリの場合に基づいています。
まず、Springの構成ファイルを次のように変更します。
<?xml version = "1.0" encoding = "utf-8"?> <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" xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.orgwork.org/schems.spring/ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd "> <コンテキスト:コンポーネント---scanベースパッケージ=" cn.it "/> </beans
例
次に、@Service Annotationを使用して、次のようにPersonServiceBeanクラスに注釈を付けます。
@ServicePublic Class PersonServiceBeanはPersonService {Private PersonDao Persondao; public void setpersondao(pershdao persondao){this.persondao = persondao; } @Override public void save(){persondao.add(); }}次のように、@Repositoryアノテーションを使用してPersonDaobeanクラスを注釈します。
@RepositoryPublic Class PersonDaobeanはPersondaoを実装します{@Override public void add(){system.out.println( "add()メソッドをpersondaobean"); }}最後に、SpringTestクラスのコードを次のように変更します。
public class springtest {@test public void instancespring(){abstractApplicationContext ctx = new ClassPathXMLApplicationContext( "beans.xml"); personService personService =(personService)ctx.getBean( "personservicebean"); pershdao persondao =(persondao)ctx.getbean( "persondaobean"); System.out.println(personService); System.out.println(persondao); ctx.close(); }}InstancesPring()メソッドをテストすると、Eclipseコンソールが印刷されていることがわかります。
指定された名前を使用して取得する場合は、PersonServicebeanクラスのコードを次のように変更できます。
@Service( "PersonService")Public Class PersonServiceBeanはPersonService {private persondao persondao; public void setpersondao(pershdao persondao){this.persondao = persondao; } @Override public void save(){persondao.add(); }}このようにして、SpringTestクラスのコードを次のように変更する必要があります。
public class springtest {@test public void instancespring(){abstractApplicationContext ctx = new ClassPathXMLApplicationContext( "beans.xml"); personService personService =(personService)ctx.getBean( "personservice"); System.out.println(personService); ctx.close(); }}InstancesPring()メソッドをテストすると、Eclipseコンソールが印刷されていることがわかります。
以前に春に管理された豆の範囲を学びましたが、上記の2つの春に管理された豆の範囲がデフォルトでシングルトンであることがわかります。もちろん、PersonServiceBeanクラスのコードを変更するなど、春に管理された豆の範囲を次のように変更することもできます。
@Service( "PersonService")@scope( "Prototype")Public Class PersonServiceBean Implesign PersonService {private persondao persondao; public void setpersondao(pershdao persondao){this.persondao = persondao; } @Override public void save(){persondao.add(); }}これは、Springが管理するPersonServicebeanの範囲がプロトタイプになったことを意味します。この時点で、SpringTestクラスのコードを以下に変更します。
public class springtest {@test public void instancespring(){abstractApplicationContext ctx = new ClassPathXMLApplicationContext( "beans.xml"); personService personService1 =(personService)ctx.getBean( "personservice"); personService personService2 =(personService)ctx.getBean( "personservice"); System.out.println(personservice1 == personservice2); ctx.close(); }}InstancesPring()メソッドをテストすると、Eclipseコンソールが印刷されていることがわかります。
プロトタイプの範囲とは、スプリングコンテナから豆を入手するたびに新しいオブジェクトであることを意味します。
ClassPathパスで自動的にスキャンすることにより、スプリングコンテナにコンポーネントが含まれている場合、Beanの初期化方法と破壊方法を指定する方法は?現時点では、@PostConstructと@predestroyの2つの注釈を使用する必要があります。実験のために、PersonServiceBeanクラスのコードを次のように変更しました。
@Service( "PersonService")Public Class PersonServiceBeanはPersonService {private persondao persondao; @postconstruct public void init(){system.out.println( "initialize resource"); } @predestroy public void destroy(){system.out.println( "Destroy、close resource"); } public void setPersondao(persondao persondao){this.persondao = persondao; } @Override public void save(){persondao.add(); }}次に、SpringTestクラスのコードを次のように変更する必要があります。
public class springtest {@test public void instancespring(){abstractApplicationContext ctx = new ClassPathXMLApplicationContext( "beans.xml"); personService personService =(personService)ctx.getBean( "personservice"); ctx.close(); }}このようにして、InstancesPring()メソッドをテストし、Eclipseコンソールが印刷します。
ソースコードを表示するには、クリックしてスプリングを自動的にスキャンして豆を管理してダウンロードします。
要約します
上記は、エディターが導入した自動スプリングスキャンパッケージです。私はそれが誰にでも役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は、すべての人に時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!