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. MessagePack Java 0.6.X 使用一个消息打包(message-packable)类

    使用注解 @Message 来让你可以序列化你自己类中对象的 public 字段. 本代码可以在 https://github.com/cwiki-us-demo/messagepack-6-demo ...

  2. touch:创建文件及修改文件时间戳

    touch 命令不光可以用来创建文件(当指定操作文件不存在时,该命令会在当前位置建立一个空文件),此命令更重要的功能是修改文件的时间参数(但当文件存在时,会修改此文件的时间参数). Linux 系统中 ...

  3. python基础之流程控制

    流程控制之----if 流程控制,是指程序在运行时,个别的指令(或者是陈述.子程序)运行或者求值的顺序.人生道路上的岔口有很多,在每个路口都是一个选择,在每个路口加上一个标签,选择哪个就是满足哪个条件 ...

  4. R_Studio(时序)Apriori算法寻找频繁项集的方法

    应用ARIMA(1,1,0)对2015年1月1日到2015年2月6日某餐厅的销售数量做为期5天的预测 setwd('D:\\dat') #install.packages("forecast ...

  5. 分布式-网络通信-IO-基础(2)

    IS 与 OS1. 基本 IO 操作1.1. InputStream 与 OutputStream1.1.1. 输入与输出我们编写的程序除了自身会定义一些数据信息外,经常还会引用外界的数据,或是将自身 ...

  6. java @Value注解 和 @Data注解

    @Value注解 service层代码 @Service public class HelloServiceImpl implements HelloService { @Autowired priv ...

  7. 并发量,tps,qps

    QPS/TPS/并发量/系统吞吐量的概念 2017年08月13日 17:24:47 阅读数:10682 我们在日常工作中经常会听到QPS/TPS这些名词,也会经常被别人问起说你的系统吞吐量有多大.这个 ...

  8. Jmeter性能测试环境搭建(Windows下)

    最近刚开始接触Jmeter性能测试,现总结环境搭建如下: 一.windows安装JDK步骤与环境变量配置: 1.先将下载的JDK安装到其默认目录:C:\Program Files\Java\jdk1. ...

  9. Alpha发布--美工+文案

    此作业对应要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/8677 一.美工: 1.产品logo 2.原型页面展示 2.1 进入萌 ...

  10. Win10无法修改编辑hosts文件

    Win10无法修改编辑hosts文件 一.总结 一句话总结: 这里我的问题是windows的某次更新中把hosts文件或者上级目录设置成了只读,在文件属性中去掉这个只读就好 后文的操作相当于是给文件添 ...