Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
こんなことできる?
public class Main {
public static void main(String[] args) {
Bean bean = BeanFactory.create("aBean");
bean.run();
}
}
@Component("aBean")
public class ABean {
public void run() {
System.out.println("aBeanが呼ばれた");
}
}
@Component("bBean")
public class BBean {
public void run() {
System.out.println("bBeanが呼ばれた");
}
}
@suzukitadashi
Copy link
Author

suzukitadashi commented Jan 26, 2021

呼び出すBean名を引数などで指定して、動的に呼び出したい。

@suzukitadashi
Copy link
Author

suzukitadashi commented Jan 26, 2021

ConfigurableApplicationContextを使えばいい?

@MasatoshiTada
Copy link

MasatoshiTada commented Jan 26, 2021

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        Object bean = context.getBean("aBean");
        Method run = bean.getClass().getMethod("run");
        run.invoke(bean);
    }

}

@Component("aBean")
class ABean {

    public void run() {
        System.out.println("aBeanが呼ばれた");
    }
}

@Component("bBean")
class BBean {

    public void run() {
        System.out.println("bBeanが呼ばれた");
    }
}
aBeanが呼ばれた

@MasatoshiTada
Copy link

MasatoshiTada commented Jan 26, 2021

共通のインタフェースを作ればもうちょい簡単です

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        Bean bean = context.getBean("aBean", Bean.class);
        bean.run();
    }

}

interface Bean {
    public void run();
}

@Component("aBean")
class ABean implements Bean {

    @Override
    public void run() {
        System.out.println("aBeanが呼ばれた");
    }
}

@Component("bBean")
class BBean implements Bean {

    @Override
    public void run() {
        System.out.println("bBeanが呼ばれた");
    }
}

@MasatoshiTada
Copy link

MasatoshiTada commented Jan 28, 2021

ブログで書いてらっしゃった目的なら、 CommandLineRunner で処理を書いてProfileで実行するものを切り替える方法もあります。

プロファイルについてはこちら→Springのプロファイル機能、そしてSpring Bootのapplication.properties

@Order で実行順序を指定できます(整数値の昇順)。

package com.example.demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

@Component
@Order(2)
class DefaultBean implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("defaultBeanが呼ばれた");
    }
}

@Component
@Profile("a")
@Order(1)
class ABean implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("aBeanが呼ばれた");
    }
}

@Component
@Profile("b")
@Order(1)
class BBean implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("bBeanが呼ばれた");
    }
}
$ export SPRING_PROFILES_ACTIVE=a && mvn spring-boot:run
aBeanが呼ばれた
defaultBeanが呼ばれた
$ export SPRING_PROFILES_ACTIVE=b && mvn spring-boot:run
bBeanが呼ばれた
defaultBeanが呼ばれた
$ export SPRING_PROFILES_ACTIVE= && mvn spring-boot:run
defaultBeanが呼ばれた

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment