블로그 이미지

Rurony's Training Gym

Rurony의 트레이닝 도장! by Rurony


'spring'에 해당되는 글 3건

  1. 2012.06.09 Spring JAXB를 활용한 Tree 구현
  2. 2011.12.09 Spring quartz(Scheduler) 사용
  3. 2011.12.09 Spring Bean을 Servlet Filter에서 사용1

Spring JAXB를 활용한 Tree 구현

Spring OXM모듈의 JAXB를 활용한 Tree 구현 예제.

1. spring-oxm 라이브러리 추가 (maven dependency 사용)
jar download url : http://mvnrepository.com/artifact/org.springframework/spring-oxm/3.0.5.RELEASE


2. Spring Bean 설정

XML 응답을 위한 MarshallingView 등록하고 XML 출력을 위한 바운드 객체를 property로 주입.

<bean id="xmlViewer" class="org.springframework.web.servlet.view.xml.MarshallingView">

<constructor-arg>

<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">

<property name="classesToBeBound">

<list>

<value>com.rurony.format.common.tree.TreeNode</value>

</list>

</property>

</bean>

</constructor-arg>

</bean>


3. XML 출력을 위한 바운드 객체

XML 출력을 위해 JAXB annotation을 설정하고 Tree 구현을 위해 객체 내에 하위 객체를 포함하도록 구현.

@XmlRootElement(name="Tree")

@XmlAccessorType(XmlAccessType.FIELD)

public class TreeNode {

@XmlAttribute

private String option;

@XmlAttribute

private String status;

@XmlAttribute

private String name;

@XmlAttribute

private int depth;

@XmlAttribute

private int seq;

@XmlAttribute

private String type;

@XmlAttribute

private String parentId;

@XmlAttribute

private String id;

@XmlElement(name="Node")

protected List<TreeNode> nodes = new ArrayList<TreeNode>();

 

public TreeNode() {

}


public TreeNode(String id, String parentId, String type, int seq, String name, String status, String option) {

this.id = id;

this.parentId = parentId;

this.type = type;

this.seq = seq;

this.depth = 0;

this.name = name;

this.status = status;

this.option = option;

}


public void add(TreeNode node) {

if (id.equals(node.parentId)) {

node.depth = depth + 1;

nodes.add(node);

return;

}

Iterator<TreeNode> iter = nodes.iterator();

while (iter.hasNext()) {

iter.next().add(node);

}

}  


[... Setter/Getter ...]

public List<TreeNode> getNodes() {

return nodes;

}


public void setNodes(List<TreeNode> nodes) {

this.nodes = nodes;

}

}


4. MakeTreeNode Class

XML 바운드 객체을 사용하여 Tree XML을 구현하는 객체.

public class MakeTreeNode {

TreeNode treeNode;

public MakeTreeNode(String id, String parentId, String type, int seq, String name, String status, String option) {

treeNode = new TreeNode(id, parentId, type, seq, name, status, option);

}

public MakeTreeNode() {

treeNode = new TreeNode("0", "-1", "1", 1, "ROOT", "1", "");

}


public void add(TreeNode node) {

treeNode.add(node);

}


public void addAll(List<TreeNode> nodes) {

Iterator<TreeNode> iter = nodes.iterator();

while (iter.hasNext()) {

add(iter.next());

}

}

public TreeNode getTreeXmlNode() {

return treeNode;

}

} 


5. Test Tree Util

Domo Tree 추가

public static TreeNode getTestTreeXml() {

List<TreeNode> treeList = new ArrayList<TreeNode>();

treeList.add(new TreeNode("1", "0", "type", 1, "Node 1", "status", "option"));

treeList.add(new TreeNode("2", "1", "type", 1, "Node 1-1", "status", "option"));

treeList.add(new TreeNode("3", "1", "type", 2, "Node 1-2", "status", "option"));

treeList.add(new TreeNode("4", "0", "type", 2, "Node 2", "status", "option"));

treeList.add(new TreeNode("5", "4", "type", 1, "Node 2-1", "status", "option"));

treeList.add(new TreeNode("6", "4", "type", 2, "Node 2-2", "status", "option"));

treeList.add(new TreeNode("7", "5", "type", 1, "Node 2-1-1", "status", "option"));

treeList.add(new TreeNode("8", "5", "type", 2, "Node 2-1-2", "status", "option"));

treeList.add(new TreeNode("9", "0", "type", 3, "Node 3", "status", "option"));

treeList.add(new TreeNode("10", "9", "type", 1, "Node 3-1", "status", "option"));

MakeTreeNode tree = new MakeTreeNode("0", "-1", "1", 1, "Tree Root", "status", "option");

tree.addAll(treeList);

return tree.getTreeXmlNode();

}


6. Controller

@ResponseBody annotation을 사용하여 Url Mapping.

@RequestMapping("/testTreeXML")

public @ResponseBody TreeNode treeXML() {

return MakeTreeUtil.getTestTreeXml();

}


7. Domo Page

Top

Spring quartz(Scheduler) 사용

Spring Bean을 Injection하여 사용자 접근 통계의 하루 누적치 통계를 수행하기 위한 Scheduler 사용 예제. 

1. Quartz 라이브러리 추가 (maven dependency 사용)
jar download url : http://mvnrepository.com/artifact/quartz/quartz/1.5.2

2. Service Class
Quartz Class에 Injection할 Service Bean.
public class StatServiceImpl implements StatService, ApplicationContextAware {
[...]
public void insertStat(String date) {
                //해당날짜의 log select.
StatBean stat = logDao.getLogStatByDate(date);
if (logger.isInfoEnabled()) {
logger.info(date + " Date Stat : " + stat.toString());
}
int checkStat = statDao.checkStat(date);
if (checkStat > 0) {
statDao.updateStat(stat);    //통계 update
if (logger.isInfoEnabled()) {
logger.info(date + " Date Stat Update");
}
} else {
statDao.insertStat(stat);    //통계 insert
if (logger.isInfoEnabled()) {
logger.info(date + " Date Stat Insert");
}
}
String accessLogDir = "/accessLog/";
String address = constantVo.getUrlContext() + "/admin/store/accessStat/dayStat?offsetDate=" + date;
String localFileName = context.getServletContext().getRealPath(constantVo.getDataUrl() + accessLogDir + date + ".html");

                //해당날짜의 접속통계 페이지를 HTML로 download
int downOk = downloadURL(address, localFileName);
if (downOk == 1) {
if (logger.isInfoEnabled()) {
logger.info(date + " Date Page Download");
}
logDao.deleteLog(date);    //해당날짜의 log delete
if (logger.isInfoEnabled()) {
logger.info(date + " Date Log Delete");
}
}
}
[...]
}

3. Quartz Class
@Inject, @Autowired로 Injection이 안되는 것 같음? 아래 Spring Bean 설정에서 Bean을 선언 하여 사용.
public class StatInsert extends QuartzJobBean {
private StatService statService; //could not autowired
public void setStatService(StatService statService) {
this.statService = statService;
}
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
[...]
statService.insertStat(date);
}
}

4. Spring Bean 설정
Spring Quartz Scheduler는 interval time 동작 방식과 cron tab 동작 방식이 있음.
접속 통계 누적 Scheduler는 cronExpression을 통한 매일 0시 5분에 실행 하도록 구현.
Injection할 Service Bean(들)은 jobDataAsMap의 entry로 등록하여 사용.
<!-- Service Bean -->
<bean id="statService" class="com.rurony.format.access.service.StatServiceImpl" />

<!-- Spring Quartz Scheduler Bean Setting -->
<!-- Simple Quartz Scheduler -->
<!--
<bean id="simpleQuartzJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.rurony.format.schedule.StatInsert"/>
<property name="jobDataAsMap">
<map>
<entry key="statService">
<ref local="statService"/>
</entry>
</map>
</property>
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="simpleQuartzJob"/>
<property name="startDelay" value="6000"/>
<property name="repeatInterval" value="6000"/>
</bean>
-->

<!-- Cron Quartz Scheduler -->
<!-- Scheduler Bean -->
<bean id="cronQuartzJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.rurony.format.schedule.StatInsert" />
<property name="jobDataAsMap">
<map>
<entry key="statService">
<ref local="statService"/>
</entry>
</map>
</property>
</bean>

<!-- Scheduler Trigger cronExpression setting -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="cronQuartzJob"/>
<!-- <property name="cronExpression" value="1 * * * * ?"/> -->
<!-- <property name="cronExpression" value="0 30 9 * * ?"/> -->
<property name="cronExpression" value="0 5 0 * * ?"/> <!-- 매일 0시 5분 0초 에 실행 -->
</bean>

<!-- Scheduler Trigger  -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- <ref bean="simpleTrigger"/> -->
<ref bean="cronTrigger"/>
</list>
</property>
<property name="quartzProperties">
<props>
<prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
    <prop key="org.quartz.threadPool.threadCount">5</prop>
    <prop key="org.quartz.threadPool.threadPriority">4</prop>
    <prop key="org.quartz.jobStore.class">org.quartz.simpl.RAMJobStore</prop>
    <prop key="org.quartz.jobStore.misfireThreshold">60000</prop>
</props>
</property>
</bean>



Top

Spring Bean을 Servlet Filter에서 사용

Spring Bean을 Injection하여 Servlet Filter를 활용한 사용자 접근 통계 예제.
 
1. Service Class
Filter Class에 Injection할 Service Bean.
public class LogServiceImpl implements LogService {
@Autowired
private LogDao logDao;
public void insertLog(LogBean log) {
logDao.insertLog(log);
}
[...]
}

2. Filter Class 

component-scan으로 자동 등록된 Bean은 Injection이 안되는 것 같음? 아래 Spring Bean 설정에서 Bean을 선언 하여 사용.
public class AccessLogFilter implements Filter {
@Autowired
private LogService logService;
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest sReq, ServletResponse sRes, FilterChain chain) throws IOException, ServletException {
[...]
chain.doFilter(sReq, sRes);
logService.insertLog(log);
}

public void destroy() {
}
}

3. Spring Bean 설정
<!-- Service Bean -->
<bean id="logService" class="com.rurony.format.access.service.LogServiceImpl" />
 <!-- Filter Bean -->
<bean id="accessLogFilter" class="com.rurony.format.filter.AccessLogFilter" />

4. web.xml 설정
스프링에서 제공하는 DelegatingFilterProxy를 사용하여 설정, 위 Spring Bean 설정에서 선언된 Filter Bean의 id와 filter-name을 같게 설정.
<filter>
<filter-name>accessLogFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
  
<filter-mapping>
<filter-name>accessLogFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Top

prev 1 next