상세 컨텐츠

본문 제목

ch21. Spring Inversion of Control overview

Spring/Spring&Hibernate 강의

by 빙하둘리 2022. 9. 28. 23:22

본문

728x90

spring provides object factory

configuration file이나 annotation에 기반하여, 스프링은 정확한 implementation을 제공한다.

 

Spring Container

ㅇPrimary functions

-Create and manage objects (Inversion of Control)

-Inject object's dependencies(Dependency Injection)

 

Configuring Spring Container

-XML configuration file(legacy, but most legacy apps still use this)

-Java Annotations(modern)

-Java Source Code(modern)

 

Spring Development Process

1. Configure your Spring Beans

2. Create a Spring Container

3. Retrieve Beans from Spring Container

 

 

1. Configure your Spring Beans

XML file: applicationContext.xml

class를 적을 때는 fully qualified class name of implementation class로 적어준다.

 

2. Create a Spring Container

Spring container는 일반적으로 ApplicationContext로 알려져있다.

Specialized implementations

-ClassPathXmlApplicationContext

-AnnotationConfigApplicationContext

-GenericWebApplicationContext

-others ...

 

 

 

 

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

ClassPathXmlApplicationContext()의 괄호 안에 config file의 이름을 넣는다.

 

3. Retrieve Beans from Spring Container

retrieving beans from container

MyApp이 give me a "Coach" object라고 하면, configuration file에 기반하여 interface의 implementation을 해줄 것이다.

//create a spring container
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//retrieve bean from spring container
Coach theCoach = context.getBean("myCoach", Coach.class);

위에서 myCoach는 실제 applicationContext.xml 파일의 bean id = "myCoach"와 일치

Coach.class는 interface 관련된 것)실제로 Coach.java는 인터페이스 파일이다.

<bean id="myCoach"
	class="com.luv2code.springdemo.BaseballCoach">
    </bean>

그리고 xml 파일에서 class="com.luv2code.springdemo.BaseballCoach"가 Coach라는 interface가 implements 될 클래스다.

 

728x90

관련글 더보기

댓글 영역