• 使用注解简化配置ssh文件


    在SSH框架上简化配置文件。

    SSH框架的搭建可以参考:http://www.cnblogs.com/thz-weiai/p/5842208.html

    首先我们需要找到applicationContext.xml文件(用上一次搭建的ssh框架为例):

    <?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:aop="http://www.springframework.org/schema/aop"   
            xmlns:context="http://www.springframework.org/schema/context"  
            xmlns:jee="http://www.springframework.org/schema/jee"  
            xmlns:tx="http://www.springframework.org/schema/tx"  
            xsi:schemaLocation="    
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd  
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
                http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd  
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
       <!-- 头信息一般不会变--> 
      <!-- 类似于财务部门一样,类就是钱,所有需要类的实例都由spring去管理 -->
        
        <bean id="myIndexAction" class="ssh.action.IndexAction" scope="prototype">
            <!-- setIs(myIndexService) -->
            <property name="is" ref="myIndexService"/>
        </bean>
        
        <!-- myIndexService = new ssh.service.IndexServiceImpl() -->
        <bean id="myIndexService" class="ssh.service.IndexServiceImpl" scope="prototype">
            <property name="id" ref="myIndexDao"/>
        </bean>
        
        <bean id="myIndexDao" class="ssh.dao.IndexDaoImpl" scope="prototype">
            <property name="sessionFactory" ref="mySessionFactory" />
        </bean>
        
        <!-- 数据库连接池是单例 -->
        <bean id="mySessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <!-- 注入连接池,包含了数据库用户名,密码等等信息 -->
            <property name="dataSource" ref="myDataSource"/>
            
            <!-- 配置Hibernate的其他的属性 -->
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                    <prop key="hibernate.connection.autocommit">false</prop>
                    <!-- 开机自动生成表 -->
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                </props>
            </property>
            <property name="mappingResources">
                <list>
                    <value>ssh/entity/BookCard.hbm.xml</value>
                </list>
            </property>
        </bean>
            
        <!-- C3P0连接池 -->
        <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"/>
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/BookCardDB"/>
            <property name="user" value="root"/>
            <property name="password" value=""/>
            <!-- 每300秒检查所有连接池中的空闲连接 -->
            <property name="idleConnectionTestPeriod" value="300"></property>
            <!-- 最大空闲时间,900秒内未使用则连接被丢弃。若为0则永不丢弃 -->
            <property name="maxIdleTime" value="900"></property>
            <!-- 最大连接数 -->
            <property name="maxPoolSize" value="2"></property>
        </bean>
        
        <!-- dbcp连接池 -->
        <bean id="myDataSource2" destroy-method="close" class="org.apache.commons.dbcp2.BasicDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/BookCardDB"/>
            <property name="username" value="root"/>
            <property name="password" value=""/>
            
         </bean>
        
    </beans>

    我们可以发现下面这段代码是不是感觉比较复杂或者说不好看、不好理解:

     我们可以先简单的简化一下,

      1.在applicationContext.xml文件加上下面一段代码:

      <context:annotation-config/>

      这一行代码的原理是:自动注入processor解析器,用来解析注解

      2.把上面的这一行代码加上之后,就可以把那一段代码改成下面这样:

      3.这里改好了之后,就需要添加注解了:

        (1).先找到IndexAction.java文件,找到里面的注入代码:

        

        (2).将这里的注入改为注释的格式:

        

        (3).上面只列举了在action中的注解,我们还需要在dao、service里也使用同样的方法添加注解。这里就不一一举例了。

    我们在这个基础上再次简化。

    这次简化,我们就把下面这几行代码去掉:

     

      (1).再次来到applicationContext.xml文件,将我们第一次简化添加的代码删掉,再添加另外一条代码:

      <context:component-scan base-package="ssh"></context:component-scan>

      这行代码会自动扫描包,也会自动注入解释器,所以不需要 context:annotation-config

      "ssh"这个则表示所有class,因为上面的class都用了ssh做为前缀。

      (2).添加好上面的代码就可以将要去掉的代码去掉

      (3).了解一些我们常用的注解

        1.@Controller注解--定义控制器(类级别上的注解)action)

          2.@service注解--定义业务处理类(service)

          3.@repository注解--定义底层数据库处理接口(dao)

          4.@Resource注解--实现注入(jdk)

          5.@Scope("prototype")--//非单例

      (4).再次来到action文件,将文件改为一下模式:

      

      

      (5).上面依旧是以action为例,dao、service也需要改,格式是一样的。但要注意不同的模块使用注解是不同的。 

       

  • 相关阅读:
    cas 单点登录服务端客户端配置
    POI 导出excel
    关于小米手机刷机亲尝
    C#对本地文件重命名--适用于下载的图片、电视剧等奇怪名字的重命名
    泛型List<T>与非泛型ArrayList
    设置一键启动多文件
    网页显示电子表
    插入sql语句01值时,在数据库中的查询时显示为1
    C#面向对象--继承
    SqlServer数据库查询不同字段-年龄段分析
  • 原文地址:https://www.cnblogs.com/thz-weiai/p/5967665.html
Copyright © 2020-2023  润新知