java架构搭建(一)--工程搭建
当今程序员做开发几乎都是从半空学起,什么意思呢?
如今做开发的公司都会有自己的框架或者叫平台,无论叫什么吧,归根到底就是架构。
架构师把整个项目可以封装的功能进行封装,从而使程序员在开发的时候少做一些繁琐的工作,
从而达到快速,高校,准确的目的。也就是我们经常说“只需要配置什么什么就可以”。
这里配置就是架构师的目的,你不必再去关系实现的细节,不必再去看繁多的源码,只需要配置一些东西就可以实现效果。
话分两头。。。。。。。
一:架构师
成功的架构师能从整个工程规划,把工程的架构做的健壮,高效,开发简单。(包括对插件等的封装,通常需要js架构师,美工,ui合作完成)。
二:程序员
程序员在此基础上开发变得简单,只需关心业务逻辑。整个开发流程在简单的配置和几段业务代码就ok了。
对一个资深的程序员,基本上不需要架构师的培训就知道怎么使用架构。只要自己跟踪源码,大体浏览一遍源码就知道怎么用了,同时还能
学习到架构师的设计思维。而初级程序员或者说那些刚毕业,刚开始接触开发的人可要小心了,这些架构就像烟雾弹让你开不到
实现的细节,而基础的东西你有没有研究过,久而久之,你只能在这样的架构或者说平台进行开发。越是把架构做大做牛的公司对
初学者越是不利。我见过做了1年多开发的人对struts2的基本原理都不清楚,说白了只知道怎么配置,不知道怎么实现的。
闲话到此,现在开始搭建一个工程,我打算使用ssh。
首先,新建一个web工程,完成后习惯性的我们要建一个源文件夹“config”用于放置配置文件config.properties
在webapp目录下,建一个jsp文件夹用于放置页面。
同时再建立script和resource目录用于放置js和css以及web插件。(jquery是必须有的,这个别忘了)
ssh的jar包我就不说了,网上说的太多了。
其次,ssh的整合我们要关心4个文件:configure.properties,applicationContext.xml,struts.xml和web.xml
先来看看整体的工程结构吧,注意文件的路径:
详细配置文件:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 字符编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- struts2过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- spring监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- session超时设置,单位分钟 -->
<session-config>
<session-timeout>360</session-timeout>
</session-config>
</web-app>
configure.properties
# applicationContext.xml
### C3P0 Connection Pool
c3p0.maxPoolSize=3
c3p0.minPoolSize=1
c3p0.maxIdleTime=1800
c3p0.maxStatements=0
c3p0.acquireIncrement=2
c3p0.idleConnectionTestPeriod=600
#oracle
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@10.55.15.66:1521:cdbank
jdbc.username=ccdb
jdbc.password=ccdb
hibernate.dialect=org.hibernate.dialect.Oracle9Dialect
applicationContext.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-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:configure.properties</value>
</list>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.query.substitutions">true=1,false=0</prop>
<prop key="hibernate.jdbc.batch_size">25</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.region_prefix">direct</prop>
<prop key="hibernate.cache.use_structured_entries">false</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
</props>
</property>
<property name="dataSource" ref="dataSource"/>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/org/first/config/</value>
<value>classpath:/org/second/config/</value>
</list>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
<property name="minPoolSize" value="${c3p0.minPoolSize}"/>
<property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>
<property name="maxStatements" value="${c3p0.maxStatements}"/>
<property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>
<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"/>
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="transactionProxyTemplate" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="query*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 功能模块的引入 -->
<import resource="classpath:/org/first/config/context_first.xml"/>
<import resource="classpath:/org/second/config/context_second.xml"/>
</beans>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="base-struts-default" extends="struts-default">
<global-results>
<result name="error" type="dispatcher">/jsp/error.jsp</result>
</global-results>
<action name="index" class="org.first.action.FirstIndexAction">
<result name="success">/index.jsp</result>
</action>
</package>
<!-- 开发测试模块 -->
<include file="org/first/config/struts_first.xml"></include>
<include file="org/second/config/struts_second.xml"></include>
</struts>
到目前为止,工程已经搭建完毕,可以正常启动。
那么spring,struts2,hibernate到底能不能正常使用,我们还需做个功能来测试。
并且底层的api需要简单封装以便于开发。这里内容有点多了,我新启一篇,放到下片讲解。
还没有评论,来说两句吧...