@PostConstruct :
- 의존하는 객체를 설정한 이후에 초기화 작업을 수행할 메서드에 적용
- init-method 속성과 같음
@PreDestroy :
- 컨테이너에서 객체를 제거하기 전에 호출 될 메서드에 적용
- destroy-method 속성과 같음
AbstractServiceImpl.java
package service;
public class AbstractServiceImpl implements AbstractService { private String name = "111"; @Override public String getClassName() { // TODO Auto-generated method stub return "AbstractServiceImpl-" + name; }
} |
AbstractServiceImpl02.java
package service;
public class AbstractServiceImpl02 implements AbstractService { private String name = "222"; @Override public String getClassName() { // TODO Auto-generated method stub return "AbstractServiceImpl-" + name; }
} |
AbstractController.java
package controller;
import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.web.servlet.ModelAndView;
import service.AbstractService;
public class AbstractController extends org.springframework.web.servlet.mvc.AbstractController {
@Resource(name="abstractService02") private AbstractService service; @PostConstruct public void postCon(){ System.out.println("PostConstruct"); } @PreDestroy public void preDes(){ System.out.println("PreDestroy"); } @Override protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { // TODO Auto-generated method stub System.out.println("START >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); System.out.println(service.getClassName()); System.out.println("END >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); return null; } } |
spring301-servlet.xml
※log
2012. 4. 25 오전 12:59:33 org.apache.catalina.core.AprLifecycleListener init 정보: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Java\jdk1.6.0_30\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Java/jdk1.6.0_30/bin/../jre/bin/client;C:/Java/jdk1.6.0_30/bin/../jre/bin;C:/Java/jdk1.6.0_30/bin/../jre/lib/i386;C:\Java\jdk1.6.0_30\bin;C:\Java\apache-tomcat-6.0.29\bin;C:\Program Files\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Common Files\Acronis\SnapAPI\;C:\Users\snoopy\AppData\Local\Bandizip\7z;C:\Program Files\IDM Computer Solutions\UltraCompare\;C:\eclipse_spring;;. 2012. 4. 25 오전 12:59:33 org.apache.tomcat.util.digester.SetPropertiesRule begin 경고: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:spring301' did not find a matching property. 2012. 4. 25 오전 12:59:33 org.apache.coyote.http11.Http11Protocol init 정보: Initializing Coyote HTTP/1.1 on http-8080 2012. 4. 25 오전 12:59:33 org.apache.catalina.startup.Catalina load 정보: Initialization processed in 368 ms 2012. 4. 25 오전 12:59:33 org.apache.catalina.core.StandardService start 정보: Starting service Catalina 2012. 4. 25 오전 12:59:33 org.apache.catalina.core.StandardEngine start 정보: Starting Servlet Engine: Apache Tomcat/6.0.29 log4j:WARN No appenders could be found for logger (org.springframework.web.filter.CharacterEncodingFilter). log4j:WARN Please initialize the log4j system properly. 2012. 4. 25 오전 12:59:33 org.apache.catalina.core.ApplicationContext log 정보: Initializing Spring FrameworkServlet 'spring301' PostConstruct 2012. 4. 25 오전 12:59:34 org.apache.coyote.http11.Http11Protocol start 정보: Starting Coyote HTTP/1.1 on http-8080 2012. 4. 25 오전 12:59:34 org.apache.jk.common.ChannelSocket init 정보: JK: ajp13 listening on /0.0.0.0:8009 2012. 4. 25 오전 12:59:34 org.apache.jk.server.JkMain start 정보: Jk running ID=0 time=0/15 config=null 2012. 4. 25 오전 12:59:34 org.apache.catalina.startup.Catalina start 정보: Server startup in 1172 ms START >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> AbstractServiceImpl-222 END >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 2012. 4. 25 오전 12:59:41 org.apache.coyote.http11.Http11Protocol pause 정보: Pausing Coyote HTTP/1.1 on http-8080 2012. 4. 25 오전 12:59:42 org.apache.catalina.core.StandardService stop 정보: Stopping service Catalina PreDestroy |