2018년 1월 22일 월요일

spring프로젝트 java 파일에서 Spring Bean불러오기

1. Controller

@Autowired
ServletContext sc;

test t = new test();
t.test(sc);

2. JAVA

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.FrameworkServlet;

import javax.servlet.ServletContext;
import java.util.HashMap;

public class test {

    public void test(ServletContext sc) throws Exception {
        WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(sc, FrameworkServlet.SERVLET_CONTEXT_PREFIX + "dispatcherServlet");
        TESTDAO testDAO= (TESTDAO ) wac.getBean("testDAO");
        HashMap hs = testDAO.test();
    }



}
Share:

0 개의 댓글:

댓글 쓰기