SPRING
spring standalone application 에서 @service 실행
spring standalone application 에서 @service 실행 스프링 프로젝트에서 Main 함수안에서 기존에 구축된 @service 를 사용하려면 다음과 같이 하면 된다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package xxx.xxx.www.test.console; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; import xxx.xxx.www.test.service.TestService; @Component public class Main { private static final String CONFIG_PATH = "classpath*:spring/all-config.xml"; public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext(CONFIG_PATH); Main p = context.getBean(Main.class); p.start(args); } @Autowired private TestService serv; private void start(String[] args) throws Exception { Map map = new HashMap(); map.put("docnum", "108"); List list = serv.selectList(map); System.out.println("결과 logger:::::::::::::::: " + list); } } |
한참 해맸었던 이유가 있는데, 기존에 스프링 환경은 아래와 같이 각 파트별로 구분지어 만들어진 환경이다. spring-common.xml spring-datasource.xml …. 이걸 부분만 적용하려니 잘 되지 않았다. 아래와 같이 한꺼번에 가져오도록 xml 파일을 하나 가져와서 해결.
1 2 3 4 5 6 7 8 |
<?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-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <import resource="classpath*:spring/spring-*.xml"/> </beans> |