文章目錄
1、Junit測試工具
-
什麼是Junit
JUnit是一個Java語言的單元測試框架,Junit測試是程序員測試,即所謂白盒測試,因爲程序員知道被測試的軟件如何(How)完成功能和完成什麼樣(What)的功能
-
優點
極限編程、重構
-
特性
- 用於測試期望結果的斷言(Assertion)
- 用於共享共同測試數據的測試工具
- 用於方便的組織和運行測試的測試套件
- 圖形和文本的測試運行器
-
常用註解
- @Before 註解:在每個測試方法之前執行;
- @After 註解:在每個測試方法之後執行;
- @BeforeClass 註解:在所有方法執行之前執行;
- @AfterClass 註解:在所有方法執行之後執行;
- @Test(timeout = xxx) 註解:設置當前測試方法在一定時間內運行完,否則返回錯誤
- @Test(expected = Exception.class) 註解:設置被測試的方法是否有異常拋出。拋出異常類型爲:Exception.class
- @Ignore 註解:註釋掉一個測試方法或一個類,被註釋的方法或類,不會被執行
- @Test註解:被註解後的類可以理解爲測試類的一個入口
2、IOC
1、getBean()的五種用法的區別
-
context.getBean(String id/name)
通過id值或name值進行找,找到就返回一個Object類型的對象,需要強制類型轉換
-
context.getBean(Class clazz)
通過類型查找,無序類型轉換,但是可能會存在多個類型相同的類型之間衝突,分不清要的是哪一個
-
context.getBean(String id/name , Class clazz)
通過類型和id/name查找,準確,無序類型轉換,不衝突
-
context.getBean(Class clazz , Object…objs)
使用類型,通過工廠方法,使用objs初始化對象的參數列表
-
context.getBean(String id/name , Object…objs)
使用id/name,通過工廠方法,使用objs初始化對象的參數列表
2、bean的id和name屬性的區別
- id的命名必須要滿足XML的命名規範(不能以數字、符號開頭,不能有空格等),而name的命名沒有這麼多的限定
- 在同一個上下文中,id和name之間的命名不能衝突,也就是id和id、id和name、name和name之間,都不能重名
- id只能取一個值,而name可以有多個值
3、IOC注入的方式
-
IOC屬性注入的方式一共有4種,分別爲
-
set注入
本質是調用實體類的setXXX()方法來完成對象的創建
在配置文檔中使用<property name="" value=""/>標籤
-
構造器注入
本質是調用實體類的構造方法來創建對象
在配置文檔中使用<constructor-arg name=" " value=""/>標籤
-
@value注入
使用@value註解在實體類的屬性上,註解的值與屬性的值進行綁定,不推薦這樣子講屬性的值寫死
開啓註解掃描
<context:component-scan base-package="包名"/>
-
註解注入(推薦使用)
使用@Configuration註解標註過的類相當於一個xml配置文檔,而使用@Bean標記過的方法相當於xml配置文檔中的<bean></bean>標籤
-
4、xml配置和註解配置
-
XML配置
就是在applicationContext.xml配置文檔中進行配置
-
註解配置
在實體類上使用@Configuration註解之後,該類就等同於一個配置文檔,不過要在配置文檔中打開註解掃描
5、singleton與prototype的區別
-
singleton(單例)
@Scope(“singleton”)
只有一個實例對象
當將一個Bean定義設置爲singleton作用域時,Spring IOC容器中只會存在一個共享的Bean實例,並且所有對Bean的請求,只要id與該Bean定義相匹配,都只會返回該Bean的同一實例
-
prototype
@Scope(“prototype”)
訪問一次創建一個實例
prototype作用域的Bean會在每次對該Bean請求(將其注入到另一個Bean中,或者以程序的方式調用容器的getBean()方法)時都創建一個新的Bean實例。對有狀態的Bean應使用prototype作用域,而對無狀態的Bean則應該使用singleton作用域。
對於具有prototype作用域的Bean,Spring不能對該Bean的整個生命週期負責,具有prototype作用域的Bean創建後交由調用者負責銷燬對象回收資源
6、懶加載與積極加載
-
@Lazy(true)
懶加載:用到的時候再進行加載,不用不加載
-
@Lazy(false)
默認是積極加載:事先加載好,不管用不用
7、IOC常用的註解
-
@Component
添加在實體類上,表示一個組件
-
@Service
添加在服務層,表示是服務層的代碼
-
@Controller
添加在控制層,表示是控制層的代碼
-
@Repository
添加在接口上,表示是接口
-
@Bean
添加在方法上,便是一個Bean對象
-
@Configuration
配置註解,添加在配置類上,相當於一個配置文檔
-
@Scope
生存範圍,默認是單例(singleton),可以更改爲非單例(prototype)
-
@Lazy
加載策略,默認爲懶加載(true),可以更改爲積極加載(false)
-
@Value
對實體類的屬性進行賦值綁定,不推薦使用
-
@Resource
根據名稱自動注入
-
@Autowired
根據類型自動注入
-
@Qualify
與@Autowired結合,先按照類型進行尋找,如果類型衝突,就按照名稱尋找
8、工廠模式的組成
-
接口
-
接口實現類
-
工廠類
提供生產產品(接口實現類對象等)的方法,一般是靜態公開方法
最好設計成單例模式
9、案例:如何注入較複雜的屬性(自定義、集合)
-
一個人有多輛車的時候
Person類與Car類
import java.util.List; public class Person { private String name; //姓名 private String gender; //性別 private int age; //年齡 private List<Car> cars; //這個人擁有多輛車; public Person() { } public Person(String name, String gender, int age, List<Car> cars) { this.name = name; this.gender = gender; this.age = age; this.cars = cars; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<Car> getCars() { return cars; } public void setCars(List<Car> cars) { this.cars = cars; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", gender='" + gender + '\'' + ", age=" + age + ", cars=" + cars + "}"; } } //Car類 public class Car { private String brand; //品牌 private String color; //顏色 private int price; //價格 public Car() { } public Car(String brand, String color, int price) { this.brand = brand; this.color = color; this.price = price; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override public String toString() { return "Car{" + "brand='" + brand + '\'' + ", color='" + color + '\'' + ", price=" + price + '}'; } }
-
使用xml文檔配置
使用list標籤和ref標籤的形式,表述集合
或根據id的值自動注入
<bean id="car1" class="org.westos.domain.Car"> <property name="brand" value="BENZ"/> <property name="color" value="黑色"/> <property name="price" value="400000"/> </bean> <bean id="car2" class="org.westos.domain.Car"> <property name="brand" value="BMW"/> <property name="color" value="紅色"/> <property name="price" value="300000"/> </bean> <bean id="myP2" class="org.westos.domain.Person"> <property name="name" value="張三"/> <property name="gender" value="男"/> <property name="age" value="23"/> <property name="cars"> <list> <ref bean="car1"/> <ref bean="car2"/> </list> </property> </bean>
-
使用註解方式注入
-
@Resource:按照名稱找到然後自動注入
嚴謹,但是沒有Autowire靈活
-
@Autowire:按照類型找注到然後自動注入
更加的靈活,當以多態接口的形式,會找該接口的所有的實現類的對象,更加的靈活
-
@Autowire與@Qualify結合
先按照類型去尋找,如果類型衝突,就按照名字去找。
@Resource private List<Car> myCars1; @Bean("myP1") public Person getMyPerson() { return new Person("范冰冰", "女", 20, myCars1); } @Bean("myCars1") public List<Car> getMyCars1() { List myCars1 = new ArrayList<Car>(); myCars1.add(getCar3()); myCars1.add(getCar4()); return myCars1; } @Bean("car3") public Car getCar3() { return new Car("TOYOTA", "白色", 80000); } @Bean("car4") public Car getCar4() { return new Car("NISSAN", "藍色色", 60000); }
-
-
測試類
public class PersonDomainTest { private static ApplicationContext context;//XML文檔連接對象 //@BeforeClass,測試類的初始化過程,僅僅初始化一次。 @BeforeClass public static void initContext(){ context = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void testGetPersonBean(){ Person p = (Person) context.getBean("myP2"); System.out.println(p); } }
-