测试需求分析与测试模型设计:从需求文档到用例的完整推导方法
2026/10/13 21:01:07
翻译版
英文版
重点掌握单例和原型就可以了
<beanid="user2"class="com.cike4.pojo.User"c:name="user2"c:age="20"scope="singleton"/>测试方法
@Testpublicvoidtest2(){ApplicationContextcontext=newClassPathXmlApplicationContext("userbeans.xml");// 这里申明了类型,就不需要强壮类型 User 类了Useruser=context.getBean("user2",User.class);Useruser2=context.getBean("user2",User.class);System.out.println(user==user2);}bean共享同一个实例化对象,所以返回true
每次从容器中get的时候,都会产生一个新的对象
<beanid="user2"class="com.cike4.pojo.User"c:name="user2"c:age="20"scope="prototype"/>测试方法
@Testpublicvoidtest2(){ApplicationContextcontext=newClassPathXmlApplicationContext("userbeans.xml");// 这里申明了类型,就不需要强壮类型 User 类了Useruser=context.getBean("user2",User.class);Useruser2=context.getBean("user2",User.class);System.out.println(user.hashCode());System.out.println(user2.hashCode());System.out.println(user==user2);}两个获取的对象不相等,hash也不一样,因为实例化不一样
request、session、application、这些只能在web开发中使用
官方解释:
https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-factory-scopes https://docs.spring.io/spring-framework/reference/core/beans/factory-scopes.html在Spring中有三种装配的方式:
环境搭建:一个人有两个宠物
byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应beanid
<beanid="cat"class="com.cike5.pojo.Cat"/><beanid="dog"class="com.cike5.pojo.Dog"/><!-- byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应beanid --><beanclass="com.cike5.pojo.People"id="people"autowire="byName"><propertyname="name"value="cike_y"/></bean><beanclass="com.cike5.pojo.Cat"/><beanclass="com.cike5.pojo.Dog"/><!-- byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean 弊端,类型全局为一才会自动装配 --><beanclass="com.cike5.pojo.People"id="people"autowire="byType"><propertyname="name"value="cike_y"/></bean>小结:
官方解释:
https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-autowired-annotation https://docs.spring.io/spring-framework/reference/core/beans/annotation-config/autowired.htmlJDK1.5支持的注解,Spring2.5就支持注解了!
The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML.
要使用注解须知:
<context:annotation-config/>干净并且完整的xml配置:
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><context:annotation-config/></beans>官方文档:
https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-annotation-config https://docs.spring.io/spring-framework/reference/core/beans/annotation-config.html https://docs.spring.io/spring-framework/reference/core/beans/annotation-config/autowired.html在类的属性上面进行注释
publicclassPeople{@AutowiredprivateCatcat;@AutowiredprivateDogdog;privateStringname;publicCatgetCat(){returncat;}publicDoggetDog(){returndog;}}测试方法
@Testpublicvoidtest(){// 创建Spring容器对象ApplicationContextcontext=newClassPathXmlApplicationContext("beans.xml");// 获取bean对象Peoplepeople=context.getBean("people",People.class);people.getCat().shout();people.getDog().shout();}@Nullable字段标记了这个注解,说明这个字段可以为null可以这样子写,让name值为空
private@NullableStringname;查看Autowired的注解接口,可以看见默认值是true
public@interfaceAutowired{/** * Declares whether the annotated dependency is required. * <p>Defaults to {@code true}. */booleanrequired()defaulttrue;}也可以这样子写
publicclassPeople{// 如果显示定义了Autowired 的 required属性为false,说明这个对象可以为null,否则不允许为空@Autowired(required=false)privateCatcat;@Autowired(required=false)privateDogdog;可以进行显示定义进行自动装配
@Qualifier("dog11")privateDogdog;beans.xml 中的容器id为dog11,可以进行对应
<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--开启注解的支持--><context:annotation-config/><bean id="dog11"class="com.cike5.pojo.Dog"/><bean id="cat"class="com.cike5.pojo.Cat"/><bean id="people"class="com.cike5.pojo.People"/></beans>@Autowired和@Qualifier的区别:
支持隐式自动装配、也支持显示定义,相当于@Autowored和@Quailifier的结合体
publicclassPeople{// 隐式定义、自动装配@ResourceprivateCatcat;publicclassPeople{// 显示定义指定容器id、自动装配@Resource(name="cat")privateCatcat;小结:
@Resource和 @Autowired的区别: