zoukankan      html  css  js  c++  java
  • Spring -- 1

      目前公司项目用的框架是Spring4.0+Mybatis+tiles,用ivy管理jar包,深度封装。其中还包括了公司自己开发的工作流和标签,开发起来还是很方便的。

         但是要自己去把框架搭建起来还是有一定的难度的,一点一点试一下。

         本文主要讲述了如何搭建一个Spring工程并用JUint去测试。

         这个过程还是比较容易的。

    •      新建一个工程test1,按照MVC的架构建好相应的目录,并把相应的jar包放到lib下(后面报错都是由于jar包没有导入的问题)
    •      在编译路径下建一个Spring的配置文件applicationContext.xml,配置的信息需要仔细的琢磨一下,注释写的都很清楚:      
     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!-- 引用Spring 的多个Schama空间的格式定义文件 -->
     3 
     4 <beans xmlns="http://www.springframework.org/schema/beans"
     5     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     6     xmlns:p="http://www.springframework.org/schema/p"
     7     xmlns:context="http://www.springframework.org/schema/context"
     8     xmlns:aop="http://www.springframework.org/schema/aop" 
     9     xmlns:tx="http://www.springframework.org/schema/tx"
    10     xsi:schemaLocation="http://www.springframework.org/schema/beans 
    11        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    12        http://www.springframework.org/schema/context 
    13        http://www.springframework.org/schema/context/spring-context-3.0.xsd
    14        http://www.springframework.org/schema/tx 
    15        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    16        http://www.springframework.org/schema/aop
    17        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    18    <!-- 扫描类包,将标注Spring注解的类自动转换为Bean,同时完成bean的注入 -->  
    19    <context:component-scan base-package="module2.UserDao"/>
    20    <context:component-scan base-package="module2.service.impl"/>
    21    
    22    <!-- 定义一个使用DBCP实现的数据源 -->
    23    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    24       destroy-method="close"
    25       p:driverClassName="oracle.jdbc.driver.OracleDriver"
    26       p:url="jdbc:oracle:thin:@localhost:1521:orcl"
    27       p:username="ftcsp_dev"
    28       p:password="ftcsp_dev"/>
    29       
    30    <!-- 配置JDBC模板 -->
    31    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
    32       p:dataSource-ref="dataSource"/>
    33    
    34    <!-- 配置事务管理器 -->
    35    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
    36       p:dataSource-ref="dataSource"/> 
    37       
    38    <!-- 通过AOP配置使service包下所有的bean的所有方法拥有事务 -->
    39    <aop:config proxy-target-class="true">
    40       <aop:pointcut expression="execution(* com.baobaotao.service..*(..))" 
    41       id="serviceMethod"/>
    42       <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
    43    </aop:config>         
    44    
    45    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    46         <tx:attributes>
    47             <tx:method name="*" />
    48         </tx:attributes>
    49     </tx:advice>        
    50 </beans>

               里面配置扫描类包,其实可以用SPEL表达式,不用一个一个配置。

    •    写JUnit4测试类,很幸运就是Spring对JUnit也提供了支持,虽然对其中的API不是很熟悉,还是很方便的,代码不多:

               

    package module2.service.Impl;
    
    import static org.junit.Assert.assertEquals;
    import module2.service.IUserService;
    import module2.vo.FtcspSecUser;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)//指定测试运行器
    @ContextConfiguration(locations={"/applicationContext.xml"})//指定Spring的配置文件
    public class TestUserService {
    	
    	@Autowired
    	private IUserService userServiceImpl;
    
    	@Test
    	public void test() {
    		FtcspSecUser user = userServiceImpl.findUserByLoginName("fengbaolin");
    		System.out.println(user.getUserName());
    		System.out.println(user.getEncode());
    		System.out.println(user.getLoginName());
    		System.out.println(user.getId());
    		
    		assertEquals(user.getLoginName(), "wwwyyyccc");
    	}
    }
    

          目前我用的框架规范,是要先把Service托管到Spring,然后配置<property>注入具体的Service类中(需要写set方法),感觉很规范。

               这里 @Autowired自动装配用起来感觉也不错,set方法都不用写了。

    •    对JdbcTemplate模板的API不是很熟悉,需要百度一下。之前用到的是Mybatis的接口也是封装过的,数据源是dbcp的数据源。

         完成后报错都是由于和aop有关的jar包没有导入,还有就是Service类没有用@Service注解。

         这篇Blog参考了Spring3.X企业应用开发实战。工程test1已上传。

         

         

  • 相关阅读:
    宏任务、微任务与Event Loop
    puppteer的使用
    docker的使用 -- windows
    vscode集成eslint
    删除git中无用的大文件
    git 使用
    利用chrome devtool 观察页面占用内存
    JS对象-不可扩展对象、密封对象、冻结对象
    数学
    素数 + 背包
  • 原文地址:https://www.cnblogs.com/lovery/p/3684350.html
Copyright © 2011-2022 走看看