⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 localservicelocator.java

📁 《j2ee开发全程实录》随书源码
💻 JAVA
字号:
package com.cownew.PIS.framework.server.helper;

import java.util.Properties;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.interceptor.TransactionProxyFactoryBean;

import com.cownew.PIS.framework.common.ITxService;

import com.cownew.PIS.framework.server.TxMgr.TxManagerFactory;
import com.cownew.PIS.framework.server.logMgr.ILogableService;
import com.cownew.PIS.framework.server.logMgr.LogableBeanFactory;
import com.cownew.PIS.framework.server.permissionMgr.IPermissionCtrlService;
import com.cownew.PIS.framework.server.permissionMgr.PermCtrlBeanFactory;

public class LocalServiceLocator
{

	private static LocalServiceLocator instance;

	private LocalServiceLocator()
	{
		super();
	};

	private static BeanFactory appContext = null;

	public static LocalServiceLocator getInstance()
	{
		if (instance == null)
		{
			instance = new LocalServiceLocator();

		}

		return instance;
	}

	public Object getService(Class serviceIntfClass)
	{

		String serviceId = serviceIntfClass.getName();
		Object bean = appContext.getBean(serviceId);

		//		添加日志切面,事务操作必须嵌套在所有数据库相关操作的外面
		if (ILogableService.class.isAssignableFrom(bean.getClass()))
		{
			ILogableService logBean = (ILogableService) bean;
			Properties logAttributes = logBean.getLogAttributes();
			if (logAttributes != null && logAttributes.size() > 0)
			{
				bean = LogableBeanFactory.getBean(logBean);
			}
		}

		// 添加权限切面,事务操作必须嵌套在所有数据库相关操作的外面
		if (IPermissionCtrlService.class.isAssignableFrom(bean.getClass()))
		{
			IPermissionCtrlService permBean = (IPermissionCtrlService) bean;
			Properties permAttributes = permBean.getPermissionAttributes();
			if (permAttributes != null && permAttributes.size() > 0)
			{
				bean = PermCtrlBeanFactory.getBean(permBean);
			}
		}

		// 因为事务操作必须嵌套在所有数据库相关操作的外面,所以必须把事务AOP包裹在最外边
		// 所以日志 权限等AOP要放在事务AOP之前,否则在日志 权限操作数据库的时候就会发生
		// Connection is closed的问题
		if (ITxService.class.isAssignableFrom(bean.getClass()))
		{
			ITxService txService = ((ITxService) bean);
			Properties txAttributes = txService.getTransactionAttributes();
			if (txAttributes != null && txAttributes.size() > 0)
			{
				TransactionProxyFactoryBean txBean = new TransactionProxyFactoryBean();
				txBean.setTarget(bean);
				txBean.setTransactionManager(TxManagerFactory.buildTxManager());
				txBean.setTransactionAttributes(txAttributes);
				txBean.afterPropertiesSet();
				bean = txBean.getObject();
			}
		}

		return bean;
	}

	static
	{
		appContext = new ClassPathXmlApplicationContext(ServerConfig
				.getInstance().getBeanFiles());
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -