如何在應用程序中調用service

在web程序中,因爲有struts2提供的插件,咱們可以在action中經過get,set方法獲取到service的實例,可是在應用程序中,沒有struts的插件,而咱們又須要用到事務的時候,如何獲取到service的實例,爲了不遺忘,特寫下此篇博客。java

1.配置文件中配置了相應的dao,serviceweb

<!-- dao -->
 	<bean id="holdInfodao" class="com.sseinfo.marginvote.bg.holdinfo.deletehold.dao.HoldInfoDAOImpl">
 		<property name="sessionFactory" ref="sessionFactory" />
 	</bean>


	<!-- service -->	
	<bean id="holdInfoService" class="com.sseinfo.marginvote.bg.holdinfo.deletehold.service.HoldInfoServiceImpl">
		<property name="holdInfodao" ref="holdInfodao"/>
	</bean>

2.工具類用來從配置文件中獲取到service
spring

package com.sseinfo.marginvote.bg.holdinfo.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringFactoryBean {
private static final ApplicationContext CTX;
	
	static {
		String[] paths = {"classpath:applicationContext-beans.xml"};
		CTX = new ClassPathXmlApplicationContext(paths);
	}
	
	public static Object getBean(String beanName)
	{
		return CTX.getBean(beanName);
	}
}

3.在類中經過工具類獲取到service的實例session

HoldInfoService holdinfoservice =(HoldInfoService) SpringFactoryBean.getBean("holdInfoService");
holdinfoservice.del(strpara);
相關文章
相關標籤/搜索