? ? ?四年前,从spring mvc框架转移到play framework框架完全是为了更快,更好的去创业,那时候由于spring mvc配置的臃肿,以及不堪忍受tomcat研发时的加载,一度想去转向php开发,但很难舍弃对java的热爱,无意中发现了play framework,让我眼前一亮,随即花了一两天时间对它进行深入的研究,发现它就是我理想中想要的java框架,即满足我的灵活配置,同时可以抛弃tomcat以及又可以部署发布在多平台与tomcat里。一直这样坚持下来用了四年,也一直在用1.2.x版本,也觉得这个版本最稳定,2.0之上的版本完全已经不是java风格了。
?
? ? 我喜欢把MVC体系架构分成三个系统来设计,M我会用数据系统来代替,V我会用业务系统来代替,而C我喜欢我接口系统来替代,虽然这样开发会加大工作量,其实对于熟练的人来说,这工作量可以忽略不计,但是这有一个很大的好处在于未来平台的扩展以及业务的清晰与了解,开发的纯粹与质量。
? ??
? ? 首先搭建一个play框架项目,结合mybatis与spring,手动加载spring1.0.3在本地的moduels里。
? ? 在dependencies.yml文件里配置
- play
- org.mybatis -> mybatis 3.1.1
- org.mybatis -> mybatis-spring 1.1.1
- com.jolbox -> bonecp 0.7.1.RELEASE
- play -> spring 1.0.3
?
? ??
? ? ?在application.conf文件末尾加载spring配置
? ? ?
# The spring module
module.spring=${play.path}/modules/spring-1.0.3
play.spring.component-scan=true
play.spring.component-scan.base-packages=models
play.spring.add-play-properties=false
?
? ? 数据库连接设置
? ?
public class BoneCPDataSourceFactoryBean implements FactoryBean<BoneCPDataSource>, InitializingBean{
private BoneCPDataSource boneCPDataSource;
public final static int DEFAULT_POOL_MAX_SIZE = 30;
public final static int DEFAULT_POOL_MIN_SIZE = 10;
@Override
public BoneCPDataSource getObject() throws Exception {
return boneCPDataSource;
}
@Override
public Class<?> getObjectType() {
return BoneCPDataSource.class;
}
@Override
public boolean isSingleton() {
return true;
}
@Override
public void afterPropertiesSet() throws Exception {
boneCPDataSource = new BoneCPDataSource();
boneCPDataSource.setJdbcUrl(Play.configuration.getProperty("db.url"));
boneCPDataSource.setUsername(Play.configuration.getProperty("db.user"));
boneCPDataSource.setPassword(Play.configuration.getProperty("db.pass"));
boneCPDataSource.setDriverClass(Play.configuration.getProperty("db.driver"));
boneCPDataSource.setMaxConnectionsPerPartition(getIntValue("db.pool.maxSize", DEFAULT_POOL_MAX_SIZE));
boneCPDataSource.setMinConnectionsPerPartition(getIntValue("db.pool.minSize", DEFAULT_POOL_MIN_SIZE));
}
public int getIntValue(String config, int defalutValue){
String value = Play.configuration.getProperty(config );
if(!StringUtils.isEmpty(value)){
try{
defalutValue = Integer.parseInt(value);
}catch (Exception e) {
}
}
return defalutValue;
}
}
?
?
? ? 内部数据系统与业务系统之间通信采用rmi,所以在application.conf配置
? ? ?
#rmi服务的端口号设置。
bsd.rmi.server.host=localhost
bsd.rmi.server.port=1100
?
?
? ? 创建rmi服务类
public class BsdRmiServiceExporter extends RmiServiceExporter {
@Override
public void afterPropertiesSet() throws RemoteException {
System.setProperty("java.rmi.server.hostname ", Play.configuration.getProperty("bsd.rmi.server.host"));
super.afterPropertiesSet();
}
}
?
? ? 创建application-context.xml文件配置rmi与mybatis
? ??
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:application.conf</value>
</property>
</bean>
<bean id="dataSource" class="common.jdbc.datasource.BoneCPDataSourceFactoryBean" />
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
<!-- Transaction manager for a single JDBC DataSource -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/configuration.xml"></property>
<property name="mapperLocations" value="classpath:models/model/mapper/*.xml" />
</bean>
<bean id="bsdRmiServiceExporter" class="common.rmi.BsdRmiServiceExporter">
<property name="serviceName" value="bsdRmiService" />
<property name="service" ref="bsdRmiService" />
<property name="serviceInterface" value="models.rmi.BsdRmiService" />
<property name="registryPort" value="${bsd.rmi.server.port}" />
</bean>
?
? ? 创建业务工厂类
? ??
public class RmiService {
public static BsdRmiService getBsdRmiService()
{
return (BsdRmiService)play.modules.spring.Spring.getBean("bsdRmiService");
}
}
?
? ? 在application.conf里配置rmi接收端口
? ? ?rmi.base.url=rmi://localhost:1100
?
? ? 配置rmi文件application-context.xm
? ?
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:application.conf</value>
</property>
</bean>
<bean id="bsdRmiService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="${.rmi.base.url}/bsdRmiService" />
<property name="serviceInterface" value="models.rmi.BsdRmiService" />
<property name="lookupStubOnStartup" value="false" />
<property name="refreshStubOnConnectFailure" value="true" />
</bean>
?
? ? play framework是一个非常好做接口的框架,特别是restful以及http等,仅仅只需要renderJSON 就可以发布一个http接口出去。
? ? 发布GET或者POST接口,只需要在routes文件里配置即可,这里不再详细描述
?
后面我会抽时间写一些工作阶段遇到的问题及解决办法,写这些也是记录这么多年的工作经验,让笔记留住它。
?
?
Play Framework系列之一如何搭建一个play 框架的spring体系
原文:http://tsinghua2008.iteye.com/blog/2301245