序文
Javaconfigは春のサブプロジェクトであることが判明しました。 Javaクラスを通じてBean定義情報を提供します。 Spring4バージョンでは、Javaconfigは正式にSpring4のコア関数になりました。
この記事では、春のJavaクラスに基づいた構成に関する関連コンテンツを詳細に紹介します。以下ではあまり言いません。詳細な紹介を一緒に見てみましょう。
1定義Bean
通常のPojoは、@Configurationアノテーションがマークされている限り、スプリングコンテナのBean定義情報を提供できます。
@configurationPublic class SystemConfig { / ** * beanを定義し、インスタンス化 * * @return * / @bean public userdao userdao(){return new userdao(); } @bean public deptdao deptdao(){return new deptdao(); } / ** *ユーザーサービスを定義し、以前に定義されたuserdaoおよびdeptdao * * @return * / @bean public userservice userservice(){userservice service = new userservice(); service.setuserdao(userdao()); service.setdeptdao(deptdao());返品サービス。 }}このクラスの方法は、@Beanアノテーション、つまり豆を定義するために注釈が付けられています。豆のタイプは、メソッドリターン値のタイプによって決定されます。名前はメソッド名とメソッド名にデフォルトです。 @bean(name =” xxx”)などのパラメーターを入力して、Bean名を指定することもできます。 @Beanでマークされたメソッド本体は、豆をインスタンス化するためのロジックを提供します。
上記の構成は、次のXMLと同等です。
<bean id = "userdao"/> <bean id = "deptdao"/> <bean id = "userservice" p:userdao-ref = "userdao" p:deptdao-ref = "deptdao"/>
XMLまたは注釈ベースの構成方法に基づくJavaクラスに基づく構成方法と比較 -
@Configuration Annotationクラス自体には@Componentがあるため、これらのクラスは普通の豆のような他の豆に注入できます。
@configurationPublic Class ApplicationConfig {@Autowired Private SystemConfig SystemConfig; @Bean Public AuthorityService AuthorityService(){AuthorityService service = new AuthorityService(); service.setuserdao(systemconfig.userdao()); service.setdeptdao(systemconfig.deptdao());返品サービス。 }}Springは、構成クラスで@BeanとマークされたすべてのメソッドのAOP強化を使用して、Beanのライフサイクル管理ロジックを導入します。たとえば、上記のSystemConfig.userdao()では、豆に対応するシングルトンを返します。
@Beanでは、@Scopeアノテーションに注釈を付けて、Beanの範囲を制御することもできます。
@scope( "prototype")@beanpublic deptdao deptdao(){return new deptdao();}これにより、Deptdao()メソッドへの各呼び出しは、新しいインスタンスを返します。
assertnotsame(authordservice.getDeptdao()。hashcode()、authordservice.getDeptdao()。hashcode());
注:構成にはJavaベースのクラスを使用し、Spring AOPおよびCGLIBライブラリをClassPathに含める必要があります。
2スプリングコンテナを起動します
2.1 @configurationクラスのみを使用します
AnnotationConfigApplicationContextクラスのコンストラクターを使用して、@ConfigurationとマークされたJavaクラスに渡して、スプリングコンテナを起動できます。
ApplicationContext Context = new AnnotationConfigApplicationContext(systemConfig.class); userservice userservice =(userservice)context.getBean( "userservice"); assertnotnull(userservice);
複数の@Configuration Configurationクラスがある場合は、AnnotationConfigApplicationContextに登録してから、コンテナを更新してこれらの構成クラスを適用できます。
AnnotationConfigApplicationContext Context = new AnnotationConfigApplicationContext(); //複数の構成クラスContext.register.Register(SystemConfig.Class); Context.Register(ApplicationConfig.Class); //コンテナ(これらの構成クラスを適用)Context.Refresh()
また、@Importを介して複数の構成クラスを1つの構成クラスに組み立ててから、組み立てられた構成クラスを登録してコンテナを起動することもできます。
@configuration @import(systemconfig.class)public class applicationconfig2 {@autowired private systemconfig systemconfig; @Bean Public AuthorityService AuthorityService(){AuthorityService service = new AuthorityService(); service.setuserdao(systemconfig.userdao()); service.setdeptdao(systemconfig.deptdao());返品サービス。 }}ユニットテスト:
annotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig2.Class); ApplicationConfig2 config = context.getBean(ApplicationConfig2.Class); AssertNotnull(config); final AuthorityService AuthorityService = config.authorityservice(); assertnotnull(authordservice.getDeptdao()); assertnotsame(authordservice.getDeptdao()。hashcode()、authoridservice .getdeptdao()。hashcode());
2.2 XMLファイルを使用して@Configurationクラスの構成を参照してください
@Configurationとマークされた構成クラスもBeanであるため、Springの<Context:Component-Scan>によってスキャンすることもできます。したがって、ConfigurationクラスをXML構成ファイルに組み立て、XML構成ファイルを介してスプリングを起動する場合は、XMLの<コンテキスト:Component-Scan>を使用して、対応する構成クラスをスキャンする必要があります。
<コンテキスト:component-scanベースパッケージ= "net.deniro.spring4.conf" resource-pattern = "applicationconfig2.class" />
2.3 @Configurationクラスの参照XMLファイルの構成
@configuration Configurationクラスでは、@Importresourceを介してXML構成ファイルを直接導入できます。そのため、@Autowiredを介してXML構成ファイルで定義されている豆を直接参照できます。
構成ファイル:
<bean id = "groupdao"/> <bean id = "roledao"/>
@configurationクラス:
@importresource( "classpath:beans5-11.xml") @configurationpublic class serviceconfig {@bean @autowired public Relationservice relationervice(Groupdao Groupdao、Roledao Roledao){Relationservice Service = New Relationservice(); service.setgroupdao(GroupDao); service.setroledao(roledao);返品サービス。 }}ユニットテスト:
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(serviceconfig.class); serviceconfig config = context.getbean(serviceconfig.class); assertnotnull(config); relationservice service = config.relationerservice((groupdao)context.get.get.getbbean " .getbean( "roledao")); assertnotnull(service.getRoledao());
さまざまな形の豆のこれらの定義情報をスプリング容器に積み込むことができる限り、春は豆間のアセンブリを賢く完成させることができます。
要約します
上記は、この記事のコンテンツ全体です。この記事の内容には、すべての人の研究や仕事に特定の参照値があることを願っています。ご質問がある場合は、メッセージを残してコミュニケーションをとることができます。 wulin.comへのご支援ありがとうございます。