学习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 的文章,发表在技术社区.我的博客和我的公号内.粗略的统计了一下总共的文章加起来大概有六十多篇了,其 ...
随机推荐
- python Web生成token的几种方法,你确定不进来看看?
1.使用rest_framework_jwt from rest_framework_jwt.settings import api_settings jwt_payload_handler = ap ...
- 第二单元总结:基于synchronize锁的简单多线程设计
单元统一的多线程设计策略 类的设计 电梯 每部电梯为一个线程. 电梯从调度器接收原子指令,知晓自己的状态(内部的人/服务的人.运行方向.所在楼层) 原子指令包括且仅包括: 向上走一层 / 向下走一层 ...
- chart 模板【转】
Helm 通过模板创建 Kubernetes 能够理解的 YAML 格式的资源配置文件,我们将通过例子来学习如何使用模板. 以 templates/secrets.yaml 为例: 从结构看,文件的内 ...
- Console-terminal-tty-shell-kernel
Console-terminal-tty-shell-kernel 1. 先看图表 1.1 简表 1.2 shell与内核的示意图 1.3 Console-terminal-t ...
- mysq8设置编码utf8
设置mysql默认编码utf8 以及其他配置 系统:centos7 vi /etc/my.cnf #红色部分如果以存在则在他的下方添加 [mysql] default-character-set=ut ...
- Java笔记--泛型
1.泛型解决元素存储的安全性问题:解决获取数据元素时,需要类型强转的问题. --泛型的核心思想:把一个集合中的内容限制为一个特定的数据类型. 2.泛型的使用 1)在集合中使用 2)自定义泛型类.泛型接 ...
- Java小项目之:教你做个聊天系统!
Java小项目之:聊天系统 今天给大家带来的java练手小项目是一个简单的聊天室,界面简单,操作不难. 分为注册系统,登录系统和聊天系统三部分,很适合java小白练手. 完整的源码和素材请关注并私信我 ...
- 005-PHP函数输出一行内容
<?php function printBold($inputText) //定义function printBold() { print("<B>" . $in ...
- Spring Aop 原理分析
@EnableAspectJAutoProxy Aop功能开启注解 为容器中导入 @Import(AspectJAutoProxyRegistrar.class)组件,在其重写方法中为 ioc容器 注 ...
- 关于安装openfiler
简介 Openfiler 由rPath Linux驱动,它是一个基于浏览器的免费网络存储管理实用程序,可以在单一框架中提供基于文件的网络连接存储 (NAS) 和基于块的存储区域网 (SAN).Open ...