0
0

More than 1 year has passed since last update.

Spring Boot @Bean @Component 違い

Posted at

どちらで登録するオブジェクトもSpringコンテナに登録される

Bean(カスタムしたインスタンスを登録)

CustomGreeting.java
// 引数つきコンストラクタを勝手に生成してくれるアノテーション
@AllArgsConstructor
public class CustomGreeting {
	private final String name;
	private final String words;

	public void greeting() {
		System.out.println(words + name + "さん");
	}
}

カスタムインスタンスをBean登録

BeanConfiguration.java
@Configuration
public class BeanConfiguration {

	@Bean
	CustomGreeting greetTanaka() {
		return new CustomGreeting("田中", "こんにちは");
	}

	@Bean
	CustomGreeting greetJohn() {
		return new CustomGreeting("John", "hi");
	}

}

Component(クラスのインスタンスが登録される)

BeanSample.java
@Component
public class BeanSample {

	public void hello() {
		System.out.println("hello");
	}

}

実行

SampleApplication.java
@SpringBootApplication
public class SampleApplication {

	public static void main(String[] args) {
		ConfigurableApplicationContext context = SpringApplication.run(SampleApplication.class, args);
		BeanSample bean = context.getBean(BeanSample.class);
		bean.hello();

		CustomGreeting greetTanaka = (CustomGreeting) context.getBean("greetTanaka");
		greetTanaka.greeting();

		CustomGreeting greetJohn = (CustomGreeting) context.getBean("greetJohn");
		greetJohn.greeting();

		context.close();
	}

}

hello
こんにちは田中さん
hiJohnさん

Beanで登録したオブジェクトを引数に

登録されるクラス

Yuusha.java
@AllArgsConstructor
public class Yuusha {
	private String name;
	private Mahou mahou;
	
	public void attack() {
		System.out.println("勇者" + name + "の攻撃");
		mahou.hatudou();
	}
}

引数で登録するクラス

Mahou.java
@AllArgsConstructor
public class Mahou {
	private String zokusei;
	private int count;

	public void hatudou() {

		if (count-- >= 0) {
			System.out.println(zokusei + "を発動しました");
		} else {
			System.out.println("MPがありません");
		}

	}
}

Beanを登録

BeanConfiguration.java
@Configuration
public class BeanConfiguration {

	@Bean
	Yuusha yuushaYoshida(Mahou fire) {
		return new Yuusha("Yoshida", fire);
	}

	@Bean
	Mahou fire() {
		return new Mahou("ふぁいあー", 3);
	}

    // 適当なBeanも登録しておく
	@Bean
	Mahou kaminari() {
		return new Mahou("かみなり", 3);
	}

}

実行

SampleApplication.java
@SpringBootApplication
public class SampleApplication {

	public static void main(String[] args) {
		ConfigurableApplicationContext context = SpringApplication.run(SampleApplication.class, args);
		BeanSample bean = context.getBean(BeanSample.class);

		Yuusha yuushaYoshida = (Yuusha) context.getBean("yuushaYoshida");
		yuushaYoshida.attack();
		yuushaYoshida.attack();
		yuushaYoshida.attack();
		yuushaYoshida.attack();
		yuushaYoshida.attack();

		context.close();
	}

}

しっかりfireが登録されている

勇者Yoshidaの攻撃
ふぁいあーを発動しました
勇者Yoshidaの攻撃
ふぁいあーを発動しました
勇者Yoshidaの攻撃
ふぁいあーを発動しました
勇者Yoshidaの攻撃
ふぁいあーを発動しました
勇者Yoshidaの攻撃
MPがありません
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0