javaのspring-inocの使用を深く理解します。


spring-inocの使用
IOC容器は多くのフレームで使われていますが、スプリングでは最も広く使われています。フレームワークでは、多くの機能がioc技術を使用しています。次に、iocの使い方を見てみます。
  • サービスをioc容器
  • に登録する。
  • 属性注入反射対応型の例
  • 多形の場合、名称反射タイプを使用する例
  • サービスをioc容器に登録する
    @Bean登録コンポーネント
    @Bean注釈を使ってタイプの登録を行い、デフォルトではあなたのioc容器のタイプはbeanの返却値で、名前はbeanのすべての方法名で、あなたのバッグ名と直接関係がありません。もしあなたのインターフェースが多様に実現すれば、登録時に@Bean(「lind」)という方式で声明できます。@Component,@Configuration,Service,Repository これらの注釈はいずれも上記のカテゴリで宣言されていますが、@Beanは方法的に声明されています。この点に注意してください。これらの注釈は一般的に一つのインターフェースの実現を指しています。例えば、一つのデータストアインターフェースUserRepositoryは、SqlUserReporationのような複数のデータの持続化の方法があります。登録するときは、@Repositoryなどの別名をつける必要があります。ql-User RepositoryImplは、デフォルトの名前はクラス名ですが、注意してください。
    
    public interface EmailLogService {
     void send(String email, String message);
    }
    
    @Component()
    public class EmailLogServiceHttpImpl implements EmailLogService {
     private static final Logger logger = LoggerFactory.getLogger(EmailLogServiceHttpImpl.class);
    
     @Override
     public void send(String email, String message) {
     Assert.notNull(email, "email must not be null!");
     logger.info("send email:{},message:{}", email, message);
     }
    }
    @Component("email-socket")
    public class EmailLogServiceSocketImpl implements EmailLogService {
     private static final Logger logger = LoggerFactory.getLogger(EmailLogServiceSocketImpl.class);
    
     @Override
     public void send(String email, String message) {
     Assert.notNull(email, "email must not be null!");
     logger.info("send email2:{},message:{}", email, message);
     }
    }
    //            
     @Resource(name = "email-socket")
     EmailLogService socketEmail;
     @Autowired
     @Qualifier( "emailLogServiceHttpImpl")
     EmailLogService httpEmail;
    
     @Test
     public void testIoc2() {
     socketEmail.send("ok", "ok");
     }
    
    
     @Test
     public void testIoc1() {
     httpEmail.send("ok", "ok");
     }
    プログラムでbeanオブジェクトを使う
    1.Resourceを使ってbeanオブジェクトを組み立てる でbeanを呼び出す時、@Resource注釈を使ってオブジェクトを組み立てることができます。
    2.@Autowiredを使ってbeanオブジェクトを組み立てる
    @Autowiredも使えます。
    @Qualfier(「email Log ServiceHttpImpl」)の2つの注釈はプログラム中の を実現します。
    使用シーン
    いくつかの同じ挙動をしているが、実装形態が異なるシーンでは、バージョン1インターフェースとバージョン2インターフェースのように、get方法で実装されるものとは異なる。
    両方のバージョンを同時に保持します。この時は開閉原則を遵守し、新しいインターフェースを拡張し、業務上でコードを再構築します。
    二つのバージョンの同じ方法をベースに抽出し、自分で独自の方法を維持し、それらのbeanに名前をつけて組み立てる時に、
    beanの名前を組み立ててもいいです。
    擬似コードを書く:
    
    class Api_version1(){
    @Autowired 
    @Qualifier("print-version1")
    PrintService printService;
    }
    
    class Api_version2(){
    @Autowired 
    @Qualifier("print-version2")
    PrintService printService;
    }
    
    class BasePrintService{}
    
    interface PrintService{}
    
    @Service("print-version1")
    class PrintServiceImplVersion1 extends BasePrintService implements PrintService{}
    
    @Service("print-version2")
    class PrintServiceImplVersion2 extends BasePrintService implements PrintService{}
    以上は小编でご绍介したjavaのspring-inocの使い方を详しく整理しました。皆さんに助けてほしいです。もし何か疑问があれば、メッセージをください。小编はすぐに返事します。ここでも私たちのサイトを応援してくれてありがとうございます。