From: https://www6.software.ibm.com/developerworks/cn/education/java/j-spring2/index.html
1. spring bean 描述符文件
spring-service.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="employeeService" class="com.ibm.dw.spring2.EmployeeDAO">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="entityManagerFactory" class=
"org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="false"/>
<property name="databasePlatform" value=
"oracle.toplink.essentials.platform.database.DB2Platform"/>
</bean>
</property>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/>
</property>
</bean>
<!-- Driver class -->
<!-- bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:mem:dwspring"/>
<property name="username" value="sa" />
<property name="password" value=" " />
</bean -->
<!-- JNDI -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/dwspring2" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
2. 在 META-INF/context.xml 文件中配置类装入器
<Context>
<Loader loaderClass="org.springframework.instrument.
classloading.tomcat.TomcatInstrumentableClassLoader"/>
...
</Context>
3. 把 Spring 2 的上下文装入器侦听器添加到 Tomcat
Spring 2 要求挂接到 Tomcat 的上下文装入管道。可以在 WAR 文件的 WEB-INF/web.xml 文件添加以下行进行这个配置:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
必须在 <servlet> 和 <servlet-mapping> 定义之前。
4. 在 META-INF/context.xml 中配置 JNDI 资源
<Context>
...
<Resource name="jdbc/dwspring2" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="bill"
password="lotus123" driverClassName="com.ibm.db2.jcc.DB2Driver"
url="jdbc:db2://192.168.23.36:50000/dwspring"/>
</Context>
5. WEB-INF/web.xml 中添加数据源的 JNDI 资源引用
...
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/dwspring2</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
6. WEB-INF/web.xml 中配置上下文参数,这个元素需要是第一个元素
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
<display-name>spring2web</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dwspring2-service.xml</param-value>
</context-param>
...
7. 如果利用了 JPA 的话还需要 META-INF/persistence.xml 文件
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="dwSpring2Jpa" transaction-type="RESOURCE_LOCAL"/>
</persistence>
8. web.xml 中的 Spring DispatcherServlet 配置
<servlet>
<description>
Spring MVC Dispatcher Servlet</description>
<display-name>DispatcherServlet</display-name>
<servlet-name>dwspring2</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwspring2</servlet-name>
<url-pattern>*.cgi</url-pattern>
</servlet-mapping>
9. DispatcherServlet 配置文件
默认情况下,用以下规则形成配置文件的名称:采用 Spring DispatcherServlet 的 servlet 名称,后面加上 _servlet.xml。
Spring DispatcherServlet 类通过 Web 容器接受传入的 Web 请求,并把它分配到某个控制器。
这个示例中,DispatcherServlet 用 dwspring2 名称配置,因此,配置文件应当在 dwspring2_servlet.xml。这个配置文件如下
dwspring2-servlet.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mainController" class="com.ibm.dw.spring2.web.MainController">
<property name="employeeService">
<ref bean="employeeService"/>
</property>
</bean>
<bean name="empDetailsController" class="com.ibm.dw.spring2.web.EmpDetailsController">
<property name="employeeService">
<ref bean="employeeService"/>
</property>
</bean>
<bean id="controllermap" class=
"org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/home.cgi">mainController</prop>
<prop key="/empdet.cgi">empDetailsController</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class=
"org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp"/>
</bean>
</beans>
2007年11月28日星期三
订阅:
博文评论 (Atom)
没有评论:
发表评论