2015년 12월 14일 월요일

오라클 페이징

오라클 페이징 처리에 대한 보편적인 방법


  1. SELECT *  
  2.   FROM ( SELECT ROWNUM AS RNUM,  
  3.                 A.*  
  4.            FROM ( SELECT * FROM TABLE_NAME ORDER BY COLUMN_NAME DESC) A  
  5.           WHERE ROWNUM <= 10  
  6.        )  
  7.  WHERE RNUM >= 1;  



2015년 12월 10일 목요일

프로파일(profile) 속성을 이용한 설정

동일한 스프링 빈을 여러개 만들어 놓고 상황(환경)에 따라서 적절한 스프링 빈을 사용할 수 있다. profile 속성을 사용하면 된다.

setActiveProfiles(profile); 을 이용하여 컨텍스트에 대하여 다른 스프링 빈을 사용할 수 있다.


  1. String resources = "classpath:applicationContext.xml";  
  2. String resources2 = "classpath:applicationContext2.xml";  
  3. GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();  
  4. ctx.getEnvironment().setActiveProfiles("dev");  
  5. ctx.load(resources, resources2);  
  6. ctx.refresh();  
  7.   
  8. /* do something */  
  9.   
  10. ctx.close();  

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.     http://www.springframework.org/schema/beans/spring-beans.xsd"  
  6.     profile="dev">  
  7.     <bean id="myCalculator" class="com.devchun.TwoNumber">  
  8.         <property name="num1" value="10" />  
  9.         <property name="num2" value="30" />  
  10.     </bean>  
  11. </beans>  

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.     http://www.springframework.org/schema/beans/spring-beans.xsd"  
  6.     profile="run">  
  7.     <bean id="myCalculator" class="com.devchun.TwoNumber">  
  8.         <property name="num1" value="90" />  
  9.         <property name="num2" value="120" />  
  10.     </bean>  
  11. </beans> 


위와 같이 두개의 스프링 빈(xml)에 대하여 profile 설정함에 따라 각기 다른 스프링 빈을 사용할 수 있다.

프로퍼티 파일을 이용하여 빈을 설정하는 방법

Environment객체를 이용하지 않고 프로퍼티 파일을 직접 이용하여 스프링 빈을 성정하는 방법이다.

방식에는 두 가지가 있다.

첫째, 스프링 설정 XML 파일에 프로퍼티 파일을 명시하는 방법
둘째, 스프링 설정 JAVA 파일에 프로퍼티 파일을 명시하는 방법

하지만 이번에는 많이 사용하는 첫번째 방법에 대하여 말하고자 한다.

<applicationContext.xml> - Xml Fle
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://www.springframework.org/schema/context   
  8.         http://www.springframework.org/schema/context/spring-context-3.1.xsd">  
  9.       
  10.     <context:property-placeholder location="classpath:number.properties" />  
  11.     <bean id="twoNumber" class="[classpath].TwoNumber">  
  12.         <property name="num1">  
  13.             <value>${num1}</value>  
  14.         </property>  
  15.         <property name="num2">  
  16.             <value>${num2}</value>  
  17.         </property>  
  18.     </bean>  
  19. </beans>  

<number.properties> - Properties File
  1. num1=10  
  2. num2=20  

<SpringFramework.java> - Class File
  1. public class SpringFramework implements InitializingBean, DisposableBean{  
  2.     public static void main(String args[]) {  
  3.         TwoNumber twoNumber;  
  4.           
  5.         String resources = "classpath:applicationContext.xml";  
  6.         GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();  
  7.         ctx.load(resources);  
  8.         ctx.refresh();  
  9.           
  10.         twoNumber = ctx.getBean("twoNumber", TwoNumber.class);  
  11.         twoNumber.printNumber();  
  12.     }  
  13. }  

<TwoNumber.java> - Class File
  1. public class TwoNumber {  
  2.     private int num1;  
  3.     private int num2;  
  4.       
  5.     public void setNum1(int num1) {  
  6.         this.num1 = num1;  
  7.     }  
  8.     public void setNum2(int num2) {  
  9.         this.num2 = num2;  
  10.     }  
  11.       
  12.     public void printNumber() {  
  13.         System.out.println("num1 = " + num1);  
  14.         System.out.println("num2 = " + num2);  
  15.     }  
  16. }  

출력 결과는 다음과 같다.

num1 = 10
num2 = 20



Environment 객체를 이용한 스프링 빈 설정






  1. Object object;  
  2. ConfigurableApplicationContext ctx = new GenericXmlApplicationContext();  
  3. ConfigurableEnvironment env = ctx.getEnvironment();  
  4.   
  5. MutablePropertySources propertySources = env.getPropertySources();  
  6. try {  
  7.     propertySources.addLast(new ResourcePropertySource("classpath:app.properties"));  
  8.       
  9.     System.out.println(env.getProperty("property1"));  
  10.     System.out.println(env.getProperty("property2"));  
  11. catch (IOException e) {}  
  12. GenericXmlApplicationContext gCtx = (GenericXmlApplicationContext) ctx;  
  13. gCtx.load("clathpath:appContext.xml");  
  14. gCtx.refresh();  
  15.   
  16. object = gCtx.getBean("beanID", Object.class);  
  17. // ConfigurableApplicationContext 즉, ctx에는 propertySouce로 app.properties 
  18. // 파일의 내용이 들어가 있다.  
  19. // 이 내용과 더불어 GenericXmlApplicationContext 즉, gCtx에서 appContext.xml 리소스를
  20. // 함께 사용하고 있다.  
  21. // 이 경우 ctx.getBean()으로 부터 주입된 객체는 app.properties의 내용과 appContext.xml
  22. // 모두의 설정을  사용할 수있다.  

  23. /* do something */  
  24.   
  25. ctx.close();  



스프링 빈 생명 주기 - InitializingBean, DisposableBean

스프링 빈 생명 주기

스프링 빈 생명주기를 사용하고 알아보기 위해서는 클래스에서 다음과 같은 설정을 해야한다.

implements InitializingBean, DisposableBean

InitializingBean 인터페이스를 참조할 경우 afterPropertiesSet() 메소드를 구현함으로써 빈 초기화 과정에서 사용 직전 처리에 대한 작업을 수행할 수 있다.

DisposableBean 인터페이스를 참조할 경우 destroy() 메소드를 구현함으로써 빈 소멸 과정에서 소멸 전 처리에 대한 작업을 수행할 수 있다.

두 인터페이스는 각각 사용 가능하며, 필요한 인터페이스만을 참조하여 사용하면 된다.

스프링 컨테이너 설정 중 ctx.refresh(); 를 수행하게 되면 '빈 초기화 과정에서afterPropertiesSet() 함수가 호출된다.

이후 스프링 컨테이너를 종료하기 위하여 ctx.close(); 를 수행하게 되면 destroy() 함수가 호출된다.


  1. Object object;  
  2. String resource = "classpath:appContext.xml";  
  3. GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();   
  4. // 스프링 컨테이너 생성  
  5. ctx.load(resource); // 스프링 컨테이너 설정  
  6. ctx.refresh(); // 스프링 컨테이너 설정  
  7.   
  8. object = ctx.getBean("beanID", Object.class); // 스프링 컨테이너 사용  
  9. /* do something */  
  10.   
  11. ctx.close(); // 스프링 컨테이너 종료  

  1. @Override  
  2. public void afterPropertiesSet() throws Exception {  
  3.     /* 빈 초기화 과정에서 호출  */  
  4. }  
  5. @Override  
  6. public void destroy() throws Exception {  
  7.     /* 빈 소멸 과정에서 호출 */  
  8. }