우당탕탕 개발일지
61일차_DI (Dependency Injection) 본문
반응형
Lombok 사용
1. Lombok 등록
pom.xml 파일에 lombok 등록
STS에 롬복을 한번도 설정한 적이 없어서 STS에 설정해줘야 함.
2. STS에 Lombok 설정
- cmd로 lombok.jar 실행
- sts.exe 파일 등록
- 설치 버튼 클릭
- 나가기 버튼 클릭
3. STS.ini 파일 확인
STS.ini 파일에 lombok.jar 등록되어 있는지 확인
(한국어, 띄어쓰기 있으면 오류발생)
1. 설정 XML 파일: 의존성 주입
프로젝트명: Chapter02_XML
패키지명: sample01
환경설정 파일
- applicationContext.xml
pom.xml
- lombok 사용 X
1-1. Bean 생성
환경설정 파일에 Bean 등록
- Constructor Injection: 생성자 주입
- Setter Injection: 수정자 주입
1-2. XML 환경 파일
applicationContext.xml
essageBeanImpl 클래스를 bean 생성
<?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"
xmlns:util="http://www.springframework.org/schema/util"
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-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<beans>
<bean id="messageBeanImpl" class="sample01.MessageBeanImpl">
<constructor-arg>
<value>사과</value>
</constructor-arg>
<property name="cost">
<value>5000</value>
</property>
<property name="qty" value="3"></property>
</bean>
</beans>
1-3. Java 코드
MessageBeen.java
package sample01;
public interface MessageBeen {
public void sayHello();
public void sayHello(String fruit, int cost);
public void sayHello(String fruit, int cost, int qty);
}
MessageBeanImpl.java
@RequiredArgsConstructor
- @NonNull 이 붙은 필드에 대해 생성자 자동 생성
- @NonNull : null 들어가면 안됨
- fruit 필드 변수만 받는 생성자 자동 생성
@Setter
- 클래스가 아닌 필드 위에 위치하면 특정 필드 변수만 Setter 함수 생성
package sample01;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
@RequiredArgsConstructor
public class MessageBeanImpl implements MessageBeen {
@NonNull
private String fruit;
@Setter
private int cost;
@Setter
private int qty;
@Override
public void sayHello() {
System.out.println(fruit + "\t" + cost + "\t" + qty);
}
@Override
public void sayHello(String fruit, int cost) {
System.out.println(fruit + "\t" + cost + "\t" + qty);
}
@Override
public void sayHello(String fruit, int cost, int qty) {
System.out.println(fruit + "\t" + cost + "\t" + qty);
}
}
HelloSpring.java
context 변수 생성 시, xml 환경설정 파일 읽어 Bean 등록
messageBeen.sayHello();
- xml 환경설정 파일에서 지정한 값 출력
- fruit: 사과
- cost: 5000
- qty: 3
messageBeen.sayHello("딸기", 10000);
- qty만 xml 환경설정 파일에서 지정한 값 출력
- qty: 3
messageBeen.sayHello("참외", 3500, 10);
- 매개변수로 받은 값 출력
package sample01;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MessageBeen messageBeen = (MessageBeen) context.getBean("messageBeanImpl");
messageBeen.sayHello();
messageBeen.sayHello("딸기", 10000);
messageBeen.sayHello("참외", 3500, 10);
}
}
결과 화면
2. 설정 XML 파일: 패키지 명시
프로젝트명: Chapter02_ ANNO
패키지명: sample01
* MessageBeanImpl 클래스를 제외한 모든 java 코드 동일
환경설정 파일
- applicationContext.xml
pom.xml
- lombok 사용 O
2-1. Bean 생성
2-2. XML 환경설정 파일
applicationContext.xml
<?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-4.3.xsd">
<context:component-scan base-package="sample01"></context:component-scan>
</beans>
-------------------------여기서부터 작성해야함......------------------------------------------
2-3. Java 코드
MessageBeanImpl.java
@Value("사과")
@Autowired
package sample01;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("prototype")
public class MessageBeanImpl implements MessageBeen {
private String fruit;
private int cost;
private int qty;
// Spring에 걸려있는 Value 어노테이션
public MessageBeanImpl(@Value("사과") String fruit) {
this.fruit = fruit;
}
@Autowired // 수정자 주입 : MessageBeanImpl, Bean 객체를 가져올 때 @Autowired에 의해 값 세팅
// @Autowired: 자동 호출을 하게 만들어주는 어노테이션
public void setCost(@Value("5000") int cost) {
this.cost = cost;
}
@Autowired
public void setQty(@Value("3") int qty) {
this.qty = qty;
}
@Override
public void sayHello() {
System.out.println(fruit + "\t" + cost + "\t" + qty);
}
@Override
public void sayHello(String fruit, int cost) {
System.out.println(fruit + "\t" + cost + "\t" + qty);
}
@Override
public void sayHello(String fruit, int cost, int qty) {
System.out.println(fruit + "\t" + cost + "\t" + qty);
}
}
applicationContext.xml
Bean 생성
생성자
값이 안 나옴
생성자 주입 & 값 초기화
수정자 주입 & 값 초기화
다시 동일하게 보임
3. 환경 JAVA 파일
반응형
'비트캠프 > 이론 및 정리' 카테고리의 다른 글
64일차_MyBatis & Spring MVC (0) | 2024.10.07 |
---|---|
63일차_AOP & JDBC (방법 3가지) (1) | 2024.10.04 |
60일_Spring Framwork (0) | 2024.09.30 |
59일차_클라우드 버킷에 이미지 저장 (0) | 2024.09.26 |
58일차_Naver Cloud (0) | 2024.09.25 |