Java EE---Spring框架创建Account小项目

使用Spring建立项目Account(初识dao、service、controller三者关系)

  • 题目
    1、使用Spring IOC/DI 模拟添加账户功能,程序设计涉及:
    (1)Account类:账户类,包括属性name、id,并为所有属性生成get和set方法。
    (2)AccountDao类:模拟数据库访问,方法void save(Account a)实现打印输出“添加的用户信息为:name=***,id=***”。
    (3)AccountService类:模拟业务逻辑访问,包含属性对象AccountDao dao方法void save(Account a)通过调用AccountDao类中的save方法模拟完成用户信息的添加,并为属性生成set方法。
    (4)AccountController类:模拟控制层,包含属性对象AccountService service和方法void save(Account a)调用AccountService类中的save方法模拟完成用户信息的添加,并为属性生成set方法。
    (5)对以上(2)、(3)、(4)类的使用IOC容器实例化,对(1)类使用new实例化;并使用DI将AccountDao类对象注入进AccountService类中,将AccountService类对象注入进AccountController类中。
    (6)Test类测试,完成name=“刘” id=’”1999”的账户添加。

1. 创建项目
项目目录

2. 导入jar包并应用

  • 选中工程右键如图
    在这里插入图片描述
  • 点击Add JARs在这里插入图片描述
  • 选择你放入lib文件夹下的jar包(需自己下载jar包并放入lib文件夹下)导入并应用即可

3. Account 类代码

package com.ioc.entity;

public class Account {
    
    
	String name;
	String id;
	public String getName() {
    
    
		return name;
	}
	public void setName(String name) {
    
    
		this.name = name;
	}
	public String getId() {
    
    
		return id;
	}
	public void setId(String id) {
    
    
		this.id = id;
	}
	
}

4. Account Dao类代码

package com.ioc.dao;

import com.ioc.entity.Account;

public class AccountDao {
    
    
	public  void save (Account a) {
    
    
		System.out.println("添加用户信息为:name="+a.getName()+"id="+a.getId());
	}
}


5.AccountService类代码

package com.ioc.service;

import com.ioc.dao.AccountDao;
import com.ioc.entity.Account;

public class AccountService {
    
    
	AccountDao dao;

	public AccountDao getDao() {
    
    
		return dao;
	}

	public void setDao(AccountDao dao) {
    
    
		this.dao = dao;
	}
	public void save(Account a) {
    
    
		dao.save(a);
	}
	
}

6.AccountController类代码

package com.ioc.controller;

import com.ioc.entity.Account;
import com.ioc.service.AccountService;

public class AccountController {
    
    
	AccountService service;

	public AccountService getService() {
    
    
		return service;
	}

	public void setService(AccountService service) {
    
    
		this.service = service;
	}
	public void save(Account a) {
    
    
		service.save(a);
	}
	
}

7.test类代码

package com.ioc.test;

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

import com.ioc.controller.AccountController;
import com.ioc.entity.Account;

public class test {
    
    
	public static void main(String [] args) {
    
    
		//意义:
		ApplicationContext context = new FileSystemXmlApplicationContext("src/applicationContext.xml") ;
		AccountController c=(AccountController) context.getBean("c1");
		Account a =new Account();
		a.setName("刘");
		a.setId("1999");
		c.save(a);
	}
}


8.applicationContext.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 使用spring来创建对象,在spring中这些都会成为Bean
bean相当于new了一个对象=========Hello h2 =new hello();
id=变量名
class=new的对象
property标签相当于给对象中的属性设置一个值=======h2.set("****");

 -->

<bean id="d1" class="com.ioc.dao.AccountDao"></bean>
<bean id="s1" class="com.ioc.service.AccountService">
<!--AccountDao类对象注入进AccountService类中 -->
	<property name="dao" ref="d1"/>
</bean>
<bean id="c1" class="com.ioc.controller.AccountController">
<!--AccountService类对象注入AccountController类中 -->
	<property name="service" ref="s1"/>
</bean>
</beans>

9.输出结果
在这里插入图片描述
笔记

  • Spring ioc的核心思想是将所有类的控制权转交到xml文件中,减少各个类之间的耦合度,使程序更加健壮。
  • xml文件的作用是它可以为每一个在xml文件登记过(bean过)的类初始化对象并赋值,也就是实例化和装配。然后根据各个类的需要给予其他类的对象然后销毁。
  • 关于Account、AccountDao、AccountService、AccountController之间的关系:Account类是负责最基础的创建基本的类属性;而dao层则是逻辑层,其中大部分代码都在这里实现;Service层是服务层,它会定义一个dao的属性并继承dao的属性;而Controller层是控制层,在xml文件中基本上是获取Controller层的一个对象再逐层控制service、dao层。
  • 且每一个类定义一个属性都要为其设置getter和setter方法。
  • xml文件是配置文件,它主要的属性是beans,其子属性bean的作用是实例化对象,其子子属性value是为bean实例化的类赋值。
  • 个人认为控制反转(IoC)就是将各个类创建实例化的权限交给xml文件来使类被动创建对象。依赖注入(DI)就是资源注入,通俗来讲就是赋值。

猜你喜欢

转载自blog.csdn.net/qq_46152664/article/details/123154505