81,111
社区成员
发帖
与我相关
我的任务
分享
import java.beans.PropertyVetoException;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
import com.xtb.util.PlatUtil;
import com.mchange.v2.c3p0.ComboPooledDataSource;
;public class ServiceFactory {
//这里是初始化数据源,并把它注册到jndi中
static{
try{
PlatUtil util = new PlatUtil();
ComboPooledDataSource dataSource=new ComboPooledDataSource();
dataSource.setUser("root");
dataSource.setPassword("000000");
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/chat?useUnicode=true&characterEncoding=gbk");
dataSource.setMaxPoolSize(10);
dataSource.setMinPoolSize(2);
dataSource.setAcquireIncrement(2);
Properties prop=new Properties();
prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.fscontext.RefFSContextFactory");
prop.setProperty(Context.PROVIDER_URL,util.getPlatPath("temp"));
Context myContext=new InitialContext(prop);
myContext.bind("dataSource",dataSource);
} catch(Exception e){
e.printStackTrace();
}
}
private static ServiceFactory factory = new ServiceFactory();
private ApplicationContext oAC = null;
private ServiceFactory() {
init();
}
public static ServiceFactory getInstance() {
return factory;
}
public Object getService(String name) {
return oAC.getBean(name);
}
public void init() {
PlatUtil util = new PlatUtil();
oAC = new FileSystemXmlApplicationContext(util
.getPlatPath("applicationContext.xml"));
}
public static void main(String[] args) {
UserService service = (UserService) ServiceFactory.getInstance()
.getService("userService");
if (service == null)
System.out.println("fail");
else
System.out.println("success");
}
}
这里是我的spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>dataSource</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/xtb/model/Users.hbm.xml</value>
<value>com/xtb/model/Message.hbm.xml</value>
<value>com/xtb/model/LoginRecord.hbm.xml</value>
</list>
</property>
</bean>
<bean id="dao" class="com.xtb.dao.BaseDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="baseService" abstract="true">
<constructor-arg>
<ref bean="dao" />
</constructor-arg>
</bean>
<bean id="userService" class="com.xtb.service.UserServiceImpl"
parent="baseService">
</bean>
<bean id="messageService" class="com.xtb.service.MessageServiceImpl"
parent="baseService">
</bean>
<bean id="loginRecordService"
class="com.xtb.service.LoginRecordServiceImpl" parent="baseService">
</bean>
</beans>