Guice在Ioc上火了一把,但是对于持久层部分支持一直是他的短板,很多人对它没有持久层部分的支持进行批判。Guice的初衷就是一个Ioc。但是Warp-persist的出现弥补了这个短板。用Guice并不说Spring的Ioc部分不好,用Warp也不是说Spring已经过时了,毕竟SSH的学习曲线和运作成本目前来说还是比较低的,主要是廉价劳动力多啊,大街上一伸脚绊倒10个程序员,有8个是在用SSH,还有1个是在学SSH。o(∩_∩)o..。对于项目来说 没有最好的框架,更没有最先进的框架,只有最适合的框架。
借助Guice+Struts2+Warp-persist来构建一个比较轻盈的web开发框架,目的是想抛砖引玉。随后还会将Scala部分整合进来,目的就是唯恐框架不烂!(*^__^*)。
对于Guice、warp-persist的部分我就不介绍了,因为我介绍不了,Struts2更不用介绍了,大伙都知道。
能够完成Guice+warp-persist+struts 非常感谢warp-core google group 中的 Dhanji R. Prasanna 和 Robbie Vanbrabant !
在网上找了N久,没有找到一个Guice+Struts+warp-persist能拿的起放的下的例子! 所以借助于上面两位大师的指点,翻山越岭、跋山涉水完成了该例子。希望得到更多高手的指导!
例子中仅对Warp-persist中的Jpa支持部分做了简单的实例,分别采用了Dynamic Finders 和Named Queries 两种方式的实现。
随后会完善出Domain Driven Design的实现。
由于对guice和warp的了解有限,里面存在很多不妥之处,希望得到各路高手的斧正!
注: 附件中没有放入lib包。搭建环境的时候直接从guice和warp项目中拷入即可。
下载地址: http://download.youkuaiyun.com/source/759283 不好意思啊 有3分的资源分。
没有资源分的,发mail :meconsea@hotmail.com 偶给你发过去!
在此先放部分代码:
persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="wgsTest" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>meconsea.gs.test.model.SUser</class>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/warp?useUnicode=true&characterEncoding=UTF-8"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="315315"/>
<property name="hibernate.connection.pool_size" value="10"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLMyISAMDialect"/>
<property name="hibernate.connection.autocommit" value="false"/>
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
GsModule.java
/**
* @author meconsea
* MSN: meconsea@hotmail.com
* Mail: wangzhanhai@139.com
*
*/
public class GsModule extends AbstractModule {
private final static Logger log = Logger.getLogger(GsModule.class);
@Override
protected void configure() {
log.info("GsModule configure .....");
bindConstant().annotatedWith(JpaUnit.class).to("wgsTest");
install(PersistenceService.usingJpa().across(UnitOfWork.REQUEST)
.transactedWith(TransactionStrategy.LOCAL)
.forAll(Matchers.any())
.buildModule());
log.info("GsModule bind ..... ");
// bind(InitGs.class).asEagerSingleton();
}
}
SUserAccessorImpl.java
/**
*
* @author meconsea
* MSN: meconsea@hotmail.com
* Mail: wangzhanhai@139.com
*/
public class SUserAccessorImpl implements SUserAccessor {
@Finder(namedQuery = SUser.LIST_BYNAME)
public SUser findByName(@Named("name") String name) {
return null;
}
}
SUserAccessor.java
/**
*
* @author meconsea
* MSN: meconsea@hotmail.com
* Mail: wangzhanhai@139.com
*/
@ImplementedBy(SUserAccessorImpl.class)
public interface SUserAccessor {
public SUser findByName(String name);
}
SUserDao.java
/**
* @author meconsea
* MSN: meconsea@hotmail.com
* Mail: wangzhanhai@139.com
*
*/
public class SUserDao extends BaseDao<SUser, String> implements
IDao<SUser, String> {
public SUserDao() {
super(SUser.class);
}
@SuppressWarnings("unchecked")
@Finder(namedQuery = SUser.LIST_BYNAME)
public SUser findByName(@Named("name") String name) {
return (SUser)emp.get().createQuery(SUser.LIST_BYNAME).setParameter("name",
name).getSingleResult();
}
}
另外: 欢迎留言斧正!