Spring/Spring framework

Spring Bean

hoonssss 2023. 8. 21. 10:01
반응형
SMALL
package com.in28minutes.learnspringframework.helloword;

import java.util.Arrays;

import org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration.AnnotationConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class App02HelloWorldSpring {

	public static void main(String[] args) {
		
		try(var context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class)){ //클래스 호출)
				
		System.out.println(context.getBean("name")); //"호출 메소드 이름"
		
		System.out.println(context.getBean("age"));

		System.out.println(context.getBean("person"));
		
		System.out.println(context.getBean("person1"));
	
		System.out.println(context.getBean("person2"));
		
		System.out.println(context.getBean("address2"));
		
		System.out.println(context.getBean(Address.class));
		
		System.out.println(context.getBean("person4Qualifier"));
		
//		Arrays.stream(context.getBeanDefinitionNames())
//		.forEach(System.out::println());
		}
	}
}
package com.in28minutes.learnspringframework.helloword;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

record Person(String name, int age, Address adress) {};
record Address(String firstLine, String City) {};


@Configuration
public class HelloWorldConfiguration {
	
	@Bean
	public String name() {
		return "Ranga";
	}
	
	@Bean
	public int age(){
		return 15;
	}
	
	
	@Bean
	public Person person() {	
	return new Person("ravi", 20, new Address("Main Street","Utrecht"));
	}
	
	@Bean
	public Person person1() {
		return new Person(name(), age(), address()); //메소드 불러옴
	}
	
	@Bean
	public Person person2(String name, int age, Address address2) {
		return new Person(name, age, address2);
	}
	
	@Bean
	@Primary
	public Person person3(String name, int age, Address address) {
		return new Person(name, age, address);
	}
	
	@Bean
	public Person person4Qualifier(String name, int age, @Qualifier("address3Qualifier")Address address) {
		return new Person(name, age, address);
	}
	
	@Bean (name = "address2")
	@Primary //기본으로 만듬
	public Address address() {
		return new Address("South Korea", "Busan");
	}
	
	@Bean (name = "address3")
	@Qualifier("address3Qualifier") //person4Qualifier과 연결
	public Address address3() {
		return new Address("Motinager", "Hyderabad");
	}
	
}
출력값
Ranga
15
Person[name=ravi, age=20, adress=Address[firstLine=Main Street, City=Utrecht]]
Person[name=Ranga, age=15, adress=Address[firstLine=South Korea, City=Busan]]
Person[name=Ranga, age=15, adress=Address[firstLine=South Korea, City=Busan]]
Address[firstLine=South Korea, City=Busan]
Address[firstLine=South Korea, City=Busan]
Person[name=Ranga, age=15, adress=Address[firstLine=Motinager, City=Hyderabad]]

package com.in28minutes.learnspringframework;

import com.in28minutes.learnspringframework.Packmangame;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.in28minutes.learnspringframework.MarioGame;
import com.in28minutes.learnspringframework.SuperContra;
import com.in28minutes.learnspringframework.packman;

public class App03GamingSprintBean {

	public static void main(String[] args) {
		
		try(var context = new AnnotationConfigApplicationContext(GamingConfiguration.class)) 
			{
				context.getBean(GamingConsole.class).up();
				
				context.getBean(GameRunner.class).run();
				
			}
//		var game = new packman();
//		var game = new MarioGame(); MarioGame의 인스턴스 game 생성
//		//var game = new SuperContra();
//		var gameRunner = new GameRunner(game);
//		gameRunner.run();
	}

}
package com.in28minutes.learnspringframework;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.in28minutes.learnspringframework.Packmangame;

@Configuration
public class GamingConfiguration {
	
	@Bean
	public GamingConsole game() {
		var game = new packman();
		return game;
	}

	@Bean //GameRunner 만듬
	public GameRunner gameRunner(GamingConsole game) {
		var gameRunner = new GameRunner(game);
		return gameRunner;
	}
	
	
//	var game = new packman();
//	var game = new MarioGame(); MarioGame의 인스턴스 game 생성
//	//var game = new SuperContra();
//	var gameRunner = new GameRunner(game);
//	gameRunner.run();
	

}
출력값
up
Running game : com.in28minutes.learnspringframework.packman@738dc9b
up
HI down
left
right
​
반응형
LIST

'Spring > Spring framework' 카테고리의 다른 글

CDI @Named, @Inject  (0) 2023.08.25
@Lazy, @Scope, @PostConstruct, @PreDestroy  (0) 2023.08.25
@Component, @Bean 비교  (0) 2023.08.21
Spring 용어 정리  (0) 2023.08.21
Spring Framework, @Primary, @Qualifier  (0) 2023.08.21