Spring ์ธํ
- maven project ์์ฑ
- pom.xml ์ dependency ์ถ๊ฐ : https://mvnrepository.com/
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 ํจํด์ด ๋ฌด์์ธ์ง๋ ๋ค์ ํฌ์คํ ์์ ๋ค๋ฃฐ ์์ !!
'JAVA > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring ์ปค์คํ Annotation (@Target, @Retention) (0) | 2023.09.19 |
---|---|
[JAVA] Spring Boot ํ๊ฒฝ์์ ํฐ์บฃ ์ฌ์ฉํ๊ธฐ (0) | 2021.09.27 |
[JAVA] Spring ์ผ๋ก ์ต๋ช ํฌํ(์ ๊ฑฐ) ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ #2 (0) | 2021.09.19 |
[JAVA] Spring ์ผ๋ก ์ต๋ช ํฌํ(์ ๊ฑฐ) ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐ (0) | 2021.09.17 |
[JAVA] Spring ๋ฐ์ดํฐ ์ฃผ๊ณ ๋ฐ๊ธฐ (0) | 2021.09.16 |
๋๊ธ