Spring测试

本文详细介绍使用Spring框架进行项目开发的基本步骤,包括项目架构搭建、核心配置文件spring.xml的设置、User类定义、UserDao及其实现类UserDaoImpl的创建、UserService接口与实现类UserServiceImpl的交互,以及通过TestUser类进行测试验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用Spring框架进行开发的时候,需要怎么做,下面我来模拟一下!

 基本的项目架构如下图所示:

spring.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		        http://www.springframework.org/schema/beans/spring-beans.xsd
		        http://www.springframework.org/schema/context
		        http://www.springframework.org/schema/context/spring-context.xsd
		        http://www.springframework.org/schema/mvc
		        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <import resource="spring-*.xml"></import>

</beans>

User类

package com.jie.domain;

public class User {
    private int id;
    private String name;
    private String address;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public User() {
    }
}

 

UserDao接口

package com.jie.dao;

import com.jie.domain.User;

import java.util.List;

public interface UserDao {
    public void hello();
    public void hello(int id);
    public void hello(User user);
    public void hello(List list);

}

UserDaoImpl实现类

package com.jie.dao.impl;

import com.jie.dao.UserDao;
import com.jie.domain.User;

import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public void hello() {

        System.out.println("This is UserDaoImpl.class to say 'Hello World'");
    }

    @Override
    public void hello(int id) {
        System.out.println("This is UserDaoImpl.class to say 'Hello World' and my ID--->"+id);
    }

    @Override
    public void hello(User user) {
        System.out.println("This is UserDaoImpl.class to say 'Hello World'-----user====>"+user.getName());
    }

    @Override
    public void hello(List list) {
        System.out.println("This is UserDaoImpl.class to say 'Hello World'---->"+list.get(0));
    }
}

UserService接口类

package com.jie.service;

import com.jie.domain.User;

import java.util.List;

public interface UserService {
    public void hello();
    public void hello(int id);
    public void hello(User user);
    public void hello(List list);
}

UserServiceImpl实现类

package com.jie.service.impl;

import com.jie.dao.impl.UserDaoImpl;
import com.jie.domain.User;
import com.jie.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDaoImpl userDao;
    private List list;
    @Override
    public void hello() {
        userDao.hello();
    }

    @Override
    public void hello(int id) {
        userDao.hello(id);
    }

    @Override
    public void hello(User user) {
        userDao.hello(user);
    }

    @Override
    public void hello(List list) {
        userDao.hello(this.list);
    }

    public void setUserDao(UserDaoImpl userDao) {
        this.userDao = userDao;
    }

    public void setList(List list) {
        this.list = list;
    }

    public List getList() {
        return list;
    }
}

spring的其他配置文件

spring-domain.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		        http://www.springframework.org/schema/beans/spring-beans.xsd
		        http://www.springframework.org/schema/context
		        http://www.springframework.org/schema/context/spring-context.xsd
		        http://www.springframework.org/schema/mvc
		        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <bean class="com.jie.domain.User" id="user">
        <property name="name" value="xiaojiejie"></property>
    </bean>

</beans>

spring-dao.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		        http://www.springframework.org/schema/beans/spring-beans.xsd
		        http://www.springframework.org/schema/context
		        http://www.springframework.org/schema/context/spring-context.xsd
		        http://www.springframework.org/schema/mvc
		        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<bean id="userDao" class="com.jie.dao.impl.UserDaoImpl"></bean>

</beans>

spring-service.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		        http://www.springframework.org/schema/beans/spring-beans.xsd
		        http://www.springframework.org/schema/context
		        http://www.springframework.org/schema/context/spring-context.xsd
		        http://www.springframework.org/schema/mvc
		        http://www.springframework.org/schema/mvc/spring-mvc.xsd"
><!--default-autowire="byName"-->

<!--
autowire 属性值的自动装配
         默认是no(不装配)
         byName通过name属性名装配
         byType通过类型装配
         default跟随上级的特性
 -->
<bean id="userService" class="com.jie.service.impl.UserServiceImpl" autowire="no">

    <property name="userDao" ref="userDao"></property>

    <property name="list">
        <list>
            <value>剑侠情缘</value>
            <value>葬剑山庄</value>
        </list>
    </property>
</bean>

</beans>

TestUser测试类

package com.jie;

import com.jie.domain.User;
import com.jie.service.UserService;
import com.jie.service.impl.UserServiceImpl;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class TestUser {

    @Test
    public void test1(){
        BeanFactory factory=new ClassPathXmlApplicationContext("/spring.xml");
        UserServiceImpl userService=(UserServiceImpl) factory.getBean("userService");
        userService.hello();
        userService.hello(22);
        User user=(User)factory.getBean("user");
        userService.hello(user);
        List list  =  userService.getList();
        userService.hello(list);
    }
}

结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值