当前位置: 首页 > 图灵资讯 > 技术篇> 测试我们手写的myspring框架

测试我们手写的myspring框架

来源:图灵教育
时间:2023-06-08 09:21:54

引入我们的myspring框架

<!--        使用myspring框架需要引入框架-->        <dependency>            <groupId>org.myspringframework</groupId>            <artifactId>course15</artifactId>            <version>1.0.0</version>        </dependency>
1.代码汇总pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.powernode</groupId>    <artifactId>course16</artifactId>    <version>1.0-SNAPSHOT</version>    <dependencies><!--        使用myspring框架需要引入框架-->        <dependency>            <groupId>org.myspringframework</groupId>            <artifactId>course15</artifactId>            <version>1.0.0</version>        </dependency>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.13.2</version>            <scope>test</scope>        </dependency>    </dependencies>    <properties>        <maven.compiler.source>17</maven.compiler.source>        <maven.compiler.target>17</maven.compiler.target>    </properties></project>
com.powernode.myspring.beanVip
package com.powernode.myspring.bean;//测试myspring框架  68public class Vip {    private String name;    private int age;    private double height;    @Override    public String toString() {        return "Vip{" +                "name='" + name + '\'' +                ", age=" + age +                ", height=" + height +                '}';    }    public void setName(String name) {        this.name = name;    }    public void setAge(int age) {        this.age = age;    }    public void setHeight(double height) {        this.height = height;    }}
OrderDao
package com.powernode.myspring.bean;//测试myspring框架  68public class OrderDao {    public void insert(){        System.out.println(正在保存的信息);    }}
OrderService
package com.powernode.myspring.bean;//测试myspring框架  68public class OrderService {    private OrderDao orderDao;    public void setOrderDao(OrderDao orderDao) {        this.orderDao = orderDao;    }    public void generate(){        orderDao.insert();    }}
myspring.xml
<?xml version="1.0" encoding="UTF-8"?><beans>    <bean id="vip" class="com.powernode.myspring.bean.Vip">        <property name="name" value="jackson"></property>        <property name="age" value="30"></property>        <property name="height" value="1.83"></property>    </bean>    <bean id="orderDaoBean" class="com.powernode.myspring.bean.OrderDao"></bean>    <bean id="orderService" class="com.powernode.myspring.bean.OrderService">        <property name="orderDao" ref="orderDaoBean"/>    </bean></beans>
com.powernode.myspring.testMySpringTest
package com.powernode.myspring.test;import com.powernode.myspring.bean.OrderService;import org.junit.Test;import org.myspringframework.core.ApplicationContext;import org.myspringframework.core.ClassPathXmlApplicationContext;//测试myspring框架  68public class MySpringTest {    @Test    public void testMySpring(){        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("myspring.xml");        Object vip = applicationContext.getBean("vip");        System.out.println(vip);        OrderService orderService =(OrderService) applicationContext.getBean("orderService");        orderService.generate();    }}

上一篇:

异常处理

下一篇:

手写spring框架