**
struts.xml **
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!--禁用动态访问 user!add.action--> <constant name="struts.enable.DynamicMethodInvocation" value="false"/> <!-- 开发模式下使用,这样可以打印出更详细的错误信息 --> <constant name="struts.devMode" value="true"/> <!-- 设置url请求后缀 --> <constant name="struts.action.extention" value="do,action,html,htm"/> <!--把主题配置成simple--> <constant name="struts.ui.theme" value="simple"/> <!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 --> <constant name="struts.serve.static.browserCache" value="false" /> <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 --> <constant name="struts.configuration.xml.reload" value="true" /> <!-- spring 托管 --> <constant name="struts.objectFactory" value="spring" /> <!-- 该属性指定处理 MIME-type multipart/form-data,文件上传 --> <constant name="struts.multipart.parser" value="cos" /> <constant name="struts.multipart.parser" value="pell" /> <constant name="struts.multipart.parser" value="jakarta" /> <!-- 指定上传文件时的临时目录,默认使用 javax.servlet.context.tempdir --> <constant name="struts.multipart.saveDir" value="/tmpuploadfiles" /> <!-- 该属性指定Struts 2文件上传中整个请求内容允许的最大字节数 --> <constant name="struts.multipart.maxSize" value="2097152" /> <!-- 设置是否支持动态方法调用,true为支持,false不支持. --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <package name="" extends="struts-default" namespace="/"> </package> </struts>hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory name=""> <!--设置方言--> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!--是否打印sql语句。--> <property name="show_sql">true</property>。 <!--是否格式化sql语句。--> <property name="format_sql">true</property> <!--设置更新数据库。--> <property name="hbm2ddl.auto">update</property> <!--设置被映射的bean类。--> <mapping resource="com/cloud/sys/domain/User.hbm.xml" /> <!--设置url。--> <property name="connection.url">jdbc:mysql://localhost:3306/oa</property> <!--设置数据库账号。--> <property name="connection.username">root</property> <!--设置数据库密码。--> <property name="connection.password">root</property> <!--设置数据库驱动。--> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!--设置c3p0连接池。--> <property name="hibernate.connection.provider_class"> org.hibernate.connection.C3P0ConnectionProvider </property> <!-- 指定连接池里最大连接数 --> <property name="hibernate.c3p0.max_size">20</property> <!-- 指定连接池里最小连接数 --> <property name="hibernate.c3p0.min_size">1</property> <!-- 指定连接池里连接的超时时长 --> <property name="hibernate.c3p0.timeout">5000</property> <!-- 指定连接池里最大执行命令的个数 --> <property name="hibernate.c3p0.max_statements">100</property> <!-- 指定连接池里空闲测试时间 --> <property name="hibernate.c3p0.idle_test_period">3000</property> <!-- 指定连接池里每次增加的连接数 --> <property name="hibernate.c3p0.acquire_increment">2</property> <!-- 指定连接池每次都验证连接是否可用 --> <property name="hibernate.c3p0.validate">false</property> <!-- 设置得到的session为线程类型的 --> <property name="hibernate.current_session_context_class">thread</property> </session-factory"> </hibernate-configuration>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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置注解扫描 --> <context:component-scan base-package="zhujie"></context:component-scan> <!-- 创建sessionfactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property value="classpath:hibernate.cfg.xml" name="configLocation"/> //引进hibernate </bean> <!-- 配置事务管理器 --> <bean class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="txManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 配置事务增强 (xml的方式来配置事务)--> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="find*" read-only="true"/> <tx:method name="get*" read-only="true"/> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 配置AOP --> <aop:config> <!-- 切入点--> <aop:pointcut expression="execution(* com.cloud.*.service.impl.*.*(..))" id="pt"/> <!-- 切面 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/> </aop:config> <!-- 引入外部spring文件 --> <import resource="com/cloud/conf/test-spring.xml"/> <!-- 设置用注解的方式来开启事务 ( 需要在需要开启的类上面加上@Transactional )--> <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true" /> </beans>spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service 和 @Controller。
@Service用于标注业务层组件
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件,即DAO组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
//=====================================
@Autowired与@Resource的区别
1、@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上。 2、@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用 3、@Resource(这个注解属于J2EE的),默认安照名称进行装配,名称可以通过name属性进行指定, 如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。 当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。user.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.cloud.sys.domain"> <class name="User" table="t_user"> <id name="id" column="u_id"> <generator class="native"/> </id> <property name="account"/> <property name="password"/> <property name="gender"/> <property name="country"/> <property name="name"/> <property name="age"/> <property name="birthday"/> </class> </hibernate-mapping>在applicationContext.xml中配置如下原本在hibernate.cfg.xml中需要配置的信息,在spring中配置后hibernate.cfg.xml 可删除。 2、 db.properties
jdbcUrl=jdbc:mysql://localhost:3306/itcastTax?useUnicode=true&characterEncoding=utf8 driverClass=com.mysql.jdbc.Driver user=root password=root initialPoolSize=10 maxPoolSize=30 <!-- 导入外部的properties配置文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 配置c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="driverClass" value="${driverClass}"></property> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="${initialPoolSize}"></property> <!--连接池中保留的最小连接数。Default: 3 --> <property name="minPoolSize" value="3"></property> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="${maxPoolSize}"></property> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="3"></property> <!--最大空闲时间,1800秒内未使用则连接被丢弃,若为0则永不丢弃。Default: 0--> <property name="maxIdleTime" value="1800"></property> </bean> <!--配置sessionFactory,并将dataSource指向c3p0创建的dataSource:--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="javax.persistence.validation.mode">none</prop> </props> </property> <property name="mappingLocations"> <list> <value>classpath:cn/itcast/nsfw/*/entity/*.hbm.xml</value> <value>classpath:cn/itcast/test/entity/*.hbm.xml</value> </list> </property> </bean> <!--配置spring事务管理:--> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!—事务通知--> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="find*" read-only="true" /> <tx:method name="get*" read-only="true" /> <tx:method name="load*" read-only="true" /> <tx:method name="list*" read-only="true" /> <tx:method name="search*" read-only="true" /> <tx:method name="*" rollback-for="Throwable" /> </tx:attributes> </tx:advice> <!—配置需要进行事务控制的类 --> <aop:config> <aop:pointcut id="serviceOperation" expression="bean(*Service)" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" /> </aop:config> 【注意:上面的pointcut expression 表示拦截以Service结尾的bean,或者可写成 execution(* cn.itcast..service.impl.*.*(..))】