Spring 实践

标签: Java与设计模式


Spring简介

Spring是分层的JavaSE/EE Full-Stack轻量级开源框架.以IoC(Inverse of Control 控制反转)和AOP(Aspect Oriented Programming 面向切面编程)为内核, 取代EJB的臃肿/低效/脱离现实.
主页http://spring.io/

IoC与DI

  • IOC: 即控制反转, 解决程序对象紧密耦合问题(方式: 工厂+反射+配置文件), 将程序中原来构造对象的操作,交给IoC容器, 当程序真正需要对象时,再找IoC容器获取.
  • DI: 即依赖注入, IoC容器需要为程序提供依赖对象,而所依赖的对象又依赖于其他对象,因此可以一次获取该对象所依赖的所有对象(如Controller依赖于Service, Service依赖于DAO, 因此Controller找Ioc容器获取Service, 当IoC容器提供Service的同时,DAO也同时注入到Service中)

    详细可参考: IoC框架(依赖注入 DI)

Spring

  • 方便解耦,简化开发
    Spring就是一个大工厂,可将所有对象创建依赖关系的维护交给Spring管理;
  • AOP支持
    Spring支持面向切面编程,可以方便的实现对程序进行权限拦截/运行监控/缓存实现等功能;
  • 声明式事务管理
    只需通过配置就可完成对事务的管理,而无需手动编程;
  • 方便程序的测试
    Spring提供对Junit4支持,通过注解方便测试Spring程序;
  • 集成各种优秀框架
    Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:MyBatis/iBatis/Hibernate等)的直接支持;
  • 降低JavaEE API的使用难度
    Spring对JavaEE开发的一些API(如JDBC/JavaMail/远程调用等)提供了封装,大大降低API使用难度;

初识Spring

需求- 模拟用户注册过程:

  • Spring依赖
    进行Spring的IoC/DI开发,只需要导入Spring最核心依赖:core/beans/context/expression,为了看到DEBUG信息,我们还可以加上commons-logging, 而junit, 则是做单元测试必备的:
<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.2.0.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.0.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.2.0.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>4.2.0.RELEASE</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version></dependency>
</dependencies>
  • Controller
/*** Created by jifang on 15/12/5.*/
public class UserController {/*** 依赖注入(DI): 在Spring构造UserController对象时, 可以同时将构造好的UserService对象注入(下同)*/private IUserService userService;public IUserService getUserService() {return userService;}public void setUserService(IUserService userService) {this.userService = userService;}public void register(String userName, String password) {System.out.println("用户: " + userName + " 进行注册...");userService.register(userName, password);}
}
  • Service
public interface IUserService {void register(String userName, String password);
}
public class UserServiceImpl implements IUserService {private IUserDao userDao;public IUserDao getUserDao() {return userDao;}public void setUserDao(IUserDao userDao) {this.userDao = userDao;}@Overridepublic void register(String userName, String password) {System.out.println("用户: " + userName + " 进行注册...");userDao.add(userName, passProcess(password));}// 对密码进行加密处理private String passProcess(String password) {System.out.println("密码: " + password + "加密处理...");return password;}
}
  • DAO
public interface IUserDao {void add(String userName, String password);
}
public class UserDaoImpl implements IUserDao {@Overridepublic void add(String userName, String password) {System.out.println("用户: " + userName + ", 密码: " + password + " 加入数据库");}
}
  • 配置Bean
<?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"><bean id="userDao" class="com.fq.first.dao.impl.UserDaoImpl"></bean><bean id="userService" class="com.fq.first.service.impl.UserServiceImpl"><property name="userDao" ref="userDao"></property></bean><bean id="userController" class="com.fq.first.controller.UserController"><property name="userService" ref="userService"></property></bean></beans>
  • 测试
/*** Created by jifang on 15/12/5.*/
public class UserControllerTest extends TestCase {/*** 加载Spring容器*/private ApplicationContext context;@Beforepublic void setUp() throws Exception {context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");}@Testpublic void testRegister() throws Exception {UserController controller = context.getBean("userController", UserController.class);controller.register("翡青", "123");}
}
  1. 在程序中通过ApplicationContext接口加载Spring容器, 获取Spring工厂对象

    • ClassPathXmlApplicationContext //读取src下配置文件
    • FileSystemXmlApplicationContext //读取WEB-INF下配置文件
  2. Spring对象工厂- BeanFactory与ApplicationContext:
    • ApplicationContextBeanFactory的子接口,BeanFactory是Spring最核心工厂接口。
    • ApplicationContext提供更多功能(如国际化处理/自动装配Bean/不同应用层的Context实现)
    • ApplicationContext会在容器初始化时对其中管理Bean对象进行创建,BeanFactory会在对象获取时才进行初始化.

XML装配

Spring提供了两种装配Bean的方式, XML与注解,其中XML方式Spring支持较早,现在在配置一些不是自己写的Bean时(如数据库连接池等从Jar包种引入的Bean)时是非常有用,而注解方式则常用于装配自己写的Bean.


三种实例化Bean的方式

  • 构造器实例化
<!--使用构造器(默认无参)构造对象-->
<bean id="constructBean" class="com.fq.instance.ConstructBean">
</bean>
  • 静态工厂的静态方法实例化
<!--使用静态工厂构造对象, 注: class应为工厂类-->
<bean id="staticBean" class="com.fq.instance.StaticBeanFactory" factory-method="getInstance">
</bean>
  • 实例工厂的实例方法实例化
<!--使用实例工厂构造对象, 注: 要先实例化工厂-->
<bean id="beanFactory" class="com.fq.instance.InstanceBeanFactory">
</bean>
<!-- 再通过工厂对象的实例方法,构造目标对象 -->
<bean id="instanceBean" factory-bean="beanFactory" factory-method="getInstance">
</bean>

Bean作用域

类别 说明
singleton 在容器中仅存在一个实例(单例模式)
prototype 每次从容器中获取都返回一个新的实例,即每次调用getBean()时,都执行一次构造方法(lazy,原型模式)
request 每次HTTP请求都会创建一个新的Bean,该作用域仅适用于WebApplicationContext环境(不常用)
session 同一个Session共享一个Bean,仅适用于WebApplicationContext环境(不常用)
globalSession 一般用于Porlet应用环境,该作用域仅适用于WebApplicationContext环境(不常用)
  • scope
<!--Spring使用scope标签来制定bean的作用域(默认为Singleton)-->
<bean id="singletonBean" class="com.fq.instance.SingletonBean" scope="singleton">
</bean>
<bean id="prototypeBean" class="com.fq.instance.PrototypeBean" scope="prototype">
</bean>

Bean生命周期

Spring初始化/销毁bean时, 有时需要作一些处理工作, 因此Spring可以在创建和销毁bean的时候调用bean的两个生命周期方法;

/*** Created by jifang on 15/12/6.*/
public class LifecycleBean {public LifecycleBean() {System.out.println("Constructor ...");}/*** 声明周期方法需: 无参, 无返回值, 非static*/public void setUp() {System.out.println("SetUp ...");}/*** 同上*/public void tearDown() {System.out.println("TearDown ...");}
}
  • 配置:
<!-- init-method属性配置初始化方法,destroy-method属性配置销毁方法-->
<bean id="lifecycleBean" class="com.fq.bean.LifecycleBean" init-method="setUp" destroy-method="tearDown">
</bean>
  • 测试
/*** Created by jifang on 15/12/6.*/
public class LifecycleBeanTest extends TestCase {private ClassPathXmlApplicationContext context;@Beforepublic void setUp() throws Exception {context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");}@Testpublic void testLifecycle(){LifecycleBean bean = context.getBean("lifecycleBean", LifecycleBean.class);System.out.println(bean);}@Afterpublic void tearDown() throws Exception {// 必须手动调用context的close方法, 才会执行bean的销毁方法context.close();}
}

初始化方法与构造方法的区别?
1) 构造方法为对象申请空间, 完成对象基本属性的初始化;
2) 初始化方法主要完成对象复杂构造过程;
3) Java建议将对象复杂构造过程单独抽取出初始化方法, 如javax.servlet.GenericServlet
init方法

public void init(ServletConfig config) throws ServletException {this.config = config;this.init();
}

后处理器

Spring提供了BeanPostProcessor接口,在构造Bean对象执行对象初始化(init-method)方法时可以对Bean进行处理;

/*** Created by jifang on 15/12/6.*/
public class PrintBeanProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {// 可以根据beanName来决定对那个Bean进行后处理操作if (beanName.equals("lifecycleBean")) {System.out.println("后处理bean -- process before ...");}return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {// 如果不制定beanName, 则默认处理所有BeanSystem.out.println("后处理bean -- process after ...");return bean;}
}
  • 配置
<!-- 为Spring容器所用的bean, 不需配置id -->
<bean class="com.fq.processor.PrintBeanProcessor"></bean>

这样在执行init-method[setUp]的前后, 会分别执行BeanPostProcessor中的两个方法.

后处理器可以在对象构造过程中提供代理,这是AOP自动代理的核心.


XML依赖注入

Spring配置文件支持构造参数属性注入和Setter方法属性注入;

1. 构造参数注入

<bean id="bean" class="com.fq.di.Bean"><!--index   代表参数顺序(从0开始)name    代表参数名type    参数类型value   注入的参数值ref     引用另一个bean元素的id--><constructor-arg index="0" type="java.lang.String" value="fei_qing"></constructor-arg><constructor-arg index="1" type="java.lang.Double" value="3.14"></constructor-arg>
</bean>

2. Setter方法注入

<bean id="bean" class="com.fq.di.Bean"><!--name    属性名(congSetter方法获得)value   注入的参数值ref     引用的另一个bean的id--><property name="name" value="fei_qing"></property><property name="price" value="88.8"></property>
</bean>

3. p名称空间注入

P名称空间在spring2.5版本后引入, 目的是为了简化属性依赖注入(setter方法)

<!--p:属性名="XXX", 引入常量值p:属性名-ref="XXX", 引用其他Bean对象
-->
<bean id="bean" class="com.fq.di.Bean" p:name="feiqing" p:price="1188">
</bean>

4. SpEL表达式

在spring3.0之后,引入SpEL表达式,以简化属性注入.

#{表达式}, 通过value属性注入: 可以引用一个Bean对象/对象属性/对象方法… 详细可参考Spring 表达式语言(SpEL)

  • Bean
public class Car {private String logo;private double price;private String owner;public String getLogo() {return logo;}public void setLogo(String logo) {this.logo = logo;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public String getOwner() {return owner;}public void setOwner(String owner) {this.owner = owner;}
}
public class Employ {private String name;private Car car;public String getName() {return name;}public void setName(String name) {this.name = name;}public Car getCar() {return car;}public void setCar(Car car) {this.car = car;}
}
  • 配置
<!--SpEL 使用#{}来引用/获取对象-->
<bean id="car" class="com.fq.di.Car"><property name="logo" value="#{'logo.pic'}"/><property name="price" value="#{18.8}"/><property name="owner" value="#{'feiqing'}"/>
</bean><bean id="employ" class="com.fq.di.Employ"><!-- 可以直接使用value来引用到对象, 而不是ref --><property name="car" value="#{car}"/><!-- 可以直接引用一个对象的属性 --><!--<property name="name" value="#{car.owner}"/>--><!-- 还可以直接调用对象的方法 --><property name="name" value="#{car.getOwner().toUpperCase()}"/>
</bean>

4. 集合属性注入

java常见集合: List/Set/Map/Properties等, Spring为每种集合都提供一个标签进行注入;

  • Bean
public class CollectionBean {private List<String> list;private Set<String> set;private Map<String, String> map;private Properties properties;public List<String> getList() {return list;}public void setList(List<String> list) {this.list = list;}public Set<String> getSet() {return set;}public void setSet(Set<String> set) {this.set = set;}public Map<String, String> getMap() {return map;}public void setMap(Map<String, String> map) {this.map = map;}public Properties getProperties() {return properties;}public void setProperties(Properties properties) {this.properties = properties;}
}
  • 配置
<bean id="collectionBean" class="com.fq.di.CollectionBean"><property name="list"><list><value>aa</value><value>bb</value><value>cc</value><value>dd</value></list></property><property name="set"><set><value>11</value><value>12</value><value>11</value></set></property><property name="map"><map><entry key="key1" value="value1"/><entry key="key2" value="value2"/></map></property><property name="properties"><props><prop key="key1">value_1</prop><prop key="key2">value_2</prop></props></property>
</bean>

注解装配

注解配置Bean

  • 在需要Spring管理的类上添加@Component注解
    (@Component还可以指定组件名@Component(value = "xxx"))
@Component
public class Bean {private String name;private Double price;public Bean() {}public Bean(String name, Double price) {this.name = name;this.price = price;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Double getPrice() {return price;}public void setPrice(Double price) {this.price = price;}
}
  • 引入context命名空间并批量扫描
<?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"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 base-package="com.fq.di"/>
</beans>

Spring细化@Component以细分组件功能,提供了以下三个等价注解:

注解 说明
@Controller 控制器,web层组件
@Service 业务类,业务层组件
@Repository 持久层组件

Bean作用域

通过@Scope注解指定作用域

@Component
@Scope("prototype")
public class Bean {// ...
}

Bean生命周期

@PostConstruct 初始化
@PreDestroy 销毁
  • Bean
public class Bean {@PostConstructpublic void setUp(){System.out.println("setUp ...");}@PreDestroypublic void tearDown(){System.out.println("tearDown ...");}
}

注解依赖注入

1. @Value

  • 简单类型
@Component
public class Bean {@Value("feiqing")private String name;@Value("88.88")private Double price;// ....
}
  • 复杂属性(使用SpEL表达式)
@Component
public class Bean {@Value("#{car}")private Car car;// ...
}

2. @Autowired

  • @Autowired 默认按照类型进行注入(如果容器中存在两个相同类型对象,则@Autowired无法注入)
@Component
public class Bean {@Autowiredprivate Car car;// ....
}
  • @Autowired+@Qualifier指定注入Bean的id
@Component
public class Bean {@Autowired@Qualifier("car")private Car car;// ...
}

3. @Resource

Spring支持JSR-250规范,可以使用@Resource()进行属性注入,功能和@Autowired相同:

@Controller(value = "bean")
public class Bean {@Resource(name = "car")private Car car;//...
}

注解/XML混合

Bean定义使用XML,Bean关系依赖注入使用注解:

需要在applicationContext.xml中配置:

<context:annotation-config/>

该配置可以使@Resource@PostConstruct@PreDestroy@Autowired注解生效.

如果在配置文件中使用了<context:component-scan base-package="xxx.xx"/>则具有了<context:annotation-config/>的效果, 不必再单独配置.


转载于:https://www.cnblogs.com/itrena/p/5926901.html

Spring 实践 -IoC相关推荐

  1. Spring 容器IOC解析及简单实现

    这篇文章来自极客学院 : Spring 容器IOC解析及简单实现 最近一段时间,"容器"两个字一直萦绕在我的耳边,甚至是吃饭.睡觉的时候都在我脑子里蹦来蹦去的.随着这些天一次次的交 ...

  2. idea中生成spring的 xml配置文件_【132期】面试再被问到Spring容器IOC初始化过程,就拿这篇文章砸他~...

    点击上方"Java面试题精选",关注公众号 面试刷图,查缺补漏 >>号外:往期面试题,10篇为一个单位归置到本公众号菜单栏->面试题,有需要的欢迎翻阅 阶段汇总集 ...

  3. Spring的IoC解析

    这是Spring中得有特点的一部份.IoC又被翻译成"控制反转",也不知道是谁翻译得这么别扭,感觉很深奥的词.其实,原理很简单,用一句通俗的话来说:就是用 XML来定义生成的 对象 ...

  4. Java:Spring的IOC原理(大白话解释)

    先行参考以下半成品文章和参考链接,待学完课程后续整理此文章 IOC和DI关系 IOC:Inversion of Control,控制反转 DI:Dependency Injection,依赖注入 关系 ...

  5. 浅谈spring之IoC控制反转

    以下学习资料来源于b站动力节点 spring: 出现是在2002左右,解决企业开发的难度.减轻对项目模块之间的管理,类和类之间的管理, 帮助开发人员创建对象,管理对象之间的关系.spring核心技术 ...

  6. Spring框架IOC基础及XML的配置 第二章

    1 Spring概述 1.1 关于框架 框架的概念 框架:特指软件框架,它是我们在实际开发中解决项目需求的技术集合.运用框架可以大大简化代码的编写,缩短开发周期.同时,对后续负责项目维护的人员降低技术 ...

  7. 【132期】面试再被问到Spring容器IOC初始化过程,就拿这篇文章砸他~

    程序员的成长之路 互联网/程序员/技术/资料共享 关注 阅读本文大概需要 14 分钟. 作者:拥抱心中的梦想 juejin.im/post/5ab30714f265da237b21fbcc 一.老规矩 ...

  8. Spring容器IOC初始化过程—今天终于进行总结了

    https://www.colabug.com/2539499.html 作为一个经常使用Spring的后端程序员,小编很早就想彻底弄懂整个Spring框架了!但它整体是非常大的,所有继承图非常复杂, ...

  9. [Spring 深度解析]第6章 Spring的IoC容器系列

    6. Spring的IoC容器系列 ​ IoC容器为开发者管理对象之间的依赖关系提供了很多便利和基础服务.有许多IoC容器供开发者选择,SpringFramework的IoC核心就是其中一个,它是开源 ...

最新文章

  1. html5通html5通,HTML5 history详解
  2. 中间省略_手机号码中间4位设置为*号,我用了5小时,可同事8秒就搞定了
  3. ubuntu_subversion_mantis_testlink使用ldap认证
  4. python画椭圆-python opencv圆、椭圆与任意多边形的绘制实例详解
  5. Windows10——荣耀笔记本任务栏图标显示异常且无显示/隐藏图标的箭头解决方案
  6. hazelcast入门教程_Hazelcast入门指南第5部分
  7. Linux上Oracle 11g安装步骤图解(超详细图文教程)附带导入数据和新建数据库教程
  8. linux ssh客户端乱码,Win10专业版下Open ssh客户端乱码咋办?
  9. Meteor:快到飞起来的全栈JavaScript开发平台
  10. Arturia Analog Lab V for Mac - 超强键盘模拟合成器
  11. 数字信号处理(matlab版)(第3版) pdf,数字信号处理-(第3版)-(MATLAB版)
  12. web版python软件授权注册机
  13. TCP粘包以及UDP丢包问题
  14. 小米电视显示服务器断开连接,小米电视投屏频繁断开的解决办法
  15. 模拟电子中放大电路的基本分析方法
  16. UVA#11584Partitioning by Palindromes
  17. windows已经阻止此应用 如何解决(非专业版系统)
  18. 10款热门的企业报表工具软件,该如何选择?
  19. idea 右侧的maven没有dependencies
  20. 婚庆APP开发解决方案

热门文章

  1. pygame是python的一个库吗,python学习pygame,,基本库导入impor
  2. left join左表百万数据查询慢_Spark SQL 之 Join 实现
  3. mysql 快速升级_快速升级MySQL系统表
  4. 梯度算法求步长的公式_LM(Levenberg-Marquarelt)算法
  5. 多路三线RTD电阻温度采集电路设计方案
  6. Ubuntu virtualbox
  7. XOR and Favorite Number CF340E 莫队算法
  8. 金士顿u盘量产工具_与时俱进,金士顿双接口优盘支持Type C接口
  9. Fast ORB-SLAM
  10. USTC并行计算复习