如何在应用程序中调用service

本文介绍如何在没有Struts插件的应用程序中获取到service的实例,通过配置文件实现dao和服务的绑定,并提供工具类方便获取实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在web程序中,由于有struts2提供的插件,我们能够在action中通过get,set方法获取到service的实例,但是在应用程序中,没有struts的插件,而我们又需要用到事务的时候,如何获取到service的实例,为了避免遗忘,特写下此篇博客。

1.配置文件中配置了相应的dao,service

<!-- 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

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的实例

HoldInfoService holdinfoservice =(HoldInfoService) SpringFactoryBean.getBean("holdInfoService");
holdinfoservice.del(strpara);

 

转载于:https://my.oschina.net/gao0516/blog/66849

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值