学习spring的第一天
1.首先在maven repository中找到Spring Context依赖添加进模块
2.配置xml,resources右键new→xml configuration file→Spring Config,例如取一个名字为applicationConfig.xml。该文件是元数据,再次文件里通过一个个的bean告诉spring管理那些类,这些类必须是要能够实例化的,接口和抽象类就不行
3.开始在xml中配置元素标签。
3.1<bean>,有属性如下,id:在java代码中通过getBean("id")来得到对象,
3.2 class:指定全称,表面这个bean取得的对象是什么类的,并且其destroy-method和init-method方法只能在此类中寻找
3.3 scope: 有4种,分别为prototype(原型,每次getBean的时候都重新创建一个),singleton(单例,同时也是默认的,在spring容器启动时就被创建,每次getBean的时候都从容器中获取,具体的java代码是:ApplicationContext context = new ClassPathXmlApplicationContext("applicationConfig.xml");),request(请求,它和session都是在web中才有用,被spring管理的bean,它的请求都在一个完整的请求周期里),session(会话,在一个会话里)
3.4 factory-method:工厂方法,调用在class属性中的类中有的方法,使得getBean的返回值可以为该方法的返回值类型。
3.5 destroy-method和init-method:销毁方法和初始化方法,调用class属性的类中有的方法,在初始化和销毁阶段会调用对应的方法。同时可以在<beans>标签中写全局的销毁和初始化方法,default-init-method和default-destroy-method,但是他们会被<bean>中的覆盖,同时,只有<bean>中的class属性的类中有这2个全局属性的方法时,才会调用。
3.6 factory-bean:先创建一个<bean>,用该<bean>的id作为属性值,配合factory-method属性,该属性是方法名。
xml代码示例如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-destroy-method="ss" default-init-method="ss"> <bean id="first" class="com.util2.EmployeeDao" factory-method="getEmployee" scope="prototype"></bean> <bean id="second" class="com.util2.EmployeeDao" scope="prototype"></bean> <bean id="third" factory-bean="second" factory-method="getEmployee1" scope="prototype"></bean> <bean id="fourth" class="com.util2.EmployeeImpl" scope="prototype" ></bean> <bean id="emp" class="com.util.EmployeeDao" scope="prototype"></bean> </beans>
4.java代码实例:关于getBean方法是有重载的,也有一个参数的写法,不过没有指明具体的Class对象,返回值是Object
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationConfig2.xml");
Employee first = context.getBean("first", Employee.class);
System.out.println((Employee)context.getBean("first"));
System.out.println(first);
Employee third = context.getBean("third", Employee.class);
System.out.println(third);
EmployeeDao second = context.getBean("second", EmployeeDao.class);
System.out.println(second);
Employee fourth = context.getBean("fourth", Employee.class);//1,2,4不一样
com.util.EmployeeDao employeeDao = (com.util.EmployeeDao) context.getBean("emp");
System.out.println(fourth);
((ConfigurableApplicationContext) context).close();
}
其中可以将ApplicationContext转型为ConfigurableApplicationContext以启用close方法,测试xml中的destroy-method方法。
5.补充:今天还学了三个实现接口:一个是FactoryBean<T>,InitializingBean,DisposableBean,具体用法可以自己写个类实现以下,看看需要重写哪些方法,很容易理解。
示例:
public class EmployeeDaoLifeCycle2 implements InitializingBean, DisposableBean {
/**
* 这个方法名取名叫:"在属性设置完毕之后"
* 其意思就是此类中各种setter方法被调用后
* 才调用这个初始化方法
* @throws Exception
*/
public void afterPropertiesSet() throws Exception {
System.out.println("after properties set :初始化");
}
public void destroy() throws Exception {
System.out.println("destroy---");
}
public class MyFactoryBean implements FactoryBean<A> {
/**
* 这个方法用来创建一个对象
* @return
* @throws Exception
*/
public A getObject() throws Exception {
return new A();
}
/**
* 这个方法是用来表明此工厂Bean创建出来的对象的class
* @return
*/
public Class<?> getObjectType() {
return A.class;
}
/**
* 这个方法表明此工厂Bean创建出来的对象,在spinrg管理下的作用域
* true表示是singleton
* @return
*/
public boolean isSingleton() {
return true;
}
}
学习spring的第一天的更多相关文章
- Spring实战第一章学习笔记
Spring实战第一章学习笔记 Java开发的简化 为了降低Java开发的复杂性,Spring采取了以下四种策略: 基于POJO的轻量级和最小侵入性编程: 通过依赖注入和面向接口实现松耦合: 基于切面 ...
- 学习Spring——依赖注入
前言: 又开始动笔开了“学习Spring”系列的头…… 其实一开始写“学习SpringMVC”的几篇文章是出于想系统的了解下Spring以及SpringMVC,因为平时在公司中虽然每天都在使用Spri ...
- 深入浅出学习Spring框架(四):IoC和AOP的应用——事务配置
在前文 深入浅出学习Spring框架(一):通过Demo阐述IoC和DI的优势所在. 深入浅出学习Spring框架(三):AOP 详解 分别介绍了Spring的核心功能——IoC和AOP,光讲知识远远 ...
- 借助Maven入手Spring Boot第一个程序
目前网上有不少Spring Boot的入门文章,都很有帮助,本人最近在深入学习Spring Cloud,在搭建第一个Hello World程序时,感觉对于新手而言,介绍文章怎么详细都不为过,因为其中坑 ...
- 学习Spring Boot:(二十六)使用 RabbitMQ 消息队列
前言 前面学习了 RabbitMQ 基础,现在主要记录下学习 Spring Boot 整合 RabbitMQ ,调用它的 API ,以及中间使用的相关功能的记录. 相关的可以去我的博客/RabbitM ...
- Spring框架第一天
## 今天课程:Spring框架第一天 ## ---------- **Spring框架的学习路线** 1. Spring第一天:Spring的IOC容器之XML的方式,Spring框架与Web项目整 ...
- 【转】Spring学习---Spring IoC容器的核心原理
[原文] Spring的两个核心概念:IoC和AOP的雏形,Spring的历史变迁和如今的生态帝国. IoC和DI的基本概念 IoC(控制反转,英文含义:Inverse of Control)是Spr ...
- 从零开始手写 spring ioc 框架,深入学习 spring 源码
IoC Ioc 是一款 spring ioc 核心功能简化实现版本,便于学习和理解原理. 创作目的 使用 spring 很长时间,对于 spring 使用非常频繁,实际上对于源码一直没有静下心来学习过 ...
- 学习 Spring Boot 知识看这一篇就够了
从2016年因为工作原因开始研究 Spring Boot ,先后写了很多关于 Spring Boot 的文章,发表在技术社区.我的博客和我的公号内.粗略的统计了一下总共的文章加起来大概有六十多篇了,其 ...
随机推荐
- linux终端出现"-bash-2.05b$"现象的解决方法
参考:http://blog.chinaunix.net/uid-22823163-id-3295220.html
- PE文件结构体-IMAGE_DATA_DIRECTORY
IMAGE_OPTIONAL_HEADER结构体最后一个成员是数组结构,大小为16,每个元素都是一个IMAGE_DATA_DIRECTORY结构体 typedef struct _IMAGE_DATA ...
- firewalld学习--维护命令
启动 systemctl start firewalld 停止 systemctl stop firewalld 重启 systemctl restart firewalld 查询状态 systemc ...
- liunx mysql 5.7 二进制安装
liunx 5.6版本 本人安装次数不下20次,基本上按照正常的操作流程不会出现什么问题,一切顺利. 今天开发新项目需要按照mysql 5.7 版本.mysql 5.7版本和mysql 5.6版本变化 ...
- 使用gulp 进行ES6开发
使用gulp 进行ES6开发 一.新建项目 项目结构如下: /app -- /js -- /css /dist -- /js -- /css -- index.html gulpfile.js 我们的 ...
- 002.Oracle数据库 , 列别名
/*Oracle数据库查询日期在两者之间*/ SELECT OCCUR_DATE as "我是一列" FROM LM_FAULT WHERE ( ( OCCUR_DATE > ...
- Javascript获取当前鼠标在元素内的坐标定位
代码如下: <!doctype html> <html> <head> <meta charset="utf-8"> <tit ...
- 自定义对象使用 ArrayList 进行增删改查(dos程序)
马上要进行java基础考试了,闲着写一次博客,把这10周学的东西过一遍,可能没过全,.....但是我觉得增删改查是必须的,以前一直不会用ArrayList 自定义对象....... 案例如下:自己根 ...
- tornado peewee_async
https://peewee-async.readthedocs.io/en/latest/peewee_async/examples.html https://www.cnblogs.com/Vic ...
- c#实现"扫描检测硬件改动"
public static class Win32Api { public const int CM_LOCATE_DEVNODE_NORMAL = 0x00000000; public const ...