1、struts2中的struts.xml配置文件:
<<span style="font-size:14px;">?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!-- 每次在访问某个动作的时候都会重新加载配置文件 struts.devMode = false(default.properties) 利用strutx.xml中的constant元素来覆盖掉default.properties默认行为 -->
<constant name="struts.devMode" value="true"></constant>
<constant name="struts.i18n.encoding" value="UTF-8"></constant><!-- 相当于HttpServletRequest.setCharacterEncoding("UTF-8") -->
<constant name="struts.action.extension" value="action,,do"></constant><!-- Struts2框架实际要处理的后缀 -->
<constant name="struts.serve.static.browserCache" value="false"></constant><!-- 静态资源是否需要缓存,开发阶段最好改成false -->
<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant><!--禁用动态方法调用 -->
<constant name="struts.multipart.maxSize" value="52428800"></constant>
<!-- package的继承案例:配置全局的结果视图 -->
<package name="mypackage" extends="struts-default">
<!-- <interceptors> 只是定义拦截器,并没有起作用 -->
<!-- <interceptor name="permissionInterceptor" class="cn.itcast.interceptor.PermissionInterceptor"></interceptor>-->
<!-- <interceptor-stack name="mydefaultstack">-->
<!-- <interceptor-ref name="defaultStack"></interceptor-ref>-->
<!-- <interceptor-ref name="permissionInterceptor"></interceptor-ref>-->
<!-- </interceptor-stack>-->
<!-- </interceptors>-->
<!-- 配置全局错误结果 :范围只是本包-->
<global-results>
<result type="dispatcher" name="error">/customer/error.jsp</result>
</global-results>
</package>
<include file="customer.xml"></include>
<package name="orders" namespace="/orders" extends="mypackage">
<!-- <action name="orders_add" class="cn.itcast.action.OrdersAction" method="add">-->
<!-- <result type="dispatcher" name="success">/orders/success.jsp</result>-->
<!-- </action>-->
<!-- <action name="orders_update" class="cn.itcast.action.OrdersAction" method="update">-->
<!-- <result type="dispatcher" name="success">/orders/success.jsp</result>-->
<!-- </action>-->
<!-- <action name="orders_delete" class="cn.itcast.action.OrdersAction" method="delete">-->
<!-- <result type="dispatcher" name="success">/orders/success.jsp</result>-->
<!-- </action>-->
<!-- <action name="orders_find" class="cn.itcast.action.OrdersAction" method="find">-->
<!-- <result type="dispatcher" name="success">/orders/success.jsp</result>-->
<!-- </action>-->
<action name="orders_*" class="cn.itcast.action.OrdersAction" method="{1}">
<result type="dispatcher" name="success">/orders/{1}.jsp</result>
<result type="dispatcher" name="input">/orders/{1}.jsp</result>
</action>
<!--
当访问http://localhost:8080/struts2day02/orders/orders_add动作时,就去调用add方法,并且转向add.jsp页面。因为使用了通配符。
-->
</package>
<!-- package的继承案例:配置全局的结果视图 -->
<package name="scope" namespace="/scope" extends="mypackage">
<action name="scopeAction" class="cn.itcast.action.ScopeAction" method="execute">
<result type="redirect" name="success">/scope.jsp</result>
</action>
</package>
<package name="webObj" namespace="/webObj" extends="mypackage">
<action name="webAction1" class="cn.itcast.action.WebObjectAction" method="execute1">
<result name="success">/web.jsp</result>
</action>
<action name="webAction2" class="cn.itcast.action.WebObjectAction" method="execute2">
<result name="success">/web.jsp</result>
</action>
</package>
<package name="upload" namespace="/upload" extends="mypackage">
<action name="upload1" class="cn.itcast.action.UploadAction1" method="execute">
<result name="success">/success.jsp</result>
</action>
<action name="upload2" class="cn.itcast.action.UploadAction2" method="execute">
<result name="success">/success.jsp</result>
</action>
</package>
<package name="interceptor" extends="mypackage">
<action name="visitIndex" class="cn.itcast.action.VisitAction" method="execute">
<!-- <interceptor-ref name="mydefaultstack"></interceptor-ref>-->
<result name="success">/index.jsp</result>
<result name="login">/login.jsp</result>
</action>
</package>
<package name="validate" namespace="/validate" extends="mypackage">
<action name="user_*" class="cn.itcast.action.UserAction" method="{1}">
<result name="success">/success.jsp</result>
<result name="input">/{1}User.jsp</result>
</action>
</package>
</struts></span>
2、hibernate中的hibernate.cfg.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="jdbc.fetch_size">50</property>
<!-- 配置C3p0连接池 -->
<!-- 启用c3p0连接池 设置连接池提供的供应商 -->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- 最大连接数 -->
<property name="c3p0.max_size">20</property>
<!-- 最小连接数 -->
<property name="c3p0.min_size">5</property>
<!-- 每次请求连接的数目 -->
<property name="c3p0.acquire_increment">5</property>
<!-- 设置过期时间,以秒为单位,如果连接池中 -处于空闲状态的连接超过了这个时间,该连接就会从连接池中删除-->
<property name="c3p0.timeout">120</property>
<!-- 每个3000秒检查连接池中的空闲连接 -->
<property name="c3p0.idle_test_period">3000</property>
<!-- 设置隔离级别 1 2 4 8-->
<property name="connection.isolation">2</property>
<!-- 配置session的线程本地化 threadLocal -->
<property name="current_session_context_class">thread</property>
<!-- 是否自动建立表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 是否在控制台显示sql语句 -->
<property name="show_sql">true</property><span style="font-size:14px;"></span><pre name="code" class="html"><pre name="code" class="html"> <!-- 使用数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> <!-- 导入映射文件 -->
<mapping resource="cn/itcast/oa/entity/User.hbm.xml"/>
</session-factory></hibernate-configuration> 2、1 hibernate映射文件例子:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.itcast.e_oneToMany.Classes" table="classes">
<id name="cid" type="java.lang.Long" length="20">
<column name="cid"></column>
<generator class="increment"></generator>
</id>
<property name="cname" type="java.lang.String" length="20"></property>
<property name="description" type="java.lang.String" length="200"></property>
<!--
set元素和类中的set集合对应
从外键的角度建立classes和student之间的关系
从类和类得角度建立classes和student之间的关系
cascade级联
inverse是用来维护关系的
要么是一对多的关系
要么是多对多的关系
谁来维护关系:invetse所在的映射文件对应的持久化对象维护关系
默认值是false 表示维护关系
true 表示不维护关系
-->
<set name="students" cascade="save-update">
<!--
key是用来描述外键的
column代表就是外键的名字
-->
<key>
<column name="cid"></column>
</key>
<!--
one-to-many 描述classes和哪个对象进行关联
-->
<one-to-many class="cn.itcast.e_oneToMany.Student"/>
</set>
</class>
</hibernate-mapping>
3、spring声明式事务数据库配置样例
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 1 配置本地化代理工厂bean 这是spring整合hibenate的入口 加载hibernate.cfg.xml 创建sessionFactory对象-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 配置 hibernate.cfg.xml文件 classpath:表示从类路径下加载文件-->
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 2 配置hibernage模板 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<!-- 注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 3 配置hibernage的事务管理器 在aop术语中切面 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 4 配置通知 -->
<tx:advice id="advice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--5 配置切入点 -->
<aop:config>
<!-- 配置切入点 ,这里的切入点应用的类级别-->
<aop:pointcut id="perform" expression="execution( * cn.itcast.oa.service..*.*(..))"/>
<!-- 建立通知和切入点的结合 -->
<aop:advisor advice-ref="advice" pointcut-ref="perform"/>
</aop:config>
</beans>
3.1 spinrg注解配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!--
1、导入命名空间
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
2、到入依赖注入的注解解析器
<context:annotation-config></context:annotation-config>
3、把student和person导入进来
-->
<context:annotation-config></context:annotation-config>
<bean id="student" class="cn.spring.annotation.Student"></bean>
<bean id="person" class="cn.spring.annotation.Person"></bean>
</beans>
扫描注解解析器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!--
1、导入命名空间
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
2、启动类扫描的注解解析器
3、启动依赖注入的注解解析器
-->
<!--
component就是bean
base-package
会在base-package的值所在的包及子包下扫描所有的类
-->
<context:component-scan base-package="cn.itcast.spring.scan"></context:component-scan>
</beans>
3.2 Spring中的AOP配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!--
1、引入AOP的命名空间
2、目标类
3、切面
4、拦截器 由spring内部实现
5、aop的配置
-->
<bean id="personDao" class="cn.itcast.spring.aop.xml.PersonDaoImpl"></bean>
<bean id="transaction" class="cn.itcast.spring.aop.xml.Transaction"></bean>
<!--
aop的配置
-->
<aop:config>
<!--
切入点表达式
expression
确定哪个类可以生成代理对象
id 唯一标识
-->
<aop:pointcut expression="execution(* cn.itcast.spring.aop.xml.PersonDaoImpl.*(..))" id="perform"/>
<!--
切面
-->
<aop:aspect ref="transaction">
<!--
前置通知
* 在目标方法执行之前
*
-->
<!--
<aop:before method="beginTransaction" pointcut-ref="perform"/>
-->
<!--
后置通知
* 在目标方法执行之后
* 可以根据returning获取目标方法的返回值
* 如果目标方法遇到异常,该通知不执行
-->
<!--
<aop:after-returning method="commit" pointcut-ref="perform" returning="val"/>
-->
<!--
前置通知和后置通知只能在目标方法文中添加内容,但是控制不了目标方法的执行
-->
<!--
最终通知
* 在目标方法执行之后
* 无论目标方法是否遇到异常,都执行
* 经常做一些关闭资源
-->
<!--
<aop:after method="finallyMethod" pointcut-ref="perform"/>
-->
<!--
异常通知
目的就是为了获取目标方法抛出的异常
-->
<aop:after-throwing method="exceptionMethod" throwing="ex" pointcut-ref="perform"/>
<!--
环绕通知
能控制目标方法的执行
-->
<aop:around method="aroundMethod" pointcut-ref="perform"/>
</aop:aspect>
</aop:config>
</beans>
3.3 注入方式
3.3.1利用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-2.5.xsd">
<!--
beans
把一个类放入到spring容器中,该类就称为bean
-->
<!--
一个bean就代表一个类
id就是唯一标识符
-->
<bean id="person" class="cn.itcast.spring.di.xml.constructor.Person">
<!--
构造函数的参数
index 第几个参数,下标从0开始
type 参数的类型
ref 如果类型是引用类型,赋值
value 如果类型是基本类型,赋值
说明:
只能指定一个构造函数
-->
<constructor-arg index="0" type="java.lang.String" value="干露露"></constructor-arg>
<constructor-arg index="1" ref="student"></constructor-arg>
</bean>
<bean id="student" class="cn.di.xml.constructor.Student"></bean>
</beans>
3.3.2利用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-2.5.xsd">
<bean id="person" class="cn.itcast.spring0909.di.xml.set.Person">
<!--
property就是代表属性
在spring中基本类型(包装类型和String)都可以用value来赋值
引用类型用ref赋值
-->
<property name="pid" value="5"></property>
<property name="pname" value="王二麻子"></property>
<property name="student">
<ref bean="student"/>
</property>
<property name="lists">
<list>
<value>list1</value>
<value>list2</value>
<ref bean="student"/>
</list>
</property>
<property name="sets">
<set>
<value>set1</value>
<value>set2</value>
<ref bean="student"/>
</set>
</property>
<property name="map">
<map>
<entry key="map1">
<value>map1</value>
</entry>
<entry key="map2">
<value>map2</value>
</entry>
<entry key="map3">
<ref bean="student"/>
</entry>
</map>
</property>
<property name="properties">
<props>
<prop key="prop1">
prop1
</prop>
</props>
</property>
</bean>
<bean id="student" class="cn.itcast.spring.di.xml.set.Student"></bean>
</beans>
3.4Spring创建bean的时期
<?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-2.5.xsd">
<!--
在启动spring容器的时候,spring容器配置文件中的类就已经创建完成对象了
lazy-init
default false
true 在context.getBean的时候才要创建对象
* 优点
如果该bean中有大数据存在,则什么时候context.getBean,什么时候创建对象
可以防止数据过早的停留在内存中,做到了懒加载
* 缺点
如果spring配置文件中,该bean的配置有错误,那么在tomcat容器启动的时候,发现不了
false 在启动spring容器的时候创建对象
* 优点
如果在启动tomcat时要启动spring容器,
那么如果spring容器会错误,这个时候tomcat容器不会正常启动
* 缺点
如果存在大量的数据,会过早的停留在内存中
-->
<!--
在默认情况下,spring创建bean是单例模式
scope
singleton 默认
单例
属性是共享的
一般情况下,把数据存放在方法中的变量中
prototype
多例
当一个bean是多例模式的情况下,lazy-init为false或者default无效
-->
<bean id="helloWorld" class="cn.itcast.spring.createobject.when.HelloWorl" lazy-init="true"></bean>
<bean id="person" class="cn.itcast.spring.createobject.when.Perso" lazy-init="true"></bean>
</beans>
3.4.1Spring销毁bean时期
<?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-2.5.xsd">
<!--
init-method
* 该方法是由spring容器执行
* 在构造函数之后执行
* 如果在构造函数之后,在调用方法之前要做一些工作,可以在init方法中完成
destroy-method
* 如果该bean是单例,则在spring容器关闭或者销毁的时候,执行该方法
* 如果该bean是多例,则spring容器不负责销毁
说明:要想让spring容器控制bean的生命周期,那么该bean必须是单例
如果该bean是多例,该bean中还有资源,关闭资源的操作由程序员完成
-->
<bean id="helloWorld" class="cn.itcast.spring.initdestroy.HelloWorld" scope="prototype" init-method="init" destroy-method="destroy"></bean>
</beans
4、web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">
<!-- 整合Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置Spring的OpenSessionInViewFilter,以解决懒加载异常 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 配置Struts2的过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>