导入jar包:junit5 Spring5.3.8
创建蓝色目录:
项目结构:
源代码:
Car.java
package cn.edu.nynu.entity;
public class Car {
@Override
public String toString() {
return "奔驰轿车";
}
}
House.java
package cn.edu.nynu.entity;
public class House {
@Override
public String toString() {
return "500平米大别墅";
}
}
Myclass。java
package cn.edu.nynu.entity;
import java.util.Date;
public class MyClass {
private String name;
private int age;
private Car car;
private House house;
private Date hiredate;//
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setCar(Car car) {
this.car = car;
}
public void setHouse(House house) {
this.house = house;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
@Override
public String toString() {
return "MyClass{" +
"name='" + name + '\'' +
", age=" + age +
", car=" + car +
", house=" + house +
", hiredate=" + hiredate +
'}';
}
}
IocTest.java
package cn.edu.nynu.test;
import java.sql.Date;
import cn.edu.nynu.entity.MyClass;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Junit5Test {
static ClassPathXmlApplicationContext ioc = null;
@BeforeAll
public static void before()
{
ioc = new ClassPathXmlApplicationContext("ioc.xml");
System.out.println("**********Ioc容器开启************");
}
@AfterAll
public static void after()
{
System.out.println("***********Ioc容器关闭***********");
}
@Test
public void test1()
{
var m1 = ioc.getBean("m1", MyClass.class);
System.out.println("" + m1);
}
@Test
public void test2()
{
var m2 = ioc.getBean("m2", MyClass.class);
System.out.println(" " + m2);
}
@Test
public void test3() {
var m3 = ioc.getBean("m3", MyClass.class);
System.out.println(" " + m3);
}
@Test
public void test4() {
var m4 = ioc.getBean("m4", MyClass.class);
System.out.println(" " + m4);
}
@Test
public void test5() {
var m5 = ioc.getBean("m5", MyClass.class);
System.out.println(" " + m5);
}
@Test
public void test6() {
var date = Date.valueOf("2010-1-10");
System.out.println(" " + date);
}
@Test
public void test7()
{
MyClass mm1 = ioc.getBean("mm", MyClass.class);
System.out.println(" "+mm1);
}
}
ioc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:my.properties"/>
<bean id="car" class="cn.edu.nynu.entity.Car" lazy-init="true"/>
<bean id="house" class="cn.edu.nynu.entity.House" lazy-init="true"/>
<bean id="m1" class="cn.edu.nynu.entity.MyClass" lazy-init="true">
<property name="name" value="${xx.name}"/>
<property name="age" value="${xx.age}"/>
<property name="car" ref="car"/>
<property name="house" ref="house"/>
</bean>
<!--引用P命名空间namespace-->
<bean id="m2" class="cn.edu.nynu.entity.MyClass" lazy-init="true" p:name="xxx" p:age="21" p:car-ref="car"
p:house-ref="house"/>
<bean id="m3" class="cn.edu.nynu.entity.MyClass" lazy-init="true" p:name="${xx.name}" p:age="${xx.age}"/>
<bean id="m4" class="cn.edu.nynu.entity.MyClass" lazy-init="true" p:name="${xx.name}" p:house-ref="house">
<property name="car" ref="car"/>
<property name="age" value="${xx.age}"/>
</bean>
<!--在属性内部临时创建Bean对象-->
<bean id="m5" class="cn.edu.nynu.entity.MyClass" lazy-init="true" p:name="xxxxx" p:age="23">
<property name="car">
<bean class="cn.edu.nynu.entity.Car" lazy-init="true"/>
</property>
<property name="house">
<bean class="cn.edu.nynu.entity.House" lazy-init="true"/>
</property>
<property name="hiredate">
<bean class="java.sql.Date" lazy-init="true" factory-method="valueOf">
<constructor-arg name="s" value="2010-1-10"/>
</bean>
</property>
</bean>
</beans>
a.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="car" class="cn.edu.nynu.entity.Car" lazy-init="true"/>
</beans>
b.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="house" class="cn.edu.nynu.entity.House" lazy-init="true"/>
</beans>
c.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:my.properties"/>
</beans>
abc.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--合并a.xml 和 b.xml 及 c.xml到当前Ioc容器-->
<import resource="a.xml"/>
<import resource="b.xml"/>
<import resource="c.xml"/>
<bean id="mm" class="cn.edu.nynu.entity.MyClass">
<property name="car" ref="car"/>
<property name="house" ref="house"/>
<property name="name" value="${xx.name}"/>
</bean>
</beans>
EachTest.java
package cn.edu.nynu.test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepeatedTest;
public class EachTest {
@BeforeAll
public static void before() {
System.out.println(" **** 开始 ****");
System.out.println();
}
@AfterAll
public static void after() {
System.out.println();
System.out.println(" **** 结束 ****");
}
static int x = 0;
@RepeatedTest(5)
public void test2() {
x++;
System.out.println(" " + x * x);
}
@BeforeEach
public void aa() {
System.out.println(" **** 运行一次前 ****");
}
@AfterEach
public void bb() {
System.out.println(" **** 运行一次后 ****");
}
}
RepeatTest.java
package cn.edu.nynu.test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
public class RepeatTest {
@BeforeAll
public static void before() {
System.out.println(" **** 开始 ****");
}
@AfterAll
public static void after() {
System.out.println(" **** 结束 ****");
}
@Test
public void test() {
System.out.println(" **** hello,spring! ****");
}
@RepeatedTest(5)
public void test1() {
System.out.println(" **** hello,spring! ****");
}
static int x = 0;
@RepeatedTest(5)
public void test2() {
x++;
System.out.println(" " + x * x);
}
}
1-7test运行效果图:
RepeatTest运行效果图:
EachTest运行效果图: