Spring(二)--Spring入门案例
Spring入门案例
1.需要的实体类
2.需要的接口和实现类
3.需要的service和实现类
/**
* service层的作用
* 在不改变dao层代码的前提下,增加业务逻辑操作
*/
public class StudentServiceImpl implements StudentService { public StudentServiceImpl(){
System.out.println("StudentServiceImpl的无参构造");
}
//创建出dao层实例 存在耦合 StudentDao dao=new StudentDaoImpl();
StudentDao dao;
public void sleep() {
System.out.println("开始记录日志"); //系统级业务
dao.sleep(); //主业务
System.out.println("结束记录日志"); //系统级业务 }
public String eat() {
System.out.println("开始记录日志"); //系统级业务
String result= dao.eat();//主业务
System.out.println("结束记录日志"); //系统级业务
return result;
} public StudentDao getDao() {
return dao;
} //DI 依赖注入
public void setDao(StudentDao dao) {
this.dao = dao;
}
}
4.需要的核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.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"> <!--管理dao层的对象-->
<bean id="studentDao" class="com.xdf.dao.StudentDaoImpl"/> <!--管理service层的对象-->
<bean id="studentService" class="com.xdf.service.StudentServiceImpl">
<!--给指定的dao属性赋值-->
<property name="dao" ref="studentDao"/>
</bean> <!--创建student类的bean
xml文件中所有的bean 都是单例的
默认scope="singleton"
scope="prototype" 设置原型 默认也是延迟加载 lazy-init="true" 设置延迟加载
-->
<bean id="student" class="com.xdf.bean.Student" lazy-init="true">
<property name="id" value="20"/>
<property name="name" value="小黑"/>
</bean>
</beans>
5.需要的测试类
public class StudentDemo { /**
* 之前的方式
*/
@Test
public void test01(){
//实例化service层对象
StudentService service=new StudentServiceImpl();
service.sleep();
}
/**
* Spring容器的工作:
* 01.创建各种bean对象
* 02.管理bean之间的关系
*
*ApplicationContext接口有个实现类
* ClassPathXmlApplicationContext("spring.xml")
* 特点:
* spring.xml文件中配置的所有bean都实例化了!
*/
@Test
public void test02(){
//通过spring容器来 实例化service层对象
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
System.out.println("==========================================");
//spring.xml文件中bean的id
StudentService service= (StudentService) context.getBean("studentService");
service.sleep();
} /**
* 实现按需加载 不是把核心文件中配置的所有bean都实例化!
*/
@Test
public void test03(){
//通过spring容器来 实例化service层对象
XmlBeanFactory context=new XmlBeanFactory(new ClassPathResource("spring.xml"));
System.out.println("==========================================");
//spring.xml文件中bean的id
StudentService service= (StudentService) context.getBean("studentService");
service.sleep();
} /**
* 从某个位置获取核心配置文件
*/
@Test
public void test04(){
//通过spring容器来 实例化service层对象
ApplicationContext context=new FileSystemXmlApplicationContext("E:/spring.xml");
System.out.println("==========================================");
//spring.xml文件中bean的id
StudentService service= (StudentService) context.getBean("studentService");
service.sleep();
} /**
* 验证单例模式
* 所有由spring容器创建出来的对象 默认都是单例的
*/
@Test
public void test05(){
//通过spring容器来 实例化service层对象
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
System.out.println("==========================================");
//spring.xml文件中bean的id
Student stu1= (Student) context.getBean("student");
System.out.println(stu1);
Student stu2= (Student) context.getBean("student");
System.out.println(stu1==stu2);
}
}
还会继续更新的!下次见咯!
Spring(二)--Spring入门案例的更多相关文章
- Spring学习笔记(一)—— Spring介绍及入门案例
一.Spring概述 1.1 Spring是什么 Spring是一个开源框架,是于2003年兴起的一个轻量级的Java开发框架, 由Rod Johnson 在其著作<Expert one on ...
- SSM-Spring-01:Spring的概念+入门案例
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- Spring 提起Spring,就会想到企业级框架这个词 企业级系统: 1.大规模:用户数量多,数据规模庞大, ...
- Python爬虫Scrapy(二)_入门案例
本章将从案例开始介绍python scrapy框架,更多内容请参考:python学习指南 入门案例 学习目标 创建一个Scrapy项目 定义提取的结构化数据(Item) 编写爬取网站的Spider并提 ...
- Spring(二)之入门示例
任何编程技术,特别是入门示例,通常都是Hello World,在这里我也遵循这个业界公认的原则. 这里我使用的maven项目,大家如果想要演示,建议使用Eclipse(含maven插件)或Idea(含 ...
- Spring(二)Bean入门
一.BeanFactory介绍 1.1.Bean: 在Spring技术中是基于组件的 最基本了是最常用的单元 其实实例保存在Spring的容器当中 Bean通常被定义在配置文件当中,Bean实例化由S ...
- spring的IOC入门案例
步骤: 一,导入jar 二,创建类,在类里创建方法 三,创建Spring配置文件,配置创建类 四,写代码测试对象创建
- quartz(3)--spring整合quartz入门案例
第一步:导入jar <!-- quartz --> <dependency> <groupId>org.quartz-scheduler</groupId&g ...
- spring配置mq入门案例
第一步:添加maven配置 <!-- mq --> <dependency> <groupId>org.springframework</groupId> ...
- Spring boot 官网学习笔记 - Spring Boot CLI 入门案例
安装CLI https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.1.1.RELEASE/spring-b ...
- Spring Boot 简单入门案例
第一:打开idea 找到spring Initializr 第二:点击Next 在点击下一步 找到web之后勾选第一个spring web 就完成了 在写一个类 点击运行 结果如下:
随机推荐
- JAVA如何跳出多层循环
1. break.continue.return 的区别: break默认是跳出最里层的循环,也就是break所在的最近的那层循环 continue是终止本次循环,继续下次循环 return 结束当前 ...
- AHOI/HNOI2017 礼物
题目链接:戳我 对于题目中给的式子:(大家暂且把\(y_i\)当作\(y_{i+k}\)来看啦qwq) \(\sum_{i=1}^{n}(x_i-(y_i+c))^2\) \(=\sum_{i=1}^ ...
- 【Red Hat Linux基础】 磁盘分区详细教程
https://blog.51cto.com/sunjie123/1687535 Linux中添加新硬盘后对硬盘的分区以及挂载 https://www.linuxidc.com/Linux/2018- ...
- HDU2433—Travel (BFS,最短路)
Travel Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- JavaWeb_客户端相对/绝对路径和服务器端路径
客户端的绝对路径和相对路径 相对路径:相对与某个基准目录的路径,在同一根目录下各子目录文件之间的相互引用, 绝对路径:指目录下的绝对位置,直接到的目标位置 @charset "UTF-8&q ...
- 添加一个静态JAVA库
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # Build all java files in the java subdirectory L ...
- 微信小程序-获取当前位置
在 app.json 里面增加 permission 属性配置(小游戏需在game.json中配置): "permission": { "scope.userLocati ...
- LeetCode 337. 打家劫舍 III(House Robber III)
题目描述 小偷又发现一个新的可行窃的地点. 这个地区只有一个入口,称为“根”. 除了根部之外,每栋房子有且只有一个父房子. 一番侦察之后,聪明的小偷意识到“这个地方的所有房屋形成了一棵二叉树”. 如果 ...
- JNI崩溃调试
JNI崩溃了,系统日志会打印堆栈信息,所以第一步就是取日志 adb shell logcat -v threadtime >>d:/log.txt 然后找到日志里面的关键字backtrac ...
- mysql 5.7安装方法
yum方式安装rpm包形式,安装mysql的方法: 方法一: 使用yum方式,下载后离线安装mysql的安装包 安装前,先使用命令查看,确定系统未安装mysql安装包.彻底清除之前安装的mysql安装 ...