當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring-学习笔记06【Spring的新注解】
生活随笔
收集整理的這篇文章主要介紹了
Spring-学习笔记06【Spring的新注解】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- Java后端 學習路線 筆記匯總表【黑馬程序員】
目錄
01 spring的新注解-Configuration和ComponentScan
02 spring的新注解-Bean
03 AnnotationConfigApplicationContext的使用
04 spring的新注解-Import
05 spring的新注解-PropertySource
06 Qualifier注解的另一種用法
07 spring整合junit問題分析
08 spring整合junit完成
8.1、測試類AccountServiceTest.java
01 spring的新注解-Configuration和ComponentScan
02 spring的新注解-Bean
03 AnnotationConfigApplicationContext的使用
04 spring的新注解-Import
05 spring的新注解-PropertySource
06 Qualifier注解的另一種用法
create database eesy02;use eesy02;create table account(id int primary key auto_increment,name varchar(40),money float )character set utf8 collate utf8_general_ci;insert into account(name,money) values('aaa',1000); insert into account(name,money) values('bbb',1000); insert into account(name,money) values('ccc',1000);select * from account;07 spring整合junit問題分析
spring整合Junit分析
1、應用程序的入口
????main方法
2、junit單元測試中,沒有main方法也能執行
????junit集成了一個main方法
????該方法就會判斷當前測試類中哪些方法有 @Test注解
????junit就讓有Test注解的方法執行
3、junit不會管我們是否采用spring框架
????在執行測試方法時,junit根本不知道我們是不是使用了spring框架
????所以也就不會為我們讀取配置文件/配置類創建spring核心容器
4、由以上三點可知
????當測試方法執行時,沒有Ioc容器,就算寫了Autowired注解,也無法實現注入
08 spring整合junit完成
8.1、測試類AccountServiceTest.java
package com.itheima.test;import com.itheima.domain.Account; import com.itheima.service.IAccountService; import config.SpringConfiguration; 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;import java.util.List;/*** 使用Junit單元測試:測試我們的配置* Spring整合junit的配置* 1、導入spring整合junit的jar(坐標)* 2、使用Junit提供的一個注解把原有的main方法替換了,替換成spring提供的* @Runwith* 3、告知spring的運行器,spring和ioc創建是基于xml還是注解的,并且說明位置* @ContextConfiguration* locations:指定xml文件的位置,加上classpath關鍵字,表示在類路徑下* classes:指定注解類所在地位置* 當我們使用spring5.x版本的時候,要求junit的jar必須是4.12及以上.*/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = SpringConfiguration.class) public class AccountServiceTest {@Autowiredprivate IAccountService as = null;@Testpublic void testFindAll() {//3.執行方法List<Account> accounts = as.findAllAccount();for (Account account : accounts) {System.out.println(account);}}@Testpublic void testFindOne() {//3.執行方法Account account = as.findAccountById(1);System.out.println(account);}@Testpublic void testSave() {Account account = new Account();account.setName("test anno");account.setMoney(12345f);//3.執行方法as.saveAccount(account);}@Testpublic void testUpdate() {//3.執行方法Account account = as.findAccountById(4);account.setMoney(23456f);as.updateAccount(account);}@Testpublic void testDelete() {//3.執行方法as.deleteAccount(4);} }總結
以上是生活随笔為你收集整理的Spring-学习笔记06【Spring的新注解】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring-学习笔记04【Spring
- 下一篇: Spring-学习笔记06【spring