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入门案例的更多相关文章

  1. Spring学习笔记(一)—— Spring介绍及入门案例

    一.Spring概述 1.1 Spring是什么 Spring是一个开源框架,是于2003年兴起的一个轻量级的Java开发框架, 由Rod Johnson 在其著作<Expert one on ...

  2. SSM-Spring-01:Spring的概念+入门案例

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- Spring 提起Spring,就会想到企业级框架这个词 企业级系统: 1.大规模:用户数量多,数据规模庞大, ...

  3. Python爬虫Scrapy(二)_入门案例

    本章将从案例开始介绍python scrapy框架,更多内容请参考:python学习指南 入门案例 学习目标 创建一个Scrapy项目 定义提取的结构化数据(Item) 编写爬取网站的Spider并提 ...

  4. Spring(二)之入门示例

    任何编程技术,特别是入门示例,通常都是Hello World,在这里我也遵循这个业界公认的原则. 这里我使用的maven项目,大家如果想要演示,建议使用Eclipse(含maven插件)或Idea(含 ...

  5. Spring(二)Bean入门

    一.BeanFactory介绍 1.1.Bean: 在Spring技术中是基于组件的 最基本了是最常用的单元 其实实例保存在Spring的容器当中 Bean通常被定义在配置文件当中,Bean实例化由S ...

  6. spring的IOC入门案例

    步骤: 一,导入jar 二,创建类,在类里创建方法 三,创建Spring配置文件,配置创建类 四,写代码测试对象创建

  7. quartz(3)--spring整合quartz入门案例

    第一步:导入jar <!-- quartz --> <dependency> <groupId>org.quartz-scheduler</groupId&g ...

  8. spring配置mq入门案例

    第一步:添加maven配置 <!-- mq --> <dependency> <groupId>org.springframework</groupId> ...

  9. Spring boot 官网学习笔记 - Spring Boot CLI 入门案例

    安装CLI https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.1.1.RELEASE/spring-b ...

  10. Spring Boot 简单入门案例

    第一:打开idea 找到spring  Initializr 第二:点击Next 在点击下一步 找到web之后勾选第一个spring web 就完成了 在写一个类 点击运行 结果如下:

随机推荐

  1. 顺序查找(Sequential Search)

    1.定义 顺序查找又叫线性查找,是最基本的查找技术. 2.基本思想 从表的一端开始(第一个或最后一个记录),顺序扫描线性表,依次将扫描到的结点关键宇和给定值K相比较.若当前扫描到的结点关键字与K相等, ...

  2. CDOJ 图论专题 A.不是图论 强连通分量+拓扑排序 经典

    题目链接  在其中纠错第一次wa代码 #include <cstdio> #include <cstring> #include <cstdlib> #includ ...

  3. [jmeter] : http采样器 Parameters、Body Data使用区别

    一.使用Jmeter进行接口测试,Headers和请求参数Parameters .Body Data 的联系 1.使用Parameters时,Content-Type 不传默认值为:applicati ...

  4. TCP慢启动

    慢启动定义 慢启动,是传输控制协议使用的一种阻塞控制机制.慢启动也叫做指数增长期.慢启动是指每次TCP接收窗口收到确认时都会增长.增加的大小就是已确认段的数目.这种情况一直保持到要么没有收到一些段,要 ...

  5. JS框架_(Laydate.js)简单实现日期日历

    百度云盘 传送门 密码:71hf JavaScript日期与时间组件_____laydate.js 日期日历效果: <!DOCTYPE html> <html> <hea ...

  6. LeetCode 12. 整数转罗马数字(Integer to Roman)

    题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II , ...

  7. java代码如何在没有安装JDK的Windows下运行

    java代码如何在没有安装JDK的Windows下运行? 对于Java桌面应用来说,比较烦琐的就是安装部署问题,如:客户端是否安装有jre.jre版本.jre在哪里下载.如何用jre启动Java应用等 ...

  8. 查询一个redis集群的大keys 脚本

    1. 把redis集群中的 master 节点信息记录在文件 redis_object_port.info 中, 方便下一步遍历各实例中的大 keys redis-cli -h 10.240.47.1 ...

  9. c# 单元测试 ,对静态方法(static)和私有方法(private) 进行单元测试

    利用反射: /// <summary> /// 调用静态方法 /// </summary>akf /// <param name="t">类全名 ...

  10. 建立WIN32 DLL,并使用静态加载和动态加载

    新建工程,选择win32 dll 编写.cpp(或.c) MyDll.cpp #include "windows.h" BOOL APIENTRY DllMain(HANDLE h ...