3スプリングシリーズアノテーションベースのIOC

一般的なコメントでIOC春

 <bean id="accountService" class="com.mantishell.service.impl.AccountServiceImpl"
       scope="" init-method="" destroy-method="">
     <property name="" value="" | ref=""/>
 </bean>

Componment

オブジェクトを作成するために使用される、との役割 同じラベル

  • 成分:
    役割:現在のクラスは、バネ容器内に格納されたオブジェクト
    属性:ビーンIDを指定するための値- 。私たちが書いていない場合は、そのデフォルト値は、現在のクラス名と最初の文字を小文字であります
  • コントローラ:一般的にプレゼンテーション層に使用されます
  • サービス:一般的に、ビジネス層で使用されます
  • リポジトリ:永続化層は、一般的に使用されている
    これらの3つの音の役割、同じプロパティとComponment
    それらの3つが、Springフレームワークがより明確に私たちの3つの音、3つのオブジェクトのための明確な使用を提供しています

なお、注釈の使用を、あなたは次のようにコンフィギュレーション・ファイルのヘッダー情報を交換する必要があります。

<?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.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为
    context名称空间和约束中-->
    <context:component-scan base-package="com.mantishell"/>
</beans>

永続
IAccountDao.java

package com.mantishell.dao;
public interface IAccountDao {
    void saveAccount();
}

AccountDaoImpl.java

package com.mantishell.dao.impl;
import com.mantishell.dao.IAccountDao;
import org.springframework.stereotype.Repository;

@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
    public void saveAccount() {
        System.out.println("保存了账户");
    }
}

ビジネス層
IAccountService.java

package com.mantishell.service;
public interface IAccountService {
    void saveAccount();
}

AccountServiceImpl.java

package com.mantishell.service.impl;
import com.mantishell.dao.IAccountDao;
import com.mantishell.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
    @Autowired
    private IAccountDao accountDao = null;
    public void saveAccount() {
        accountDao.saveAccount();
    }
}

プレゼンテーション層

package com.mantishell.ui;
import com.mantishell.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 模拟一个表现层,用于调用业务层
 */
public class Client {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = (IAccountService) ac.getBean("accountService");
        as.saveAccount();
    }
}

Autowired

データ注入するための
書面でのBeanタグの役割とXML設定ファイルを 同じ効果

  • Autowired:
    • アクション:自動注入の種類に応じました。限り、コンテナのマッチタイプに注入するBeanオブジェクトと変数の一種類のみがあるとして、あなたは成功を注入することができます
      • 一致する変数の種類と豆の種類IOC容器が注入される場合、それが与えられています。
      • 複数のタイプが存在する場合のIOC容器に一致します:
    • ここに表示されます:変数はすることができ、それは方法することができ
    • 詳細:注釈注入を使用する場合は、セット方法は必須ではありません。
  • 修飾子:
    • 効果:名前で、その後は、上記噴射に係るクラスに基づいて注入しました。これは、クラスメンバーとAutowiredへの注入の時に一緒に使用する必要があります。単独では、噴射パラメータのときに方法を使用することができます。
    • 属性:値:豆注入されたIDを指定します。
  • 資源
    • 処置:豆のIDに応じて直接注入。これは、独立して使用することができます
    • 属性:名前:豆のIDを指定するために使用されます。
      以上の3つの注入豆は、他のタイプのデータ、および基本的な型と文字列型注入上記のコメントを達成するために使用することはできませんすることができます。
      また、注入のコレクション型は、XMLを介して達成することができます。
    • アクション:基本タイプとデータ型を注入するための文字列
    • 属性:値:指定されたデータの値。これはSPELのスプリング(即ちスプリングEL式)で使用することができる
      のSpeI文言の:式$ {}
    @Autowired
    @Qualifier("accountDao1")//指定使用id=accountDao1的bean对象
    private IAccountDao accountDao = null;

或者

    @Resource(name="accountDao2")//指定使用id=accountDao2的bean对象
    private IAccountDao accountDao = null;

範囲

スコープの変更
と同じ役割と機能豆スコープ属性タグを、共通の価値観:シングルトンプロトタイプ

@Scope("singleton")
public class AccountServiceImpl implements IAccountService {

PreDestryとPostConstruct

豆とは、タグのinit-方法Methodeのと破壊するような働きを
指定の破壊方法:PreDestroyを
PostConstruct:指定イニシャライザ

    @PostConstruct
    public void init(){
        System.out.println("初始化方法执行了");
    }

    @PreDestroy
    public void destroy(){
        System.out.println("销毁方法执行了");
    }

おすすめ

転載: www.cnblogs.com/mantishell/p/12557867.html