java架构搭建(一)--工程搭建

痛定思痛。 2022-03-30 11:40 466阅读 0赞

当今程序员做开发几乎都是从半空学起,什么意思呢?

如今做开发的公司都会有自己的框架或者叫平台,无论叫什么吧,归根到底就是架构。

架构师把整个项目可以封装的功能进行封装,从而使程序员在开发的时候少做一些繁琐的工作,

从而达到快速,高校,准确的目的。也就是我们经常说“只需要配置什么什么就可以”。

这里配置就是架构师的目的,你不必再去关系实现的细节,不必再去看繁多的源码,只需要配置一些东西就可以实现效果。

话分两头。。。。。。。

一:架构师

成功的架构师能从整个工程规划,把工程的架构做的健壮,高效,开发简单。(包括对插件等的封装,通常需要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

先来看看整体的工程结构吧,注意文件的路径:

1361176467_9706.jpg

详细配置文件:

web.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.4"
  3. xmlns="http://java.sun.com/xml/ns/j2ee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  6. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  7. <!-- 字符编码过滤器 -->
  8. <filter>
  9. <filter-name>encodingFilter</filter-name>
  10. <filter-class>
  11. org.springframework.web.filter.CharacterEncodingFilter
  12. </filter-class>
  13. <init-param>
  14. <param-name>encoding</param-name>
  15. <param-value>UTF-8</param-value>
  16. </init-param>
  17. </filter>
  18. <filter-mapping>
  19. <filter-name>encodingFilter</filter-name>
  20. <url-pattern>/*</url-pattern>
  21. </filter-mapping>
  22. <!-- struts2过滤器 -->
  23. <filter>
  24. <filter-name>struts2</filter-name>
  25. <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  26. </filter>
  27. <filter-mapping>
  28. <filter-name>struts2</filter-name>
  29. <url-pattern>*.action</url-pattern>
  30. </filter-mapping>
  31. <!-- spring监听 -->
  32. <listener>
  33. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  34. </listener>
  35. <!-- session超时设置,单位分钟 -->
  36. <session-config>
  37. <session-timeout>360</session-timeout>
  38. </session-config>
  39. </web-app>

configure.properties

  1. # applicationContext.xml
  2. ### C3P0 Connection Pool
  3. c3p0.maxPoolSize=3
  4. c3p0.minPoolSize=1
  5. c3p0.maxIdleTime=1800
  6. c3p0.maxStatements=0
  7. c3p0.acquireIncrement=2
  8. c3p0.idleConnectionTestPeriod=600
  9. #oracle
  10. jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
  11. jdbc.url=jdbc:oracle:thin:@10.55.15.66:1521:cdbank
  12. jdbc.username=ccdb
  13. jdbc.password=ccdb
  14. hibernate.dialect=org.hibernate.dialect.Oracle9Dialect

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
  9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  11. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  12. <property name="locations">
  13. <list>
  14. <value>classpath:configure.properties</value>
  15. </list>
  16. </property>
  17. </bean>
  18. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  19. <property name="hibernateProperties">
  20. <props>
  21. <prop key="hibernate.dialect">${hibernate.dialect}</prop>
  22. <prop key="hibernate.query.substitutions">true=1,false=0</prop>
  23. <prop key="hibernate.jdbc.batch_size">25</prop>
  24. <prop key="hibernate.show_sql">true</prop>
  25. <prop key="hibernate.format_sql">false</prop>
  26. <prop key="hibernate.generate_statistics">false</prop>
  27. <prop key="hibernate.cache.use_query_cache">false</prop>
  28. <prop key="hibernate.cache.region_prefix">direct</prop>
  29. <prop key="hibernate.cache.use_structured_entries">false</prop>
  30. <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
  31. <prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>
  32. <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
  33. </props>
  34. </property>
  35. <property name="dataSource" ref="dataSource"/>
  36. <property name="mappingDirectoryLocations">
  37. <list>
  38. <value>classpath:/org/first/config/</value>
  39. <value>classpath:/org/second/config/</value>
  40. </list>
  41. </property>
  42. </bean>
  43. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  44. <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
  45. <property name="minPoolSize" value="${c3p0.minPoolSize}"/>
  46. <property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>
  47. <property name="maxStatements" value="${c3p0.maxStatements}"/>
  48. <property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>
  49. <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"/>
  50. <property name="driverClass" value="${jdbc.driverClassName}"/>
  51. <property name="jdbcUrl" value="${jdbc.url}"/>
  52. <property name="user" value="${jdbc.username}"/>
  53. <property name="password" value="${jdbc.password}"/>
  54. </bean>
  55. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  56. <property name="dataSource">
  57. <ref bean="dataSource"/>
  58. </property>
  59. </bean>
  60. <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  61. <property name="sessionFactory" ref="sessionFactory"/>
  62. </bean>
  63. <bean id="transactionProxyTemplate" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
  64. <property name="transactionManager" ref="transactionManager"/>
  65. <property name="transactionAttributes">
  66. <props>
  67. <prop key="query*">PROPAGATION_REQUIRED</prop>
  68. <prop key="*">PROPAGATION_REQUIRED</prop>
  69. </props>
  70. </property>
  71. </bean>
  72. <!-- 功能模块的引入 -->
  73. <import resource="classpath:/org/first/config/context_first.xml"/>
  74. <import resource="classpath:/org/second/config/context_second.xml"/>
  75. </beans>

struts.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <package name="base-struts-default" extends="struts-default">
  7. <global-results>
  8. <result name="error" type="dispatcher">/jsp/error.jsp</result>
  9. </global-results>
  10. <action name="index" class="org.first.action.FirstIndexAction">
  11. <result name="success">/index.jsp</result>
  12. </action>
  13. </package>
  14. <!-- 开发测试模块 -->
  15. <include file="org/first/config/struts_first.xml"></include>
  16. <include file="org/second/config/struts_second.xml"></include>
  17. </struts>

到目前为止,工程已经搭建完毕,可以正常启动。

那么spring,struts2,hibernate到底能不能正常使用,我们还需做个功能来测试。

并且底层的api需要简单封装以便于开发。这里内容有点多了,我新启一篇,放到下片讲解。

发表评论

表情:
评论列表 (有 0 条评论,466人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ROS()工程

    1、创建Catkin的WorkSpace catkin是ROS的一个官方的编译构建系统,是原本的ROS的编译构建系统rosbuild的替代者。一个Catkin WorkSp