Spring3新体验

在使用Spring3的时候,与之前使用最大的区别在于将以前放在配置文件XML中的信息都放入到JAVA代码中了,这个对于习惯编写XML的人来说可能有点不适宜了,不过没关系,Spring3对于这些都是兼容的,可以一部分通过XML来配置,一部分通过放入JAVA代码中的"注释"来配置;下面将Spring3的一些使用简单描叙下,以备以后查用:

 

<?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:util="http://www.springframework.org/schema/util"
 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-3.0.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-3.0.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:annotation-config />
 <context:component-scan base-package="com.zsxn" />

 <util:properties id="systemProperties"
  location="classpath:resources/Jettbatch.properties" />

 

这里通过<util:properties来读取文件中的配置信息;以前是使用config.PropertyPlaceholderConfigurer来进行读取;

 

当在JAVA类中,需要使用配置文件中的信息的时候,通过@Value("#{systemProperties['output_path']}")来进行读取,其中output_path为配置文件中的KEY值;

<context:component-scan标记会将base-package中的类产生BEAN实例;这些类中需要加入"注释",如:@Repository,@Service,@Component等信息;

<bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="packagesToScan">
   <list>
    <value>com.zsxn.tm.cmbcc.data</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
    <prop key="heibernat.show_sql">true</prop>
   </props>
  </property>
 </bean>

 <!-- Transaction manager for a single Hibernate SessionFactory (alternative
  to JTA) -->
 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager"
  autowire="byName">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean>

 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
  </tx:attributes>
 </tx:advice>

 <aop:config>
  <aop:pointcut id="baseServiceMethods"
   expression="execution(* com.zsxn.tm.cmbcc.service.BaseProcessService.*(..))" />
  <aop:advisor advice-ref="txAdvice" pointcut-ref="baseServiceMethods" />
 </aop:config>

以上信息是定义SESSIONFACTORY和事务的;

 

有时候我们自己定义的POJO中,有些属性是不需要对应到数据库中的,而有的是对应到数据库中,所以在针对这种POJO进行"注释"的时候,需要将属性标记详细,如下:

 

@Entity
@Table(name = "tablename")
public class Test implements Serializable {

 @GenericGenerator(name = "generator", strategy = "assigned")
 @Id
 @Column(name = "columnname1")
 @GeneratedValue(generator = "generator")
 private String guid;

 // fields
 @Column(name = "columnname2")
 private String coverage6; 

 @Id
 private String coverage_no;

 

这里的coverage_no在数据库的表中没有对应,所以必须在这里加入 @Id,否则就会出现coverage_no这个列找不到的异常;或者添加别的"注释"来处理这个问题!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值