TIL
2022.03.23
離散数学聴力ばね242~スプリング三角形と設定について
スプリングレス注入依存性1-作成者による注入依存性
スプリングレス注入依存性1-作成者による注入依存性
package com.heaven.mvc.expert001_02;
public class Car {
Tire tire;
//유연성 증가
public Car(Tire tire) {
this.tire = tire;
}
public String getTireBrand() {
return "장착된 타이어: "+tire.getBrand();
}
}
package com.heaven.mvc.expert001_02;
public class Driver {
public static void main(String[] args) {
Tire tire = new KoreaTire();
//Tire tire = new AmericaTire();
Car car = new Car(tire);
System.out.println(car.getTireBrand());
}
}
スプリングレス注入依存性2-属性注入依存性
=> Tire tire = new Tire();
=> Car car = new Car();
=> car.setTire(tire);
package com.heaven.mvc.expert001_03;
public class Car {
Tire tire;
public String getTireBrand() {
return "장착된 타이어: "+tire.getBrand();
}
public Tire getTire() {
return tire;
}
public void setTire(Tire tire) {
this.tire = tire;
}
}
package com.heaven.mvc.expert001_03;
public class Driver {
public static void main(String[] args) {
Tire tire = new KoreaTire();
//Tire tire = new AmericaTire();
Car car = new Car();
car.setTire(tire);
System.out.println(car.getTireBrand());
}
}
スプリング注入依存性-XMLファイルの使用
運転手は総合ショッピングモールで車を買う.
運転手は自動車にタイヤ
//스프링 프레임워크에 대한 정보
//xml은 원하는 정보를 가진 파일
ApplicationContext context = new ClassPathXmlApplicationContext("com/heaven/mvc/expert002/expert002.xml");
Tire tire = context.getBean("tire", Tire.class);
Car car = context.getBean("car",Car.class);
car.setTire(tire);
package com.heaven.mvc.expert002;
//스프링 프레임워크에 대한 정보를 가지고 있는 패키지
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Driver {
public static void main(String[] args) {
//스프링 프레임워크에 대한 정보
//xml은 원하는 정보를 가진 파일
ApplicationContext context = new ClassPathXmlApplicationContext("com/heaven/mvc/expert002/expert002.xml");
Tire tire = context.getBean("tire", Tire.class);
Car car = context.getBean("car",Car.class);
car.setTire(tire);
System.out.println(car.getTireBrand());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<!-- 코드의 수정없이 xml수정 만으로도 실행 결과를 바꿀 수 있다. -->
<bean id="tire" class="com.heaven.mvc.expert002.KoreaTire"></bean>
<bean id="americaTire" class="com.heaven.mvc.expert002.AmericaTire"></bean>
<bean id="car" class="com.heaven.mvc.expert002.Car"></bean>
</beans>
スプリング注入依存性-スプリング設定ファイル(XML)から属性を注入
//스프링 프레임워크에 대한 정보
//xml은 원하는 정보를 가진 파일
ApplicationContext context = new ClassPathXmlApplicationContext("com/heaven/mvc/expert003/expert003.xml");
Car car = context.getBean("car",Car.class);
<bean id="koreaTire" class="com.heaven.mvc.expert003.KoreaTire"></bean>
<bean id="americaTire" class="com.heaven.mvc.expert003.AmericaTire"></bean>
<bean id="car" class="com.heaven.mvc.expert003.Car">
<!-- setTire()에 koreaTire를 주입 -->
<property name="tire" ref="americaTire"></property>
</bean>
スプリング注入依存性-@Autowired注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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-3.1.xsd">
<!-- anotation을 이용하겠다. -->
<context:annotation-config/>
<bean id="tire" class="com.heaven.mvc.expert004.KoreaTire"></bean>
<bean id="americaTire" class="com.heaven.mvc.expert004.AmericaTire"></bean>
<bean id="car" class="com.heaven.mvc.expert004.Car"></bean>
</beans>
package com.heaven.mvc.expert004;
import org.springframework.beans.factory.annotation.Autowired;
public class Car {
//자동주입
@Autowired
Tire tire;
public String getTireBrand() {
return "장착된 타이어: "+tire.getBrand();
}
}
package com.heaven.mvc.expert004;
//스프링 프레임워크에 대한 정보를 가지고 있는 패키지
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Driver {
public static void main(String[] args) {
//스프링 프레임워크에 대한 정보
//xml은 원하는 정보를 가진 파일
ApplicationContext context = new ClassPathXmlApplicationContext("com/heaven/mvc/expert004/expert004.xml");
Car car = context.getBean("car",Car.class);
System.out.println(car.getTireBrand());
}
}
Reference
この問題について(TIL), 我々は、より多くの情報をここで見つけました https://velog.io/@jisung/TIL-45rslo9aテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol