Spring
이클립스 spring maven 프로젝트
개갑순이돌이짱
2021. 9. 23. 14:10
maven context 검색
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.10</version>
</dependency>
maven-compiler-plugin
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>
interface Fuel{
String getFule();
}
class Water implements Fuel{
public String getFule() {
return "물";
}
}
class Airplane{
Fuel fuel;
Airplane(Fuel fuel){
this.fuel = fuel;
}
void Fly() {
System.out.println(fuel + "날다");
}
}
public class Hello {
public static void main(String[] args) {
System.out.println();
Airplane airplane = new Airplane(new Water());
airplane.Fly();
}
}
interface Fuel{
String getFule();
}
class Water implements Fuel{
public String getFule() {
return "물";
}
}
class Airplane{
Fuel fuel;
Airplane(Fuel fuel){
this.fuel = fuel;
}
void Fly() {
System.out.println(fuel + "날다");
}
}
class Factory{
Airplane makeAirplane() {
Airplane airplane = new Airplane(new Water());
return airplane;
}
}
public class Hello {
public static void main(String[] args) {
Airplane airplane = new Factory().makeAirplane();
airplane.Fly();
}
}
interface Fuel{
String getFule();
}
class Water implements Fuel{
public String getFule() {
return "물";
}
}
class Airplane{
Fuel fuel;
Airplane(Fuel fuel){
this.fuel = fuel;
}
void Fly() {
System.out.println(fuel + "날다");
}
}
class Ship{
Fuel fuel;
Ship(Fuel fuel){
this.fuel = fuel;
}
}
class Car{
Fuel fuel;
Car(Fuel fuel){
this.fuel = fuel;
}
}
class Factory{
Fuel makefuel() {
return new Water();
}
Airplane airplane() {
return new Airplane(makefuel());
}
Ship ship() {
return new Ship(makefuel());
}
Car car() {
return new Car(makefuel());
}
}
interface Fuel{
String getFule();
}
class Water implements Fuel{
public String getFule() {
return "물";
}
}
class Airplane{
Fuel fuel;
Airplane(Fuel fuel){
this.fuel = fuel;
}
void Fly() {
System.out.println(fuel + "날다");
}
}
class Ship{
Fuel fuel;
Ship(Fuel fuel){
this.fuel = fuel;
}
}
class Car{
Fuel fuel;
Car(Fuel fuel){
this.fuel = fuel;
}
}
@Configuration // 여기 안에 자동으로 객체를 생성해줌 스프링에서 사용
class Factory{
@Bean
Fuel makefuel() {
return new Water();
}
Airplane airplane() {
return new Airplane(makefuel());
}
Ship ship() {
return new Ship(makefuel());
}
Car car() {
return new Car(makefuel());
}
}
public class Hello {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(Factory.class);
//Fuel fuel = ctx.getBean("makeFuel",Fuel.class);
Fuel fuel = (Fuel)ctx.getBean("makeFuel");
System.out.println(fuel.getFule());
//Airplane airplane = new Factory().airplane(); 밑에있는 로직과 같은 방식
Airplane airplane = ctx.getBean("airplane",Airplane.class);
airplane.Fly();
ctx.close();
}
}
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) {
System.out.println(1);
// 외부에 있는 xml파일 가져오는 방법
// 밑에 GenericXmlApplicationContext 써주고
// xml파일 이름 적기
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("Context.xml");
Airplane airplane = ctx.getBean("airplane", Airplane.class);
airplane.fly();
Ship ship = ctx.getBean("ship", Ship.class);
ship.fly();
Car car = ctx.getBean("car", Car.class);
car.fly();
ctx.close();
}
}
<?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();
}
-->
<!-- makeFuel함수를 만들고 리턴값은 Water객체이다. -->
<bean id="makeFuel" class="pack.Water" />
<bean id="makeGas" class="pack.Gas" />
<!--
@Bean
Airplane airplane() {
return new Airplane(makeFuel());
}
-->
<bean id="airplane" class="pack.Airplane">
<constructor-arg ref="makeFuel"/>
</bean>
<bean id="ship" class="pack.Ship">
<constructor-arg ref="makeGas"/>
</bean>
<bean id="car" class="pack.Car">
<constructor-arg ref="makeGas"/>
</bean>
<bean id="UserDao" class="pack.Car">
<constructor-arg ref="makeGas"/>
</bean>
<bean id="MessageDao" class="pack.Car">
<constructor-arg ref="makeGas"/>
</bean>
<bean id="PrintDao" class="pack.Car">
<constructor-arg ref="makeGas"/>
</bean>
</beans>
Context.xml