on
스프링 5~17강 요약
스프링 5~17강 요약
728x90
5강_Dependency를 직접 Injection하기
(Program.java)
출력(print(),setExam())과 기능(kor,eng,math, com, total(),avg())으로 메서드로 각각 구성해서 인젝션
6강_이클립스 플러그인 설치
(폴더)
자바프로젝트 우클릭 - new -other - spring - 'setting.xml'
7강_DI 지시서 작성
(setting.xml)
8강_IoC 컨테이너 사용(ApplicationContext)
==>(Program.java)ApplicationContext context = new ClassPathXmlApplicationContext("spring/di/setting.xml";);
==>(폴더)프로젝트 폴더 우클릭 - [Configure]-[Convert to Maven Project]
==>(폴더 내) pom.xml 생성됨
==>(pom.xml)Dependencies탭 - Add
==>(pom.xml)
org.springframework
spring-context
5.1.9.RELEASE
==>(Program.java) Import 'ApplicationContext' (org.springframework.context)
(이렇게 하면 객체 두개 생성되서 인젝션 후 컨테이너에 담김)
==>(컨테이너에서 꺼낼때 2가지 방법)
ExamConsole console = (ExamConsole) context.getBean("console");
ExamConsole console = context.getBean(ExamConsole.class);
9강_값 형식 DI
==>(NewlecExam.java) getter setter메서드 추가
==>(setting.xml)(방법1)
==>(setting.xml)(방법2)
10
스프링 10강 - 생성자 DI
==>(NewlecExam.java) 기본 생성자, 생성자 오버로드 작성
==>(setting.xml)(생성자 오버로드 활용 방법)===========(방법1)
==>(setting.xml)(생성자 오버로드 활용 방법)(인덱스 속성 활용)===========(방법2)
==>(setting.xml)(생성자 오버로드 활용 방법)(name속성 활용)===========(방법3)
==>(setting.xml)(생성자 오버로드 활용 방법)(type속성 활용)===========(방법4)
==>(setting.xml)(namespace탭)(P체크)
==>(setting.xml)(생성자 오버로드 활용 방법)(p:속성 활용)===========(방법5)
스프링 강의 11강 _ 콜렉션 생성과 목록 DI
==>(setting.xml)(생성자 오버로드 활용 방법)===========(방법1)
==>(setting.xml)(콜렉션 객체 직접 생성 방법)===========(방법2)
[setting.xml]-[NameSpaces]-[utill 체크]
--------------------------------------------------참고----------------------------------------------------
는 객체를 직접 생성하는 방법이고.
는 보다 이전에 만든 객체꺼를 참조하여 객체를 생성하는 방식이다.
----------------------------------------------------------------------------------------------------------
스프링 강의 12강 _ 어노테이션을 이용할 때의 장점과 @Autowired를 이용한 DI 해보기
[setting.xml]-[NameSpaces]-[context 체크]
(setting.xml)
(setting.xml)
(InlineExamConsole.java) @Autowired
----------------------------참고---------------------------------
@Autowired지시 하나가
이 표현 두 줄을 대신 해준다
-----------------------------------------------------------------
스프링 강의 13강 - @Autowired의 동작방식 이해와 @Qualifier 사용하기
@Autowired의 동작방식 이해
(setting.xml)
(InlineExamConsole.java)
@Autowired
public void setExam(Exam exam) {
System.out.println("setter");
this.exam = exam;
}
------------------------------정리--------------------------------------
내 class="spring.di.entity.NewlecExam"를 먼저 찾고
동일한 bean이 있다면 그 다음으로 bean내에서 id="exam" 를 찾는다.
------------------------------------------------------------------------
@Qualifier("exam1") 방법도 있다.
(setting.xml)
(InlineExamConsole.java)
@Autowired
@Qualifier("exam1")
public void setExam(Exam exam) {
this.exam = exam;
}
------------------------------정리--------------------------------------
내 id가 모두 setExam메서드의 exam과 일치하지 않으므로
@Qualifier("exam1")와 일치하는 bean내 id를 찾는다.
------------------------------------------------------------------------
스프링 강의 14강 _ @Autowired의 위치와 required 옵션
setter 인젝션===========================(방법1)
public void setExam(Exam exam) {
this.exam = exam;
}
constructor 인젝션 (생성자 오버로드)=============(방법2)
public InlineExamConsole(Exam exam) {
this.exam = exam;
}
기본 생성자를 인젝션하는 방식=======================(방법3)
@Autowired
@Qualifier("exam2")
private Exam exam;
setting.xml에서 객체 없이===========================(방법4)
@Autowired(required=false)
@Qualifier("exam2")
private Exam exam;
스프링 강의 15강 - 어노테이션을 이용한 객체생성
형식으로 찾기===========================================(방법1)
(InlineExamConsole.java)
@Component
class InlineExamConsole
(NewlecExam.java)
@Component
public class NewlecExam implements Exam
(Program.java)
ApplicationContext context = new ClassPathXmlApplicationContext("spring/di/setting.xml")
ExamConsole console = context.getBean(ExamConsole.class);
(setting.xml)
이름으로 찾기===================================(방법2)
(InlineExamConsole.java)
@Component("console")
class InlineExamConsole
(NewlecExam.java)
@Component
public class NewlecExam implements Exam
(Program.java)
ApplicationContext context = new ClassPathXmlApplicationContext("spring/di/setting.xml")
ExamConsole console = (ExamConsole) context.getBean("console");
(setting.xml)
-------------------참고-------------------------
추가해줬다면
는 지워도 됨
------------------------------------------------
스프링 강의 16강 _ 특화된 @Component 어노테이션 (@Controller/@Service/@Repository)
어노테이션으로 생성기 기본값 초기화하는 법
@Component
public class NewlecExam implements Exam{
@Value("20")
private int kor;
@Value("20")
private int eng;
private int math;
private int com;
}
스프링 강의 17강 _ XML Configuration을 Java Configuration으로 변경하기
(NewlecDiConfig.java)
@Configuration <<설정을 위한 자바란 의미
@ComponentScan({"spring.di.ui", "spring.di.entity"}) <--
@Bean <--
public Exam exam(){
return new NewlecExam();
}
(Program.java)
xml을 쓰는 방식
ApplicationContext context
= new ClassPathXmlApplicationContext("spring/di/setting.xml");
어노테이션 방식
ApplicationContext context
=new AnnotationConfigApplicationContext(NewlecDiConfig.class);
from http://late90.tistory.com/117 by ccl(A) rewrite - 2022-01-01 03:02:03