- 浏览: 55009 次
- 性别:
- 来自: 上海
最新评论
-
taowayi:
keytool错误:java.io.IOException:k ...
Tomcat https(SSL) 简单的证书配置 -
usherlight:
Java的SimpleDateFormat可以设置TimeZo ...
Date with timezone convert
guice -- dependent injection
warp -- dynamic finder
AbsEntity ---- 抽象类 提炼所有域对象的Generic属性和行为
IEntity ---- 对象行为的Generic接口
觉得Annotation用的舒服啊. MainModule.java替代Spring的applicationContext.xml配置文件
这个Module就像applicationContext.xml的功能
Persist_Unit.xml 数据库的配置
warp -- dynamic finder
AbsEntity ---- 抽象类 提炼所有域对象的Generic属性和行为
IEntity ---- 对象行为的Generic接口
觉得Annotation用的舒服啊. MainModule.java替代Spring的applicationContext.xml配置文件
import java.io.Serializable; import java.util.Date; import java.util.List; import javax.persistence.EntityManager; import com.wideplay.warp.persist.Transactional; /** * <p> * Entity interface implemented by all persistent classes. */ public interface IEntity<T, PK extends Serializable> { public void setEmp(com.google.inject.Provider<EntityManager> emp); public String getId(); public Date getCreatedAt(); public void setCreatedAt(Date createAt); public Date getChangedAt(); public void setChangedAt(Date changedAt); public List<String> validate(); public T find(PK id); public List<T> findAll(); @Transactional public T save(T object); @Transactional public void remove(PK id); }
import java.io.Serializable; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.persistence.Column; import javax.persistence.EntityManager; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.MappedSuperclass; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.Transient; import com.google.inject.Inject; import com.wideplay.warp.persist.Transactional; /** * * <p> * This class provides the basic properties for entities (id, createdAt, and * changedAt) as well as default save, remove, find, and findAll methods. * * <p> * Custom finders and custom save / remove logic can be implemented in child * entity classes as necessary. */ @MappedSuperclass public abstract class AbsEntity<T, PK extends Serializable> implements IEntity<T, PK> { @Transient @Inject protected com.google.inject.Provider<EntityManager> emp; @Transient private Class<T> persistentClass; @Id @Column(length = 36, nullable = false) //@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(generator = "hibernate-uuid.hex") @org.hibernate.annotations.GenericGenerator(name = "hibernate-uuid.hex", strategy = "uuid.hex") protected String id; @Temporal(TemporalType.TIMESTAMP) @Column(name = "created_at", nullable = false) protected Date createdAt = new Date(); @Temporal(TemporalType.TIMESTAMP) @Column(name = "changed_at", nullable = false) protected Date changedAt = new Date(); /** * Constructor for dependency injection. * * @param persistentClass * the class type you'd like to persist. */ public AbsEntity(Class<T> persistentClass) { this.persistentClass = persistentClass; } public void setEmp(com.google.inject.Provider<EntityManager> emp) { this.emp = emp; } public String getId() { return id; } public Date getCreatedAt() { return createdAt; } public void setCreatedAt(Date createdAt) { this.createdAt = createdAt; } public Date getChangedAt() { return changedAt; } public void setChangedAt(Date changedAt) { this.changedAt = changedAt; } public List<String> validate() { // create our list for errors List<String> errors = new ArrayList<String>(); // Validate the model fields. if (this.id == null || this.id.length() == 0) { errors.add("Identifier is null or empty."); } if (this.createdAt == null) { errors.add("Created at date is null."); } // if no errors occured we'll return null. if (errors.size() == 0) { errors = null; } // return errors that occured return errors; } public T find(PK id) { return emp.get().find(this.persistentClass, id); } @SuppressWarnings("unchecked") public List<T> findAll() { return emp.get().createQuery( "FROM " + this.persistentClass.getSimpleName()).getResultList(); } @Transactional public T save(T object) { return emp.get().merge(object); } @Transactional public void remove(PK id) { EntityManager em = emp.get(); em.remove(em.find(this.persistentClass, id)); } @SuppressWarnings("unchecked") @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || !(o instanceof IEntity)) { return false; } IEntity other = (IEntity) o; // if the id is missing, return false if (id == null) { return false; } // equivalence by id return id.equals(other.getId()); } @Override public int hashCode() { if (id != null) { return id.hashCode(); } else { return super.hashCode(); } } @Override public String toString() { // TODO Auto-generated method stub return "id: " + id + ", createdAt: " + createdAt.toString() + ", changedAt: " + changedAt.toString(); } }
import java.util.List; import javax.persistence.Column; import javax.persistence.DiscriminatorColumn; import javax.persistence.DiscriminatorType; import javax.persistence.Entity; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import com.google.inject.name.Named; import com.model.Role; import com.wideplay.warp.persist.dao.Finder; @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "USER_TYPE", discriminatorType = DiscriminatorType.STRING) @Table(name = "USER") public abstract class BaseUser extends AbsEntity<BaseUser, String> implements IEntity<BaseUser, String> { @Column(name = "EMAIL") private String email; @Column(name = "PASSWORD", length = 50) private String password; @Column(name = "FULL_NAME", length = 50) private String fullName; @Column(name = "ACTIVE") private boolean active = true; @Column(name = "LOGIN_RANDOM_PASSWORD") private String loginRandomPassword; @ManyToOne(targetEntity = Role.class) @JoinColumn(name = "FK_ROLE_ID") private Role role; /** * Necessary to use the generic operation methods in AbsEntity. */ @SuppressWarnings("unchecked") public BaseUser() { super(BaseUser.class); } /** ********************************************** */ /** Properties *********************************** */ /** ********************************************** */ public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getFullName() { return fullName; } public void setFullName(String fullName) { this.fullName = fullName; } public boolean isActive() { return active; } public void setActive(boolean active) { this.active = active; } public String getLoginRandomPassword() { return loginRandomPassword; } public void setLoginRandomPassword(String loginRandomPassword) { this.loginRandomPassword = loginRandomPassword; } public Role getRole() { return role; } public void setRole(Role role) { this.role = role; } /** ********************************************** */ /** Operators ************************************ */ /** ********************************************** */ // Override save, remove, find, and findAll methods as necessary. // /** ********************************************** */ /** Custom Finders ******************************* */ /** ********************************************** */ @Finder(query = "FROM Role WHERE id = :roleId") public Role findRoleById(@Named("roleId")Long roleId) { return null; // never called } @Finder(query = "From Role order by roleName") public List<Role> findAllRoles() { return null; } }
import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; import com.model.abs.BaseUser; @Entity() @DiscriminatorValue("C_USER") public class CustomUser extends BaseUser { /** ********************************************** */ /** Custom Finders ******************************* */ /** ********************************************** */ }
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.google.inject.Inject; import com.wideplay.warp.persist.PersistenceService; public class InitializerJpa { private final Log log = LogFactory.getLog(getClass()); @SuppressWarnings("unused") private final PersistenceService service; /** * Starters the JPA persistence service. * * @param service * the persistence service to start. */ @Inject InitializerJpa(PersistenceService service) { this.service = service; service.start(); log.info("JPA Persistence Service started..."); } }
这个Module就像applicationContext.xml的功能
import com.app.InitializerJpa; import com.google.inject.AbstractModule; import com.wideplay.warp.jpa.JpaUnit; public class MainModule extends AbstractModule { protected void configure() { bindConstant().annotatedWith(JpaUnit.class).to("Persist_Unit"); // to automatically start up the persistence service bind(InitializerJpa.class).asEagerSingleton(); } }
Persist_Unit.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"> <!-- A JPA Persistence Unit --> <persistence-unit name="Persist_Unit" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <!-- JPA entities can be registered here, but it's not necessary --> <properties> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.url" value="jdbc:mysql://localhost/ex_ddd"/> <property name="hibernate.connection.username" value="root"/> <property name="hibernate.connection.password" value="123"/> <property name="hibernate.connection.pool_size" value="1"/> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> <property name="hibernate.hbm2ddl.auto" value="create"/> </properties> </persistence-unit> </persistence>
相关推荐
A sample Java web application that demonstrates how <br>Deployment : * Install Dekoh desktop * Start dekoh in interactive mode by executing dekoh_shell.bat / dekoh_shell.sh in directory C:...
guice.jar guice.jar guice.jar guice.jar guice.jar guice.jar guice.jar
借助Guice+Struts2+Warp-persist来构建一个比较轻盈的web开发框架,目的是想抛砖引玉。随后还会将Scala部分整合进来,目的就是唯恐框架不烂!(*^__^*)。 对于代码的不妥之处欢迎各路高手斧正。 mail to : meconsea@...
### Guice用户中文指南 #### 一、简介 Guice是一个专门为Java 5及后续版本设计的超轻量级依赖注入框架。它旨在简化应用程序组件之间的依赖管理,并提供了一个更为简洁、灵活的方式来处理对象间的耦合关系。Guice的...
Guice-3.0是Guice的一个版本,包含了核心库guice-3.0.jar,以及与Spring和Struts2集成的扩展库guice-spring-3.0.jar和guice-struts2-plugin-3.0.jar。 1. **Guice核心概念**: - **依赖注入**:Guice的核心机制,...
**Guice 概述** Guice 是 Google 推出的一款轻量级的依赖注入框架,专为 Java 5 及其后续版本设计。依赖注入(Dependency Injection,简称 DI)是一种设计模式,它允许开发者在不直接创建对象的情况下,将依赖关系...
**基于Guice的简单项目** 在Java开发中,依赖注入(Dependency Injection,简称DI)是一种设计模式,它有助于降低代码间的耦合度,提高代码的可测试性和可维护性。Guice是Google提供的一款轻量级的DI框架,它简化了...
Google Guice,全称为GoogleInject,是一个轻量级的依赖注入框架,由Google开发并开源。Guice的目标是简化Java应用程序的构造和管理,通过自动装配对象依赖关系,让开发者可以专注于业务逻辑而不是对象的创建和组装...
赠送jar包:guice-4.0.jar; 赠送原API文档:guice-4.0-javadoc.jar; 赠送源代码:guice-4.0-sources.jar; 赠送Maven依赖信息文件:guice-4.0.pom; 包含翻译后的API文档:guice-4.0-javadoc-API文档-中文(简体)版...
### Apache Shiro 与 Guice 集成详解 #### 概述 在现代软件开发过程中,集成不同的框架和技术是常见的需求。Apache Shiro 是一个强大的、易用的 Java 安全框架,提供了认证、授权、加密和会话管理功能。而 Google ...
### Google Guice: 敏捷轻量级依赖注入框架详解 #### 一、引言与背景 在现代软件开发中,依赖注入(Dependency Injection, DI)已成为构建灵活、可维护和可测试应用程序的重要手段之一。Google Guice作为一款100%...
Guice,全称为Google Guice,是一款由Google开发的轻量级依赖注入(Dependency Injection,简称DI)框架,主要用于简化Java应用的初始化和组件管理。依赖注入是一种设计模式,它可以帮助开发者解耦代码,提高软件的...
"google Guice 1.0 用户指南 中文" Guice 是一个超轻量级的、下一代的、为 Java 5 及后续版本设计的依赖注入容器。它可以帮助 Java 企业应用开发社区解决连接对象方面的问题,例如 Web 应用如何访问中间层服务、...
【标题】: Guice框架初探与测试实践 在Java世界中,依赖注入(Dependency Injection,简称DI)是一种常见的设计模式,它可以帮助我们构建松耦合、可测试的代码。Guice是Google提供的一款轻量级的DI框架,用于简化...
guice 学习资料,快速掌握guice的编程技巧以及了解其机制。
Google Guice是一个轻量级的依赖注入框架,由Google开发并维护,主要用于简化Java应用程序的构建和管理。依赖注入(Dependency Injection,简称DI)是一种设计模式,它可以帮助开发者减少代码间的耦合,提高代码的可...
**初试Guice** Guice,全称Google Guice,是一款轻量级的依赖注入(Dependency Injection,简称DI)框架,由Google开发并开源。它主要用于简化Java应用程序的构造和管理,通过DI来解耦代码,使代码更易于测试、维护...
**标题:“Guice用户指南翻译”** **描述:** Guice是Google开发的一款轻量级依赖注入(DI)框架,它使得Java应用的组件管理变得简单。依赖注入是一种设计模式,它有助于降低代码间的耦合度,提高软件的可测试性...