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. Window 环境升级node版本

    https://github.com/Kenshin/gnvm 下载gnvm,安装在node文件目录下 gnvm version 然后打开cmd命令行窗口,输入:gnvm update latest, ...

  2. HGOI20190813 省常中互测6

    Problem A 蛋糕 将$n \times m $大小的蛋糕切成每块为$1 \times 1$大小的$n\times m$块. 交换任意两块蛋糕的切割顺序的方案算作一种. 对于$100 \%$的数 ...

  3. 微信支付(公众号)爬坑记,包含 total_fee 失败和 JSAPI 签名验证失败等等

    做商城类网站不免会需要做支付功能,目前在中国大陆通用的做法就是使用支付宝支付和微信支付,上一篇博文已经讲个支付宝支付. 这篇文章来讲一讲微信支付,微信支付的方式有很多种,本文主要讲 JSAPI 支付的 ...

  4. jpa repostiory

    JpaRepository的查询   image.png   image.png Spring Data JPA框架在进行方法名解析时,会先把方法名多余的前缀截取掉,比如 find.findBy.re ...

  5. C++入门经典-例4.11-名称空间的定义和使用

    1:名称空间,也成为名字空间.命名空间,关键字为namespace.我们经常使用这样一条语句: using namespace std: 我们要使用标准输入输出流,除了包含它们所在的头文件外,还必须使 ...

  6. python开发环境的搭建,以及pycharm的安装

    先到python 官网下载python. 下载好了之后,直接运行exe文件,进行安装(在安装程序运行后的第一个form上,点击next的时候,在next的左侧有一排文字和一个复选框,那个是添加环境变量 ...

  7. leetcode-easy-listnode-19 remove nth node from end of list

    mycode  88.29% 关键是一定要head前新建一个节点,否则要分类讨论很多次来避免slow或者fast出现None.next的错误 # Definition for singly-linke ...

  8. oracle字段like多个条件

    写oracle sql时有时候会有 and (字段 like ‘匹配串1’or 字段 like ‘匹配串2’or ...)这样的情况出现,下面提供一个简洁点的解决方案: and REGEXP_LIKE ...

  9. P1200 [USACO1.1]你的飞碟在这儿Your Ride Is He…

    P1200 [USACO1.1]你的飞碟在这儿Your Ride Is He…   大写祖母转数字  -64   发现dalao   #include<bits/stdc++.h> usi ...

  10. pandas dataframe类型操作

    用python做数据分析pandas库介绍之DataFrame基本操作   怎样删除list中空字符? 最简单的方法:new_list = [ x for x in li if x != '' ] 这 ...