首页 > 开发 > JSP > 正文

Spring学习基础---配置文件

2020-05-06 19:54:26
字体:
来源:转载
供稿:网友
1,配置文件的配置头
<?xml version="1.0" encoding="UTF-8"?>

<!--
  - Application context definition for JPetStore’s business layer.
  - Contains bean references to the transaction manager and to the DAOs in
  - dataAccessContext-local/jta.xml (see web.xml’s "contextConfigLocation").
  -->
<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
这样写才对

2,配置文件可以使用多个属性文件
<!-- Configurer that replaces ${...} placeholders with values from properties files -->
 <!-- (in this case, mail and JDBC related properties) -->
 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
    <value>WEB-INF/mail.properties</value>
    <value>WEB-INF/jdbc.properties</value>
   </list>
  </property>
 </bean>
类是框架的。
里面包含两个属性文件,属性文件里都是“key=value”这种形式的。这样配置文件里就可以使用属性文件里的key,使用方法
${key},这样转移出属性设置,维护起来比较方便。

3,定义Validator供web层使用,自定义类。
<bean id="accountValidator" class="org.springframework.samples.jpetstore.domain.logic.AccountValidator"/>
类里面使用了ValidatorUtils系统类来进行处理。

4,服务层的定义。
 PetStoreImpl定义在配置文件中,是自己的类。
 所有的DAO都是它的属性,注意,DAO是interface,而不是class.
 PetStoreImpl中定义了所有的DAO接口作为属性,定义了他们的set方法,但是没有定义get方法。
 这样所有的业务操作就可以不用管DAO是如何实现的了,而只管使用这个PetStoreImpl就好了。

 DAO都是接口这种做法与平时开发不一样,我以前使用hibernate生成工具生成的dao都是默认好的实现类。
 而此处的DAO却都是接口。他们的实现方法是这样的:
 interface PetStoreFacade { } //定义所有的业务方法。
 interface AccountDao{} //定义所有帐户的业务方法。
 interface CategoryDao{} //定义类别的业务方法。
 interface ProductDao{} //定义产品的业务方法。
 。。。其他DAO接口,定义自己的业务方法。
 class PetStoreImpl implements PetStoreFacade //这个类就是一个javabean,操作的都是接口。
 //定义所有DAO接口当作自己的属性。
 //实现set方法
 //实现PetStoreFacade 定义的业务接口,实现的时候调用DAO接口的方法。

 如果是我自己,那么就会定义IDAO当作接口,因为hibernate插件自动生成dao类,容易混淆。
 

共4页上一页1234下一页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表