1. Spring XML Setter 주입
- SpringConfigHistroyApplication.java 파일
package com.psjw.springConfigHistroy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Arrays;
//@SpringBootApplication
public class SpringConfigHistroyApplication {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("application-context.xml");
String[] beanDefinitionNames = context.getBeanDefinitionNames();
System.out.println(Arrays.toString(beanDefinitionNames));
Object obj= context.getBean("xmlBeanService");
System.out.println(obj instanceof XmlBeanService);
XmlBeanService xmlBeanService=(XmlBeanService)obj;
System.out.println(xmlBeanService.toString());
System.out.println(xmlBeanService.xmlBeanRepo);
//SpringApplication.run(SpringConfigHistroyApplication.class, args);
}
}
- XmlBeanRepository.java 파일
package com.psjw.springConfigHistroy;
public class XmlBeanRespository {
}
- XmlBeanService.java 파일
package com.psjw.springConfigHistroy;
public class XmlBeanService {
XmlBeanRespository xmlBeanRepo;
public void setXmlBeanRepo(XmlBeanRespository xmlBeanRepo) {
this.xmlBeanRepo = xmlBeanRepo;
}
}
- application-context.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!--xmlBeanRepo : BookService의 변수명 -->
<!--xmlBeanRepository : 하단에 설정된 bean의 name을 참조 -->
<bean name="xmlBeanService" class="com.psjw.springConfigHistroy.XmlBeanService">
<property name="xmlBeanRepo" ref="xmlBeanRepository"/>
</bean>
<bean name="xmlBeanRepository" class="com.psjw.springConfigHistroy.XmlBeanRespository"/>
</beans>
- 실행 결과
2. Spring xml Contructor 주입
- SpringConfigHistroyApplication.java 파일
package com.psjw.springConfigHistroy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Arrays;
//@SpringBootApplication
public class SpringConfigHistroyApplication {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("application-context.xml");
String[] beanDefinitionNames = context.getBeanDefinitionNames();
System.out.println(Arrays.toString(beanDefinitionNames));
Object obj= context.getBean("xmlBeanService");
System.out.println(obj instanceof XmlBeanService);
XmlBeanService xmlBeanService=(XmlBeanService)obj;
System.out.println(xmlBeanService.toString());
System.out.println(xmlBeanService.xmlBeanRepo);
//SpringApplication.run(SpringConfigHistroyApplication.class, args);
}
}
- XmlBeanRepository.java 파일
package com.psjw.springConfigHistroy;
public class XmlBeanRespository {
}
- XmlBeanService.java 파일
package com.psjw.springConfigHistroy;
public class XmlBeanService {
XmlBeanRespository xmlBeanRepo;
public XmlBeanService(XmlBeanRespository xmlBeanRepo) {
this.xmlBeanRepo = xmlBeanRepo;
}
}
- application-context.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<bean name="xmlBeanService" class="com.psjw.springConfigHistroy.XmlBeanService">
<constructor-arg ref="xmlBeanRepository"/>
</bean>
<bean name="xmlBeanRepository" class="com.psjw.springConfigHistroy.XmlBeanRespository"/>
</beans>
- 실행결과
3. Spring XML ComponentScan
- SpringConfigHistroyApplication.java 파일
package com.psjw.springConfigHistroy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Arrays;
//@SpringBootApplication
public class SpringConfigHistroyApplication {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("application-context.xml");
String[] beanDefinitionNames = context.getBeanDefinitionNames();
System.out.println(Arrays.toString(beanDefinitionNames));
Object obj= context.getBean("xmlBeanService");
System.out.println(obj instanceof XmlBeanService);
XmlBeanService xmlBeanService=(XmlBeanService)obj;
System.out.println(xmlBeanService.toString());
System.out.println(xmlBeanService.xmlBeanRepo);
//SpringApplication.run(SpringConfigHistroyApplication.class, args);
}
}
- XmlComponentScanRepository.java 파일
package com.psjw.springConfigHistroy;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
@Component //or @Repository
public class XmlComponentScanRepository {
}
- XmlComponentScanService.java 파일
package com.psjw.springConfigHistroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Component //or @Service //Bean등록
public class XmlComponentScanService {
@Autowired
public XmlComponentScanRepository xmlComponentScanRepository;
}
- application-context.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<bean name="xmlBeanService" class="com.psjw.springConfigHistroy.XmlBeanService">
<constructor-arg ref="xmlBeanRepository"/>
</bean>
<bean name="xmlBeanRepository" class="com.psjw.springConfigHistroy.XmlBeanRespository"/>
</beans>
- 결과
4. Spring Java Bean 등록
- SpringConfigHistroyApplication.java 파일
package com.psjw.springConfigHistroy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Arrays;
//@SpringBootApplication
public class SpringConfigHistroyApplication {
public static void main(String[] args) {
ApplicationContext context=new AnnotationConfigApplicationContext(ApplicationConfig.class);
String[] beanDefinitionNames = context.getBeanDefinitionNames();
System.out.println(Arrays.toString(beanDefinitionNames));
Object obj=context.getBean("javaBeanService");
System.out.println(obj instanceof JavaBeanService);
JavaBeanService javaBeanService= (JavaBeanService) obj;
System.out.println(javaBeanService.toString());
//SpringApplication.run(SpringConfigHistroyApplication.class, args);
}
}
- JavaBeanRepository.java 파일
package com.psjw.springConfigHistroy;
public class JavaBeanRepository {
}
- JavaBeanService.java 파일
package com.psjw.springConfigHistroy;
public class JavaBeanService {
JavaBeanRepository javaBeanRepository;
}
- ApplicationConfig.java 파일
package com.psjw.springConfigHistroy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ApplicationConfig {
@Bean
public JavaBeanRepository javaBeanRepository(){
return new JavaBeanRepository();
}
@Bean JavaBeanService javaBeanService(){
return new JavaBeanService();
}
}
- 결과
5. Spring Java ComponentScan
- SpringConfigHistroyApplication.java 파일
package com.psjw.springConfigHistroy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Arrays;
//@SpringBootApplication
public class SpringConfigHistroyApplication {
public static void main(String[] args) {
ApplicationContext context=new AnnotationConfigApplicationContext(ApplicationConfig.class);
String[] beanDefinitionNames = context.getBeanDefinitionNames();
System.out.println(Arrays.toString(beanDefinitionNames));
Object obj=context.getBean("javaBeanService");
System.out.println(obj instanceof JavaBeanService);
JavaBeanService javaBeanService= (JavaBeanService) obj;
System.out.println(javaBeanService.toString());
System.out.println(javaBeanService.javaBeanRepository);
//SpringApplication.run(SpringConfigHistroyApplication.class, args);
}
}
- JavaBeanRepository.java 파일
package com.psjw.springConfigHistroy;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
@Repository //@Component 둘다 가능 빈등록
public class JavaBeanRepository {
}
- JavaBeanService.java 파일
package com.psjw.springConfigHistroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service //@Component 둘다 가능 빈등록
public class JavaBeanService {
@Autowired
JavaBeanRepository javaBeanRepository;
}
- ApplicationConfig.java 파일
package com.psjw.springConfigHistroy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
@Configuration
//@ComponentScan(basePackages = "com.psjw.springConfigHistroy") basePackage는 package를 직접 설정한 곳부터 하위패키지 스캔
@ComponentScan(basePackageClasses = SpringConfigHistroyApplication.class) //SpringConfigHistoryApplication class 위치한 곳 부터 하위패키지 스캔
public class ApplicationConfig {
}
- 결과
6. Spring Boot @SpringBootApplication
- ApplicationContextProvider.java 파일 ( ApplicationContext를 불러오기 위해 생성)
package com.psjw.springConfigHistroy;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextProvider implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
applicationContext = ctx;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
}
- SpringConfigHistroyApplication.java 파일
package com.psjw.springConfigHistroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Arrays;
@SpringBootApplication
public class SpringConfigHistroyApplication {
public static void main(String[] args) {
SpringApplication.run(SpringConfigHistroyApplication.class, args);
ApplicationContext context=ApplicationContextProvider.getApplicationContext();
String[] beanDefinitionNames = context.getBeanDefinitionNames();
System.out.println(Arrays.toString(beanDefinitionNames));
Object obj=context.getBean("javaBeanService");
System.out.println(obj instanceof JavaBeanService);
JavaBeanService javaBeanService= (JavaBeanService) obj;
System.out.println(javaBeanService.toString());
System.out.println(javaBeanService.javaBeanRepository);
}
}
- JavaBeanRepository.java 파일
package com.psjw.springConfigHistroy;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
@Repository //@Component 둘다 가능 빈등록
public class JavaBeanRepository {
}
- JavaBeanService.java 파일
package com.psjw.springConfigHistroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service //@Component 둘다 가능 빈등록
public class JavaBeanService {
@Autowired
JavaBeanRepository javaBeanRepository;
}
- 결과
'Spring' 카테고리의 다른 글
Spring Boot Start (0) | 2021.06.10 |
---|---|
Lombok (0) | 2021.06.09 |
Cross-Origin-Resource-Sharing(CORS) 처리 (0) | 2020.07.26 |
JUnit All Test 생성 (0) | 2020.07.26 |
3-tier Architecture (0) | 2020.07.21 |