Spring Core]ジェネレータを呼び出してPOJOを作成する


🐶 開始前


寝る前にspringcoreの2つ目の課題を残して、寝ることにしました.
続けていけば、春になると義父の道は長くはない…!(カラムデーモン)
ではSpring 5レシピ2-2章から始めましょう.🔥

🐨 呼び出しジェネレータによるPOJOの作成


Iocコンテナからジェネレータを呼び出し、POJOインスタンス/空を作成します.
POJOクラスで1つ以上のジェネレータを定義し、Java構成クラスでIOCコンテナで使用されるPOJOインスタンス値をジェネレータとして設定した後、IoCコンテナをインスタンス化して、コメントを貼り付けるJavaクライアントをスキャンします.
これにより、アプリケーションの一部にアクセスするようにPOJOインスタンス/空にアクセスできます.


ネットショップ応用の開発を前提として、ネットショップ商品にはnameとpriceの販促がある.

- Product.java


public abstract class Product {
    private String name;
    private double price;

    public Product(){}

    public Product(String name, double price){
        this.name = name;
        this.price = price;
    }

    // 게터 및 세터

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}
ショッピングモールは様々な商品を扱っているので,それを一つのクラスとして抽象化し,サブクラス継承の構造としている.

作成者を持つPOJOクラスの作成


ショッピングサイトでBatteryとDiscを販売し、レベルを作成します.2つの商品は上記の抽象的な製品を継承します.

- Battery

public class Battery extends Product {
    private boolean rechargeable;

    public Battery() {
        super();
    }

    public Battery(String name, double price) {
        super(name, price);
    }

    public boolean isRechargeable() {
        return rechargeable;
    }

    public void setRechargeable(boolean rechargeable) {
        this.rechargeable = rechargeable;
    }

    @Override
    public String toString() {
        return "Battery{" +
                "rechargeable=" + rechargeable +
                '}';
    }
}

- Disc

public class Disc extends Product {
    private int capacity;

    public Disc() {
        super();
    }

    public Disc(String name, double price) {
        super(name, price);
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    @Override
    public String toString() {
        return "Disc{" +
                "capacity=" + capacity +
                '}';
    }
}

Java構成クラスの作成


ジェネレータを使用して、POJOインスタンス/空を生成するJava構成クラスを作成し、値を初期化します.
package com.seoyyyy.springrecipes.sequence.shop.config;

import com.seoyyyy.springrecipes.sequence.shop.domain.Battery;
import com.seoyyyy.springrecipes.sequence.shop.domain.Disc;
import com.seoyyyy.springrecipes.sequence.shop.domain.Product;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ShopConfiguration {

    @Bean
    public Product aaa(){
        Battery p1 = new Battery("AAA",2.5);
        p1.setRechargeable(true);
        return p1;
    }

    @Bean
    public Product cdrw(){
        Disc p2 = new Disc("CD-RW",1.5);
        p2.setCapacity(700);
        return p2;
    }

}

表示


Main ClassでIoC容器が正しく商品に導入されているかを確認します.

- Main.java

import com.seoyyyy.springrecipes.sequence.shop.config.ShopConfiguration;
import com.seoyyyy.springrecipes.sequence.shop.domain.Product;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
       ApplicationContext context = new AnnotationConfigApplicationContext(ShopConfiguration.class);

       Product aaa = context.getBean("aaa",Product.class);
       Product cdrw = context.getBean("cdrw",Product.class);

        System.out.println(aaa);
        System.out.println(cdrw);
    }
}

出力結果



よく写りました.

🐰 の最後の部分


章2-1とは異なり、MainクラスでgetBean呼び出しジェネレータを使用する場合、構成クラスで指定された空のID(「aaa」「cdrw」)に従って異なるオブジェクトが呼び出される.context.getBean("aaa",Product.class);にインポートされたプロダクトオブジェクトは、構成クラスでaaaビンとして検出され、マッピングされ、Batteryによって実装されるクラスが作成されます.context.getBean("cdrw",Product.class);にインポートされたプロダクトオブジェクトは、構成クラスでcdrw空を検索してマッピングし、Disc実装クラスを生成する.
次の例は明日です...そして…!🔥🔥