用IntelliJ IDEA 開發Spring+SpringMVC+Mybatis框架 分步搭建三:配置spring並測試


這一部分的主要目的是 配置spring-service.xml  也就是配置spring  並測試service層 是否配置成功

用IntelliJ IDEA 開發Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 並測試(2 配置spring-dao和測試)
在這個基礎上面 繼續進行spring的配置。

回顧上面  我們已經成功測試通過了Mybatis的配置. 這時候的目錄結構是:

一:下面我們繼續補充目錄結構,在com.peakfortake的文件目錄項目  創建service、dto和util文件夾,在spring文件下面創建spring-service.xml
文件。並復制spring-dao.xml的頭 到spring-service.xml中 如圖

 

二:配置spring-service.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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--spring service配置-->
    <!-- 掃描service包下面所有使用注解的的類型-->
    <context:component-scan base-package="peakfortake.service"/>

    <!-- 配置事務管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <!-- 因為這個spring-service中沒有定義dataSource但在spring-dao中已經定義了 所以后期執行的時候 會自動找到-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 配置基於注解聲明事務-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

三:實現service  並加入注解 最主要的是當中的注解  @service 和@Transactional  尤其是事務的注解

@Service
public class TestUserServiceImpl implements TestUserService {
    //注入依賴
    @Autowired
    private TestUserDao testUserDao;

    @Transactional
    /**
     * 使用注解控制事務方法的優點
     * 1:開發團隊達成一致的約定,明確標注事務方法的編碼規范
     * 2:保證事務方法的執行時間盡可能短,不要穿插其他網絡操作 如HTTP操作或者剝離事務外部方法
     * 3:不是所有的方法都需要事務,如果有一條修改操作 只讀操作 不需要事務控制
     */
    public List<String> getAllUserName() {
        List<TestUser> t = testUserDao.getUser();

        List<String> allName = new ArrayList<String>();
        for (TestUser tt : t) {
            allName.add(tt.getUserName());
        }
        return allName;
    }

    public List<TestUser> userList() {
        return testUserDao.getUser();
    }
}

 

四:建立logback日志 並測試 

1、在resource下面建立logback.xml文件 
並在logbakc的官網上面  http://logback.qos.ch/manual/configuration.html 其中的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>


2:測試類是

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/spring-dao.xml","classpath:spring/spring-service.xml"})
public class TestUService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TestUserService testUserService;

    @Test
    public void getTN(){
        List<String> mm= testUserService.getAllUserName();
        logger.info("AllName={}",mm.toString());
    }
}

 測試通過:說明  spring配置完成

這個時候我們的文件結構是:

然后最后一步 就是進行
spring-web的配置。。

 

用IntelliJ IDEA 開發Spring+SpringMVC+Mybatis框架 分步搭建四:配置springmvc


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM