๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JAVA/Spring

[JAVA] Spring Bean ์‚ฌ์šฉํ•˜๊ธฐ

by soy๋ฏธ๋‹ˆ 2021. 9. 24.

 

 

Spring ์„ธํŒ…

 

 

Context.xml ์— Bean ํƒœ๊ทธ ์‚ฌ์šฉํ•ด์„œ ๊ฐ์ฒด ์ƒ์„ฑํ•˜๊ธฐ

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 
		@Bean
		Fuel makeFuel(){
			return new Water();
			}	
	-->
    
	<bean id="makeFuel" class="Pack01.Water"/>
    
	<bean id="makeGas" class="Pack01.Gas"/>
    
	<!-- 
		@Bean
		Airplane makeAirplane() {
		return new Airplane(makeFuel());
		}
	 -->
     
	<bean id="makeAirplane" class="Pack01.Airplane">
		<constructor-arg ref="makeFuel"/>
	</bean>
	<bean id="makeShip" class="Pack01.Ship">
		<constructor-arg ref="makeGas"/>
	</bean>
	<bean id="makeCar" class="Pack01.Car">
		<constructor-arg ref="makeGas"/>
	</bean>
		 
</beans>
  • @Bean : ํ•จ์ˆ˜/๊ฐ์ฒด ํ˜ธ์ถœ ์ž์ฒด๋ฅผ Spring ์ด ํ•˜๋„๋ก ํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜
  • bean ํƒœ๊ทธ์˜ id : ๋งŒ๋“ค ํ•จ์ˆ˜์˜ ์ด๋ฆ„
  • bean ํƒœ๊ทธ์˜ class : ํ•จ์ˆ˜์—์„œ ๋ฆฌํ„ดํ•  ๊ฐ’ ์ •์˜
  • constructor-arg ํƒœ๊ทธ์˜ ref : ์ƒ์„ฑ์ž์˜ ์ธ์ˆ˜ ์ „๋‹ฌ์„ ์œ„ํ•œ ํƒœ๊ทธ

 

 

 

Hello.java ์ž‘์„ฑ 

 

Factory Pattern ์‚ฌ์šฉ

package Pack01;
import org.springframework.context.support.GenericXmlApplicationContext;

interface Fuel{
	String getFuel();
}

class Water implements Fuel{
	public String getFuel() {
		return "๋ฌผ";
	}
}

class Gas implements Fuel{
	public String getFuel() {
		return "๊ฐ€์Šค";
	}
}

class Airplane{
	Fuel fuel;
	Airplane(Fuel fuel) { this.fuel = fuel; }
	void fly() {
		System.out.println(fuel.getFuel()+"๋กœ ๋‚ ๋‹ค.");
	}
}

class Ship{
	Fuel fuel;
	Ship(Fuel fuel) { this.fuel = fuel; }
	void fly() {
		System.out.println(fuel.getFuel()+"๋กœ ๋‚ ๋‹ค.");
	}
}

class Car{
	Fuel fuel;
	Car(Fuel fuel) { this.fuel = fuel; }
	void fly() {
		System.out.println(fuel.getFuel()+"๋กœ ๋‚ ๋‹ค.");
	}
}

@Configuration
// Factory ํŒจํ„ด
class Factory{
	@Bean
	Fuel makeFuel() {
		return new Water();
	}
	@Bean
	Airplane makeAirplane() {
		return new Airplane(makeFuel());
	}

	Ship makeShip() {
		return new Ship(makeFuel());
	}

	Car makeCar() {
		return new Car(makeFuel());
	}

}

public class Hello {
	public static void main(String[] args) {
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("Context.xml");
		Airplane airplane = ctx.getBean("makeAirplane", Airplane.class);
		airplane.fly();
		Ship ship = ctx.getBean("makeShip", Ship.class);
		ship.fly();
		Car car = ctx.getBean("makeCar", Car.class);
		car.fly();
		}
}

 

Bean ๊ฐ์ฒด ์‚ฌ์šฉ

package Pack01;
import org.springframework.context.support.GenericXmlApplicationContext;

interface Fuel{
	String getFuel();
}

class Water implements Fuel{
	public String getFuel() {
		return "๋ฌผ";
	}
}

class Gas implements Fuel{
	public String getFuel() {
		return "๊ฐ€์Šค";
	}
}

class Airplane{
	Fuel fuel;
	Airplane(Fuel fuel) { this.fuel = fuel; }
	void fly() {
		System.out.println(fuel.getFuel()+"๋กœ ๋‚ ๋‹ค.");
	}
}

class Ship{
	Fuel fuel;
	Ship(Fuel fuel) { this.fuel = fuel; }
	void fly() {
		System.out.println(fuel.getFuel()+"๋กœ ๋‚ ๋‹ค.");
	}
}

class Car{
	Fuel fuel;
	Car(Fuel fuel) { this.fuel = fuel; }
	void fly() {
		System.out.println(fuel.getFuel()+"๋กœ ๋‚ ๋‹ค.");
	}
}

public class Hello {
	public static void main(String[] args) {
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("Context.xml");
		Airplane airplane = ctx.getBean("makeAirplane", Airplane.class);
		airplane.fly();
		Ship ship = ctx.getBean("makeShip", Ship.class);
		ship.fly();
		Car car = ctx.getBean("makeCar", Car.class);
		car.fly();
		}
}
  • Factory ํŒจํ„ด ์‚ฌ์šฉํ•˜๋‹ค๊ฐ€ bean ๊ฐ์ฒด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ณ€ํ™˜
  • GenericXmlApplicationContext : xml ์—…๋กœ๋“œ๋ฅผ ์œ„ํ•œ ํด๋ž˜์Šค
  • getBean("๋งŒ๋“  bean ์ด๋ฆ„", ๋ฆฌํ„ด ๊ฐ์ฒด);

 

 

Factory ํŒจํ„ด์ด ๋ฌด์—‡์ธ์ง€๋Š” ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ ๋‹ค๋ฃฐ ์˜ˆ์ •!!

๋Œ“๊ธ€