在我的博客http://www.easyjf.com/blog/woweiwokuang/index.html上面有很多内容都是对生成项目中的文件介绍,现在还是要写出对它的理解,我发现EasyJWeb生成的项目中存在着许多到的哲学,能够从里面学到太多实用的东西。
因为生成的项目是JPA+EasyJWeb+Spring的应用架构,所以牵涉到的东西很多很多。
其实用这个架构说简单也简单,就是对里面的配置理解就可以了,只要涉及到Spring的配置。下面我就把新生成的项目中的配置文件按理解一下,希望看到能够帮我看看,不对的改正一下。
首先来看看里面重要的配置文件application.xml的配置:
xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<import resource="classpath:/com/easyjf/core/open-spring-transaction.xml" />
<import resource="classpath:/com/easyjf/core/jpa-generic-dao.xml" />
<import resource="dao.xml" />
<import resource="service.xml" />

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.propertiesvalue>
list>
property>
bean>
beans>

通过import导入了open-spring-transaction.xml、 jpa-generic-dao.xml两个xml配置文件。因为这两个配置文件是封装EasyJWeb的源代码中的,会在编译的时候放到classpath中,所以就用了一个classpath来指定他们的路径。
可以直接导入该项目中的配置文件,如下
<import resource="dao.xml" />
<import resource="service.xml" />
而propertyConfigurer这个bean的配置就是指定一个属性的配置文件,
这个是Spring的框架中为提供了一个BeanFactoryPostProcessor的实体类别: org.springframework.beans.factory.config.PropertyPlaceholderConfigurer 可以将一些动态参数, 移出至.properties中,如此的安排可以让XML定义系统相关设定(不常变动),而.properties可以作为客户根据实际自定义一些相关参数。里面的配置信息需要通过${}符号来指定了。比如说这个:db.properties文件中有这样的信息:
database.driverClassName = org.gjt.mm.mysql.Driver
database.password =
database.show_sql = true
database.url = jdbc:mysql://localhost:3306/ejsdb?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf8&autoReconnect=true
database.username = root

这些配置信息可以在jpa-generic-dao.xml中找到,如:
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="${database.driverClassName}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
bean>

这个bean中就配置了数据源的相关信息,可以看到${}中的参数刚好和db.properties文件中的参数相对应,也就是db.properties文件中的信息传递给了该bean中。
然后让我们看entityManagerFactory的bean的配置:
<bean name="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation"
value="classpath:persistence.xml" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
//指定的数据库为mysql
<property name="showSql" value="false" />
//在测试的时候是否显示数据库的SQL
<property name="generateDdl" value="false" />
//是否自动产生DDL
bean>
property>
bean>

首先我们来看entityManagerFactory这个bean需要一个persistenceXmlLocation属性来指定JPA的配置文件,接下来要植入一个bean就是datasource。还是一个JpaVendorAdapter指定JPA不同的实现。如果上面的两个属性showSql、generateDdl都设为true的话,database schema将会在系统执行时自动产生。
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
bean>

transactionManager指定了Spring框架对JPA事务的支持,需要植入一个entityManagerFactory,dataSource
<bean abstract="true" id="baseDAO"
class="com.easyjf.core.dao.impl.GenericDAOImpl">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
bean>

泛型DAO的基类,需要entityManagerFactory的注入。
<bean id="abstractDao" abstract="true"
class="org.springframework.aop.framework.ProxyFactoryBean">
bean>

使用Spring的代理bean,通过它可以创建不同类型的对象。
<bean id="i18nInteceptor"
class="com.easyjf.core.i18n.I18nInteceptor" />
<bean id="abstractI18nDao" abstract="true"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interceptorNames">
<list>
<value>i18nInteceptorvalue>
list>
property>
bean>

这两个bean主要用于EasyJWeb对国际化的支持,这里就不多说了!
<bean id="jpaPoLoader"
class="com.easyjf.core.dao.impl.JpaPOLoaderImpl">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
bean>

这个bean主要是支持EasyJWeb中Poloader标签的实现。
再来看看open-spring-transaction.xml里面的配置吧!
xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<aop:config>
<aop:pointcut id="easyjwebProcessor"
expression="execution(* com.easyjf.web.RequestProcessor.process(..))" />
//指定切入定。
<aop:advisor advice-ref="txEasyjwebProcessorAdvice"
pointcut-ref="easyjwebProcessor" />
//关联了切入点也通知。
aop:config>

<tx:advice id="txEasyjwebProcessorAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
tx:attributes>
tx:advice>
//定义了通知,在进入切入点的时候事务是一定存在的。
<bean name="EasyJWeb-Processor" class="com.easyjf.web.core.DefaultRequestProcessor"/>
beans>

先写这么多吧!下一篇再接着写。