一、项目案例(xml)

使用三层架构进行用户的插入操作.
界面层, 业务逻辑层, 数据访问层(模拟).

Spring会接管三层架构中哪些对象的创建?界面层的对象,业务逻辑层的对象,数据访问层的对象.

非Spring接管下的三层项目构建:
实体类
com.bjpowernode.pojo Users
数据访问层
com.bjpowernode.dao UsersMapper.java(接口)
UsersMapperImpl.java(实现类)
业务逻辑层
com.bjpowernode.service UsersService.java(接口)
UsersServiceImpl.java(实现类 )
界面层
com.bjpowernode.controller UsersController.java

非Spring接管下的三层项目构建:

Spring项目案例 - 图1
pojo包

  1. public class Users {
  2. private int uid;
  3. private String uname;
  4. private int uage;
  5. public int getUid() {
  6. return uid;
  7. }

dao包(模拟)

  1. public interface UsersMappr {
  2. //增加用户
  3. int insert(Users u );
  4. }
  1. public class UsersMapperImpl implements UsersMappr {
  2. @Override
  3. public int insert(Users u) {
  4. System.out.println(u.getUname()+"用户增加成功!");
  5. return 1;
  6. }
  7. }

service包下

  1. public interface UsersService {
  2. //增加用户
  3. int insert(Users users);
  4. }
  1. /**
  2. * 业务逻辑层的实现类
  3. */
  4. public class UsersServiceImpl implements UsersService {
  5. //切记切记:在所有的业务逻辑层中都必定有数据访问层的对象
  6. private UsersMappr usersMappr = new UsersMapperImpl();
  7. @Override
  8. public int insert(Users users) {
  9. //添加更复杂的业务,但是我们现在没有复杂业务
  10. return usersMappr.insert(users);
  11. }
  12. }

controller包

  1. /**
  2. * 界面层
  3. */
  4. public class UsersController {
  5. //如何去访问业务逻辑层,就是创建对象
  6. //切记切记:所有的界面层都会有业务逻辑层的对象
  7. public UsersService usersService = new UsersServiceImpl();
  8. //界面层的功能实现,对外提供访问的功能
  9. public int insert(Users users){
  10. return usersService.insert(users);
  11. }
  12. }

Test包

  1. public class MyTest {
  2. @Test
  3. public void testInsertUsers(){
  4. //?创建谁的对象 创建UsersController对象
  5. UsersController usersController = new UsersController();
  6. int num = usersController.insert(new Users(100,"张三",22));
  7. System.out.println(num);
  8. }
  9. }

Spring接管下的三层项目构建:

YEG`%(385(6XZ42GAM7TBYM.png
pojo包

  1. public class Users {
  2. private int uid;
  3. private String uname;
  4. private int uage;
  5. //set() get() tpString()
  6. }

resources

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <!--创建各种对象-->
  6. <!--创建数据访问层的对象-->
  7. <bean id="uMapper" class="com.bjpowernode.dao.UsersMapperImpl">
  8. </bean>
  9. <!--创建业务逻辑层-->
  10. <bean id="uService" class="com.bjpowernode.service.impl.UsersServiceImpl">
  11. <property name="usersMappr" ref="uMapper"></property>
  12. </bean>
  13. <!--创建界面层对象-->
  14. <bean id="uController" class="com.bjpowernode.controller.UsersController">
  15. <property name="usersService" ref="uService"></property>
  16. </bean>
  17. </beans>

dao包

  1. public interface UsersMappr {
  2. //增加用户
  3. int insert(Users u );
  4. }
  1. /**
  2. * 数据访问层的实现类
  3. */
  4. public class UsersMapperImpl implements UsersMappr {
  5. @Override
  6. public int insert(Users u) {
  7. System.out.println(u.getUname()+"用户增加成功!");
  8. return 1;
  9. }
  10. }

service包

  1. public interface UsersService {
  2. //增加用户
  3. int insert(Users users);
  4. }
  1. /**
  2. * 业务逻辑层的实现类
  3. */
  4. public class UsersServiceImpl implements UsersService {
  5. //切记切记:在所有的业务逻辑层中都必定有数据访问层的对象
  6. private UsersMappr usersMappr; //= new UsersMapperImpl();
  7. //交给Spring去依赖注入值,必须提供setXXX()方法
  8. public void setUsersMappr(UsersMappr usersMappr) {
  9. this.usersMappr = usersMappr;
  10. }
  11. @Override
  12. public int insert(Users users) {
  13. //添加更复杂的业务,但是我们现在没有复杂业务
  14. return usersMappr.insert(users);
  15. }
  16. }

controller包

  1. /**
  2. * 界面层
  3. */
  4. public class UsersController {
  5. //如何去访问业务逻辑层,就是创建对象
  6. //切记切记:所有的界面层都会有业务逻辑层的对象
  7. public UsersService usersService;// = new UsersServiceImpl();
  8. //交给Spring去注入值,必须提供setXXX()方法
  9. public void setUsersService(UsersService usersService) {
  10. this.usersService = usersService;
  11. }
  12. //界面层的功能实现,对外提供访问的功能
  13. public int insert(Users users){
  14. return usersService.insert(users);
  15. }
  16. }

test包

  1. public class MyTest {
  2. @Test
  3. public void testInsertUsers(){
  4. //创建容器并启动
  5. ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");
  6. //取出对象
  7. UsersController usersController =(UsersController) ac.getBean("uController");
  8. //测试功能
  9. int num=usersController.insert(new Users(200,"王五",24));
  10. System.out.println(num);
  11. }
  12. }

二、项目案例(注解)

image.png
image.png

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. 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">
  6. <!--只要是基于注解开发,必须扫描-->
  7. <context:component-scan base-package="com.bjpowernode"></context:component-scan>
  8. </beans>
  1. public class Users {
  2. private int uid;
  3. private String uname;
  4. private int uage;
  5. //set() get() toString
  6. }

dao包

  1. public interface UsersMappr {
  2. //增加用户
  3. int insert(Users u );
  4. }
  1. /**
  2. * 数据访问层的实现类
  3. */
  4. @Repository //就是交给spring框架去创建数据访问层的对象
  5. public class UsersMapperImpl implements UsersMappr {
  6. @Override
  7. public int insert(Users u) {
  8. System.out.println(u.getUname()+"用户增加成功!");
  9. return 1;
  10. }
  11. }

service包

  1. public interface UsersService {
  2. //增加用户
  3. int insert(Users users);
  4. }
  1. /**
  2. * 业务逻辑层的实现类
  3. */
  4. @Service //交给spring创建业务逻辑层的对象
  5. public class UsersServiceImpl implements UsersService {
  6. //切记切记:在所有的业务逻辑层中都必定有数据访问层的对象
  7. @Autowired
  8. private UsersMappr usersMappr;// = new UsersMapperImpl();
  9. @Override
  10. public int insert(Users users) {
  11. //添加更复杂的业务,但是我们现在没有复杂业务
  12. return usersMappr.insert(users);
  13. }
  14. }

controller包

  1. /**
  2. * 界面层
  3. */
  4. @Controller //交给Spring去创建对象
  5. public class UsersController {
  6. //如何去访问业务逻辑层,就是创建对象
  7. //切记切记:所有的界面层都会有业务逻辑层的对象
  8. @Autowired
  9. public UsersService usersService ; //= new UsersServiceImpl();
  10. //界成层的功能实现,对外提供访问的功能
  11. public int insert(Users users){
  12. return usersService.insert(users);
  13. }
  14. }

测试类

  1. package com.bjpowernode.test;
  2. import com.bjpowernode.controller.UsersController;
  3. import com.bjpowernode.pojo.Users;
  4. import org.junit.Test;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. /**
  8. *
  9. */
  10. public class MyTest {
  11. @Test
  12. public void testInsertUsers(){
  13. //创建并启动容器
  14. ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
  15. //取出UsersController对象
  16. UsersController usersController = (UsersController) ac.getBean("usersController");
  17. usersController.insert(new Users(100,"haha",25));
  18. }
  19. }

改进配置文件

image.png

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. 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">
  6. <!--批量导入其它配置文件-->
  7. <import resource="applicationContext_*.xml"></import>
  8. </beans>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. 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">
  6. <!--只要是基于注解的开发,必须包扫描-->
  7. <context:component-scan base-package="com.bjpowernode.dao"></context:component-scan>
  8. </beans>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. 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">
  6. <!--只要是基于注解的开发,必须包扫描-->
  7. <context:component-scan base-package="com.bjpowernode.service"></context:component-scan>
  8. </beans>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. 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">
  6. <!--只要是基于注解的开发,必须包扫描-->
  7. <context:component-scan base-package="com.bjpowernode.controller"></context:component-scan>
  8. </beans>