Spring02 三层架构

本文介绍了如何使用Spring框架创建系统类和自定义类型的对象,包括单例和多例的配置。通过一个三层架构的项目案例展示了从界面层到数据访问层的实现过程,强调了Spring对对象的生命周期管理和依赖注入的功能,以及如何通过XML配置和注解方式进行配置文件的拆分与整合。
摘要由CSDN通过智能技术生成

Spring创建系统类的对象

只要有类型就可以创建对象,无论是系统的还是自定义的类型
<!-- Thu Jan 01 08:00:01 CST 1970 -->
<bean class="java.util.Date" id="myDate">
	<property name="time" value="1000"/>
</bean>

Spring创建对象的作用域

创建对象可适用的范围:ApplicationContext 定义的块级范围内
Spring默认创建的对象是单例的,也可以设置为非单例的
<!--Spring容器在启动时就已经创建好了所有对象,是放入单例池的,取多少次都是同一个对象-->
<!--设置成非单例模式,取一次创建一个对象-->
<beans>
	<!--不写和添加 singleton 都是单例-->
	<bean class="java.util.Date" id="myDate" scope="singleton"/>

	<!--非单例模式-->
	<bean class="java.util.Date" id="myDate" scope="prototype"/>
</beans>

三层架构的项目案例

界面层--->业务逻辑层--->数据访问层
模拟三层实现增加用户的操作

实现步骤:

  1. 创建pojo包,添加实体类Users
public class Users {
	private Integer id;
	private String name;
	private String address;
	// 省略全参构造
	// 省略getter/setter/toString...
}
  1. 数据访问层,创建mapper包
  • UsersMapper.java接口
public interface UsersMapper {
	int insertUsers(Users users);
}
  • UsersMapper.xml实现功能
/*使用实现类模拟xml*/
public class UsersMapperImpl implements UsersMapper {
	@Override
	public int insertUsers(Users users) {
		System.out.println(users.getName() + "添加成功");
		return 1;
	}
}
  1. 业务逻辑层,创建service包
  • UsersService接口
public interface UsersService {
	int insertUser(Users users);
}
  • 添加impl包 UsersServiceImpl实现类
public class UsersServiceImpl implements UsersService {
	UsersMapper usersMapper = new UsersMapperImpl();
	
	@Override
	public int insertUser(Users users) {
		return usersMapper.insertUsers(users);
	}
}
  1. 界面层,创建controller包
  • UsersController类
public class UsersController {
	UsersService service = new UsersServiceImpl();
	
	public void insert(Users users) {
		
		int i = service.insertUser(users);
		System.out.println(i);
	}
}
  1. 测试类
public class MyTest {
	UsersController usersController = new UsersController();
	
	@Test
	public void testController() {
		usersController.insert(new Users(1, "牧羊", "青藏"));
	}
}

使用Spring接管三层架构项目

Spring解关界面测光, 业务逻辑层, 数据访问层对象的创建并完成依赖注入
在各个类中取消所有对象的创建,添加setter方法或者构造方法
  • UsersServiceImpl类
public class UsersServiceImpl implements UsersService {
	UsersMapper usersMapper;
	
	public void setUsersMapper(UsersMapper usersMapper) {
		this.usersMapper = usersMapper;
	}
	
	@Override
	public int insertUser(Users users) {
		return usersMapper.insertUsers(users);
	}
}
  • UsersController类
public class UsersController {
	private final UsersService service;
	
	public UsersController(UsersServiceImpl service) {
		this.service = service;
	}
	
	public void insert(Users users) {
		
		int i = service.insertUser(users);
		System.out.println(i);
	}
}
  • applicationContext.xml

<beans>
	<!--创建数据访问层对象-->
	<bean class="icu.sandink.mapper.UsersMapperImpl" id="usersMapper"/>

	<!--创建业务逻辑层对象-->
	<bean class="icu.sandink.service.impl.UsersServiceImpl" id="usersService">
		<property name="usersMapper" ref="usersMapper"/>
	</bean>

	<!--创建界面层对象-->
	<bean class="icu.sandink.controller.UsersController" id="usersController">
		<constructor-arg name="service" ref="usersService"/>
	</bean>
</beans>
  • 测试类
public class MyTest {
	ApplicationContext app;
	
	@Before
	public void before() {
		app = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	
	@Test
	public void test() {
		UsersController usersController = (UsersController) app.getBean("usersController");
		usersController.insert(new Users(1, "牧羊", "青藏"));
	}
}

Spring配置文件的拆分

由于项目越来越复杂,开发协作人员越来越多,一个配置文件极容易出错,所以要进行配置文件的拆分

拆分策略

  • 按层拆(掌握)
    • applicationContext_mapper.xml 创建数据访问层的对象
    • applicationContext_service.xml 创建业务逻辑层的对象
    • applicationContext_controller.xml 创建界面层的对象
<!--applicationContext_mapper.xml-->
<beans>
	<bean id="uMapper" class="icu.sandink.mapper.UsersMapperImpl"/>
	<bean id="bMapper" class="icu.sandink.mapper.BookMapperImpl"/>
</beans> 
<!--applicationContext_service.xml-->
<beans>
	<bean id="usersService" class="icu.sandink.service.impl.UsersServiceImpl">
		<property name="usersMapper" ref="uMapper"/>
	</bean>
	<bean id="bookService" class="icu.sandink.service.impl.BookServiceImpl">
		<property name="bookMapper" ref="bMapper"/>
	</bean>
</beans>
<!--applicationContext_controller.xml-->
<beans>
	<bean id="uController" class="icu.sandink.controller.UsersController">
		<property name="uService" ref="usersService"/>
	</bean>
	<bean id="bController" class="icu.sandink.controller.BookController">
		<property name="bService" ref="bookService"/>
	</bean>
</beans>
  • 按模块拆
    • applicationContext_users.xml 创建users类相关的对象
    • applicationContext_book.xml 创建book类相关的对象
<!--applicationContext_users.xml-->
<beans>
	<bean id="uMapper" class="icu.sandink.mapper.UsersMapperImpl"/>
	<bean id="usersService" class="icu.sandink.service.impl.UsersSeriveImpl">
		<property name="usersMapper" ref="uMapper"/>
	</bean>
	<bean id="uController" class="icu.sandink.controller.UsersController">
		<property name="uService" ref="usersService"/>
	</bean>
</beans>
<!--application_book.xml-->
<beans>
	<bean id="bMapper" class="icu.sandink.mapper.BookMapperImpl"/>
	<bean id="bookService" class="icu.sandink.service.impl.BookSeriveImpl">
		<property name="bookMapper" ref="bMapper"/>
	</bean>
	<bean id="bController" class="icu.sandink.controller.BookController">
		<property name="bService" ref="bookService"/>
	</bean>
</beans>

Spring配置文件的整合

  • 单个文件的导入

<beans>
	<import resource="applicationContext_mapper.xml"/>
	<import resource="applicationContext_service.xml"/>
	<import resource="applicationContext_controller.xml"/>
</beans>
  • 批量导入

<beans>
	<import resource="applicationContext_*.xml"/>
</beans>

基于注解的包扫描方式

  • 单个包扫描

<beans>
	<context:component-scan base-package="icu.sandink.controller"/>
	<context:component-scan base-package="icu.sandink.service.impl"/>
	<context:component-scan base-package="icu.sandink.mapper"/>
</beans>
  • 一次扫描多个包,多个包之间以逗号或分号或空格分隔

<beans>
	<context:component-scan base-package="icu.sandink.controller,icu.sandink.service.impl;icu.sandink.mapper"/>
</beans>
  • 扫描根包(不推荐)

<beans>
	<context:component-scan base-package="icu.sandink"/>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值