configuration file이나 annotation에 기반하여, 스프링은 정확한 implementation을 제공한다.
-Create and manage objects (Inversion of Control)
-Inject object's dependencies(Dependency Injection)
-XML configuration file(legacy, but most legacy apps still use this)
-Java Annotations(modern)
-Java Source Code(modern)
1. Configure your Spring Beans
2. Create a Spring Container
3. Retrieve Beans from Spring Container
XML file: applicationContext.xml
class를 적을 때는 fully qualified class name of implementation class로 적어준다.
Spring container는 일반적으로 ApplicationContext로 알려져있다.
Specialized implementations
-ClassPathXmlApplicationContext
-AnnotationConfigApplicationContext
-GenericWebApplicationContext
-others ...
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ClassPathXmlApplicationContext()의 괄호 안에 config file의 이름을 넣는다.
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 될 클래스다.
ch 23,24 Inversion of Control 코드 쓰기 복습 (0) | 2022.11.06 |
---|---|
ch22. Spring Bean이란? (0) | 2022.09.28 |
Inversion of Control(XML Configuration) (0) | 2022.09.28 |
16~17.Downloading Spring5 JAR Files (0) | 2022.09.15 |
set up your environment (0) | 2022.09.14 |
댓글 영역