vue+element+git+maven+springMVC+springBoot+MyBatis-电力监理生产业务管理系统-2.3 测试并配置spring framework


1.建立相关对象

1.1建立User

代码如下(示例):

package cn.lilinghui.jlemip.bean;

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

	public String getName() {
		return name;
	}

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

	public int getId() {
		return id;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}

1.2建立UserDao接口

代码如下(示例):

package cn.lilinghui.jlemip.dao;

public interface UserDao {
	public void save();
}

1.3建立UserDaoImpl实现

代码如下(示例):

package cn.lilinghui.jlemip.dao.impl;

import cn.lilinghui.jlemip.bean.User;
import cn.lilinghui.jlemip.dao.UserDao;

public class UserDaoImpl implements UserDao {

	public void save() {
		User user = new User();
		user.setName("llh");
		System.out.println("save..." + user.getName());
	}

}

1.4建立UserDaoTest测试程序

代码如下(示例):

package cn.lilinghui.jlemip.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.lilinghui.jlemip.dao.UserDao;
import cn.lilinghui.jlemip.dao.impl.UserDaoImpl;

public class UserDaoImplTest {
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserDao userDao = applicationContext.getBean(UserDaoImpl.class);
		userDao.save();
	}
}

2.配置spring

2.1配置applicationContext

代码如下(示例):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context.xsd
                    http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/tx
                    http://www.springframework.org/schema/tx/spring-tx.xsd
                    http://www.springframework.org/schema/aop
                    http://www.springframework.org/schema/aop/spring-aop.xsd">
	<bean id="userDao" class="cn.lilinghui.jlemip.dao.impl.UserDaoImpl"></bean>
</beans>

3.项目文件结构

在这里插入图片描述

4.测试结果

结果如下:

08:54:43.360 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7b3300e5
08:54:43.569 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 1 bean definitions from class path resource [applicationContext.xml]
08:54:43.665 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userDao'
save...llh

总结

以上就是今天要讲的内容,本文仅仅简单介绍了spring framework的使用、简单的配置及测试,接下来的章节,我们将引入更多详细的配置、说明及测试。

项目目录

vue+element+git+maven+springMVC+springBoot+MyBatis-电力监理生产业务管理系统 目录

©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页