Spring Framework Part3 IoC and Dynamic Proxy
spring serious of blog edit by 马士兵教育
Maven方式创建Spring工程
工程创建
1.新建项目 选择Maven Project

2.勾选 Create a simple project

3.添加项目信息

l Group id :包名
l Artifact id:标识名
l Name:项目名
依赖引入
Maven 中央仓库
使用国内镜像
创建一个maven的配置文件
参照:
http://maven.apache.org/settings.html
Pom.xml
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.1..RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1..RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1..RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency>
空值注入
Value标签
标识空值 或空字符串 “”
<property name="name"><value></value></property>
Null标签
标识Null
<property name="name"><null></null></property>
工厂方式注入
为满足更复杂的需求,Spring也提供了工厂方式来创建更加灵活的Bean。
留意观察工厂类和实现类的创建次数
动态工厂
抽象接口 Car
public interface Car {
public String getName();
public String getPrice();
}
实现类 BMW车
public class Bmw implements Car{
public String getName() {
// TODO Auto-generated method stub
return "别摸我";
}
public String getPrice() {
// TODO Auto-generated method stub
return "500000RMB";
}
}
汽车工厂类 CarFactory
public class CarFactory {
public Car getCar(String name) throws Exception{
if (name.endsWith("bmw")) {
return new Bmw();
}else {
throw new Exception("car not fond");
}
}
}
Bean配置
<bean id="carFactory" class="com.msb.CarFactory"></bean> <bean id="car" factory-bean="carFactory" factory-method="getCar" > <constructor-arg value="bmw"></constructor-arg> </bean>
静态工厂
Bean配置
<bean id="carStatic" class="com.msb.CarFactoryStatic" factory-method="getCar"> <constructor-arg value="bmw"></constructor-arg> </bean>
工厂类
public class CarFactoryStatic {
public static Car getCar(String name) throws Exception{
if (name.endsWith("bmw")) {
return new Bmw();
}else {
throw new Exception("car not fond");
}
}
}
autowire自动注入
使用自动需要在配置文件中bean上添加autowire
<bean id="person" class="com.msb.Person" autowire="byName"> </bean> <bean id="pet" class="com.msb.Pet"> <property name="name" value="kele"></property> </bean>
实体
public class Person {
private String name;
private Pet pet;
}
public class Pet {
private String name;
}
可选两种类型
byName
byName方式自动注入:要求注入的bean的id必须和被注入的bean对象的属性名一致
byType
byType方式自动注入:要求注入的bean的对象类型与被注入的bean对象类型一致,并且在配置文件中的Bean相同类型必须唯一
如果存在多个,会抛异常:
No qualifying bean of type 'com.msb.Pet' available: expected single matching bean but found 2: pet,pet2
全局空值自动注入
在首行Beans标签下添加default-autowire属性。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byType" >
annotation注解注入
使用注解需要导入AOP包
在配置文件中添加Context约束
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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 " >
<context:component-scan>
<context:component-scan base-package="com.msb"></context:component-scan>
component-scan可以自动扫描包内容,并注册Bean到Spring容器
@Component
在需要注册到容器的类上添加@Component标签,标识这个类由Spring容器接管
约定大于配置
在一个类上添加@Component默认会使用首字母小写的类名作为ID注册到Spring容器。
如果需要手动指定Bean Id可以使用@Component("p")
同属@Component的额外三个注解
@Controller @Service @Repository
这三个注意在MVC开发中会经常用到,除了注解名字和Component不一样之外,其余功能都一样。
Spring额外提供这三个注解的目的主要是为了区分MVC中每个类的区别。
@Scope
使用注解注册Bean 默认的作用域还是singleton,可以使用@Scope("prototype")改变对象作用域
@Value
在使用注解给对象注入值的时候,不再需要Get/Set方法
基础类型
使用@Value注解
@Value("小明")
private String name;
对象引用
@Autowired
private Pet MyPet;
使用@Autowired注解
默认是ByType的,如果需要ByName需要配合@Qualifier注解
@Autowired()
@Qualifier("p2")
private Pet MyPet;
面向切面编程 代码增强
AOP(Aspect Oriented Programming)面向切面编程。
面向切面,是与OOP(Object Oriented Programming)面向对象编程并列的编程思想。
Spring支持两种方法,那么我们在使用spring进行动态代理时究竟使用的哪一种方法呢?spring优先支持实现接口的方式,如果没有接口则使用cglib方式
代理
通过代理可以隐藏目标类的具体实现;在不修改目标类代码的情况下能够对其功能进行增强。
l 委托类和代理类有相同的接口或者共同的父类
l 代理类为委托类负责处理消息,并将消息转发给委托类
l 委托类和代理类对象通常存在关联关系
l 一个代理类对象与一个委托类对象关联
l 代理类本身并不是真正的实现者!而是通过调用委托类的方法来实现功能!
静态代理
使用硬编码的方式增强原有方法
l 优点:可以做到不对目标对象进行修改的前提下,对目标对象进行功能的扩展和拦截。
l 缺点:因为代理对象,需要实现与目标对象一样的接口,会导致代理类十分繁多,不易维护,同时一旦接口增加方法,则目标对象和代理类都需要维护。
Girl -> 目标对象 -> 被包装/增强的对象
public class Girl implements Human{
public void eat() {
System.out.println("Em mmm.. mm..");
}
}
抽象接口
interface Human {
public void eat();
}
ProxyGirl 代理对象,包含对原对象方法的增强,通过构造方法传入原对象,并实现和原对象相同的接口,实现接口方法,便可以利用Java多态的特性,通过访问代理方法同时能够调起原对象的实现,并对其增强。
public class ProxyGirl implements Human {
private Human human;
public ProxyGirl() {
super();
}
public ProxyGirl(Human human) {
super();
this.human = human;
}
public void eat() {
System.out.println("chiqian");
human.eat();
System.out.println("chihou");
}
}
测试类
Girl girl = new Girl(); Human proxyGirl = new ProxyGirl(girl); proxyGirl.eat();
动态代理
动态代理是指动态的在内存中构建代理对象(需要我们制定要代理的目标对象实现的接口类型),即利用JDK的API生成指定接口的对象,也称之为JDK代理或者接口代理。
l 目标对象实现了接口 JDK动态代理
l 目标对象没有实现口CGLib
JDK动态代理
CGLIB动态代理
底层ASM
Spring Framework Part3 IoC and Dynamic Proxy的更多相关文章
- Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->使用spring framework的IoC容器功能----->方法一:使用XML文件定义beans之间的依赖注入关系
XML-based configuration metadata(使用XML文件定义beans之间的依赖注入关系) 第一部分 编程思路概述 step1,在XML文件中定义各个bean之间的依赖关系. ...
- 框架应用:Spring framework (一) - IoC技术
IoC概念以及目标 IoC就是让原本你自己管理的对象交由容器来进行管理,其主要的目的是松耦合. IoC发展史 既然IoC的目标是为了松耦合,那它怎么做到的? 最后目标:降低对象之间的耦合度,IoC技术 ...
- Spring Framework Part2 IOC
spring serious of blog edit by 马士兵教育 IoC概念 IoC是一个概念,是一种思想,其实现方式多种多样.当前比较流行的实现方式之一是DI. 基于XML的DI Appli ...
- Spring Framework之IoC容器
Spring IoC 概述 问题 1.什么是依赖倒置? 2.什么是控制反转? 3.什么是依赖注入? 4.它们之间的关系是怎样的? 5.优点有哪些? 依赖倒置原则 (Dependency Inversi ...
- Spring系列(零) Spring Framework 文档中文翻译
Spring 框架文档(核心篇1和2) Version 5.1.3.RELEASE 最新的, 更新的笔记, 支持的版本和其他主题,独立的发布版本等, 是在Github Wiki 项目维护的. 总览 历 ...
- Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->关于spring framework中的beans
Spring framework中的beans 1.概述 bean其实就是各个类实例化后的对象,即objects spring framework的IOC容器所管理的基本单元就是bean spring ...
- Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework的依赖注入和控制反转
Dependency Injection and Inversion of Control 1.概述: 1.1相关概念 bean:由IoC容器所管理的对象,也即各个类实例化所得对象都叫做bean 控制 ...
- JavaIOC框架篇之Spring Framework
欢迎查看Java开发之上帝之眼系列教程,如果您正在为Java后端庞大的体系所困扰,如果您正在为各种繁出不穷的技术和各种框架所迷茫,那么本系列文章将带您窥探Java庞大的体系.本系列教程希望您能站在上帝 ...
- Spring Framework 学习笔记——核心技术之Spring IOC
Spring Framework 官网文档学习笔记--核心技术之Spring IOC 官方文档 spring-framework-5.3.9 1. Spring Framework 核心技术 1.1 ...
随机推荐
- VASP表面计算步骤小结
原文链接:http://blog.sciencenet.cn/blog-478347-374981.html 一.概述 vasp用“slab” 模型来模拟表面体系结构. vasp计算表面的大 ...
- 浏览器端-3WSchool-JavaScript:JavaScript Boolean 对象
ylbtech-浏览器端-3WSchool-JavaScript:JavaScript Boolean 对象 1.返回顶部 1. Boolean 对象 Boolean 对象表示两个值:"tr ...
- 使用matlab画半透明椭圆
先上最终效果图: 本来是想直接用scatter和alpha来画的,结果在尝试以下代码后,发现无法显示透明效果 scatter(rand(1000,1),rand(1000,1), 'filled'); ...
- MongoDB简单查询语句<平时使用语录,持续更新>
MongoDB查询语句 --查询近三个月的客户使用量 aggregate:使用聚合 match:过滤 group分组 -- mysql中select org_code as 近三个月使用商户 ...
- python学习笔记:(十五)迭代器和生成器
一.迭代器: 1.迭代器是python最强大的功能之一,是访问集合元素的一种方式. 2.迭代器是一个可以记住遍历的位置的对象. 3.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问结束.迭代 ...
- 微信小程序的配置详解
1.配置详解: 使用app.json文件来对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.设置多 tab 等. 1>pages 接受一个数组,每一项都是字符串,来指定小 ...
- CoverflowJS
coverflow是苹果首创的将多首歌曲的封面以3D界面的形式显示出来的方式 GitHub地址:[https://github.com/coverflowjs/coverflow] 下载地址:[htt ...
- vue项目与node项目分离
为了前后端分离,我们在前端和api层中间,架构了一层node层,用来做服务端渲染,来加快用户的首屏可用和对搜索引擎的友好.项目一开始放置在同一个git仓库里面,分别放在client目录和server目 ...
- USACO1.6 Number Triangles [dp-简单dp]
题目传送门 回忆童年 /* ID: Starry21 LANG: C++ TASK: ariprog */ #include<iostream> #include<string> ...
- windows qt 使用openssl API
1.下载安装openssl http://dl.pconline.com.cn/download/355862-1.html 版本: OpenSSL(Win32) 1.0.1g 2.配置QT项目文件, ...