為了配合公司部門績效~ 老是要搞那些framework...
但是他就是肥滋滋的...只有越來越胖...搞得看起來很複雜~ 好像很厲害的樣子...
但是在偶低眼裡還是不如jdbc...
就又先做個簡單的版本設定吧~
因該案子要做的部分很簡單~ 只要撈DB/中介以REST提供的服務就好~
沒有網頁~沒有SOAP~\(^_^)/~
lib配置就不提囉~ 反正有少放的話~ 會一直有ClassNotFound來煩~ 應該就會知道~
單純設定檔~
先從最基本的入口web.xml開始
1. 加入spring主設定檔(路徑要寫對)
2. 有用到DispatcherServlet處理uri pattern和response
3. 用到OpenSessionInViewFilter, 是因為不知道為啥~ open session有問題~ 查一查~ 好像是說在hibernate4, 搭配spring的實作調用不同了~ 在程式內若要直接取currentSession會有問題~ 但是我又不想在程式內用openSession...怕有人又給我忘了關...所以直接調用hibernate4的 filiter出來處理吧QQ/
(基本上~ 我認為會這樣是...政治問題XD...因為hibernate被JBoss買走~ spring是VMware體系...越來越不Open是可想而之的...不綁綁我家的server和機器怎麼賺錢!!?為了凸顯其他家開發上困難或是慢~ 好在sales時做個勝表...少一些其他支援也是正常...)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring4/root-context.xml</param-value>
</context-param>
<!-- Sets the default profile to use in the absence of any profiles set
at deployment time. -->
<context-param>
<param-name>spring.profile.default</param-name>
<param-value>dev</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Handles requests into the application -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring4/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<security-constraint>
<web-resource-collection>
<url-pattern>/*</url-pattern>
<http-method>PUT</http-method>
<http-method>DELETE</http-method>
<http-method>HEAD</http-method>
<http-method>OPTIONS</http-method>
<http-method>TRACE</http-method>
</web-resource-collection>
<auth-constraint></auth-constraint>
</security-constraint>
接下來~
指向root-context, 我簡化為三個設定
server參數檔--> properties
掃component路徑檔 --> servlet-context
資料來源設定(hibernate) --> data
<!-- Turn on support for @Annotation-based configuration e.g. @Inject -->
<context:annotation-config />
<!-- Loads application properties -->
<import resource="properties.xml" />
<!-- Loads controller/component -->
<import resource="servlet-context.xml" />
<!-- Loads model(hibernate) -->
<import resource="data.xml" />
component檔內容
直接給他上層就好~ 省得打一堆資料夾~
<context:component-scan base-package="xxx.yyy.zzz.web.**" />
data檔內容~
差別大概就是~hibernate4 的調用class不同~
另外spring4的使用xml schema也些許不同...不過應該google都能找到ok的範例
另外~ 我沒有使用bean OR mapping...我習慣使用jdbc的sql操作方式(hsql)...
能更彈性的增減物件屬性~ 以及下條件
(誰叫我老是碰到要用db內的function處理資料...hibernate碰到db function..都很難用...)
加上這次專案只有查詢需求~ 沒有新刪修這些transcation鳥事~ 就更不用管他啦^_^Y
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value>
</property>
<property name="url">
<value>jdbc:sqlserver://.......</value>
</property>
<property name="username">
<value>oooo</value>
</property>
<property name="password">
<value>xxxx</value>
</property>
<property name="timeBetweenEvictionRunsMillis" value="300000" />
<property name="numTestsPerEvictionRun" value="6" />
<property name="minEvictableIdleTimeMillis" value="1800000" />
<property name="initialSize" value="3" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="10" />
<property name="minIdle" value="1"/>
<property name="maxWait" value="5000" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="100" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
</props>
</property>
</bean>
servlet檔內容,
主要設定回傳為json格式ResponseBody
<!-- messageConverters beans -->
<beans:bean id="stringHttpMessageConverter"
class="org.springframework.http.converter.StringHttpMessageConverter">
</beans:bean>
<beans:bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
</beans:bean>
<!--
AnnotationMethodHandlerAdapter messageConverters for @ResponseBody
-->
<beans:bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="stringHttpMessageConverter" />
<beans:ref bean="mappingJacksonHttpMessageConverter" />
</beans:list>
</beans:property>
</beans:bean>
下篇再補java code的部分
沒有留言:
張貼留言