[EC-spring]3週目-DI、IOC、POJO


依存注入(DI[Dependency Injection])
IoC(制御反転)
POJO[Pain Old Java Object](古い単純Javaオブジェクト)
IoCはスプリングフレームのコアです
IOC容器配置と管理POJO
Springフレームワークの最も重要な意義は、POJOを使用してJavaアプリケーションを開発することです.
スプリングの主な機能はIOC容器にPOJOを配置・管理することに関係している
スプリングフレームをどの種類の項目に適用しても、最初のボタンはPOJOとIOC容器に関連しています.
「空」および「POJOインスタンス」は、Javaクラスで作成されたオブジェクトインスタンスです.
「コンポーネント」および「POJOクラス」は、オブジェクトインスタンスを作成するために必要な実際のJavaクラスです.
POJO類設計
@Configurationと@Beanを持つJava構成クラスを作成します.
@Component、@Repository、@Service、@Controllerを含むJavaコンポーネントの構成
IoCコンテナスキャン(ナビゲーション)エネルギーのJavaクラスを貼り付け、POJOインスタンス/空をアプリケーションの一部として構成


@Configurationと@Beanを持つJava構成クラスの作成


build.gradle
plugins {
    id 'org.springframework.boot' version '2.6.6'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'org.ec'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}
SequenceGenerator.java
package org.ec.springcore.sequence;

import java.util.concurrent.atomic.AtomicInteger;

public class SequenceGenerator {
    private String prefix;
    private String suffix;
    private int initial;
    private final AtomicInteger counter = new AtomicInteger();

    public SequenceGenerator() {

    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    public void setInitial(int initial) {
        this.initial = initial;
    }

    public String getSequence() {
        StringBuilder builder = new StringBuilder();
        builder
                .append(prefix)
                .append(initial)
                .append(counter.getAndIncrement())
                .append(suffix)
                ;
        return builder.toString();
    }
}
SequenceGeneratorConfig.java
package org.ec.springcore.sequence.config;

import org.ec.springcore.sequence.SequenceGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SequenceGeneratorConfig {

    @Bean
    public SequenceGenerator sequenceGenerator() {
        SequenceGenerator sequenceGenerator = new SequenceGenerator();
        sequenceGenerator.setPrefix("30");
        sequenceGenerator.setSuffix("A");
        sequenceGenerator.setInitial(100000);
        return sequenceGenerator;
    }
}
SpringcoreApplication.java
package org.ec.springcore;

import org.ec.springcore.sequence.SequenceGenerator;
import org.ec.springcore.sequence.config.SequenceGeneratorConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringcoreApplication {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(SequenceGeneratorConfig.class);


        SequenceGenerator generator = context.getBean(SequenceGenerator.class);

        System.out.println(generator.getSequence());
        System.out.println(generator.getSequence());
    }

}
301000000A
301000001A

Javaコンポーネントの構成(@Componentなど)


Sequence.java
package org.ec.springcore.sequence;

public class Sequence {

    private final String id;
    private final String prefix;
    private final String suffix;

    public Sequence(String id, String prefix, String suffix) {
        this.id = id;
        this.prefix = prefix;
        this.suffix = suffix;
    }

    public String getId() {
        return id;
    }

    public String getPrefix() {
        return prefix;
    }

    public String getSuffix() {
        return suffix;
    }
}
SequenceDao.java
package org.ec.springcore.sequence.dao;

import org.ec.springcore.sequence.Sequence;

public interface SequenceDao {

    Sequence getSequence(String sequenceId);
    int getNextValue(String sequenceId);
}
SequenceDaoImpl.java
package org.ec.springcore.sequence.dao.impl;

import org.ec.springcore.sequence.Sequence;
import org.ec.springcore.sequence.dao.SequenceDao;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@Component("sequenceDao")
public class SequenceDaoImpl implements SequenceDao {

    private final Map<String, Sequence> sequences = new HashMap<>();
    private final Map<String, AtomicInteger> values = new HashMap<>();

    public SequenceDaoImpl() {
        sequences.put("IT", new Sequence("IT", "30", "A"));
        values.put("IT", new AtomicInteger(10000));
    }

    public Sequence getSequence(String sequenceId) {
        return sequences.get(sequenceId);
    }

    public int getNextValue(String sequenceId) {
        AtomicInteger value = values.get(sequenceId);
        return value.getAndIncrement();
    }
}
SequenceGeneratorConfig.java
package org.ec.springcore.sequence.config;

import org.ec.springcore.sequence.SequenceGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;

@Configuration
@ComponentScan(
        basePackages = "org.ec.springcore.sequence",
        includeFilters = {
                @ComponentScan.Filter(
                        type = FilterType.REGEX,
                        pattern = {"org.ec.springcore.sequence.dao.*Dao"})
        },
        excludeFilters = {
                @ComponentScan.Filter(
                        type = FilterType.ANNOTATION,
                        classes = {org.springframework.stereotype.Controller.class}) }
)
public class SequenceGeneratorConfig {

    @Bean
    public SequenceGenerator sequenceGenerator() {
        SequenceGenerator sequenceGenerator = new SequenceGenerator();
        sequenceGenerator.setPrefix("30");
        sequenceGenerator.setSuffix("A");
        sequenceGenerator.setInitial(100000);
        return sequenceGenerator;
    }
}
package org.ec.springcore;

import org.ec.springcore.sequence.SequenceGenerator;
import org.ec.springcore.sequence.config.SequenceGeneratorConfig;
import org.ec.springcore.sequence.dao.SequenceDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringcoreApplication {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(SequenceGeneratorConfig.class);

        SequenceDao sequenceDao = context.getBean(SequenceDao.class);

        System.out.println(sequenceDao.getNextValue("IT"));
        System.out.println(sequenceDao.getNextValue("IT"));
    }

}

ソース


スプリング5フォーミュラ(第4版)