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>
三层架构的项目案例
界面层--->业务逻辑层--->数据访问层
模拟三层实现增加用户的操作
实现步骤:
- 创建pojo包,添加实体类Users
public class Users {
private Integer id;
private String name;
private String address;
// 省略全参构造
// 省略getter/setter/toString...
}
- 数据访问层,创建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;
}
}
- 业务逻辑层,创建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);
}
}
- 界面层,创建controller包
- UsersController类
public class UsersController {
UsersService service = new UsersServiceImpl();
public void insert(Users users) {
int i = service.insertUser(users);
System.out.println(i);
}
}
- 测试类
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>