Spring:Spring入门和bean的生命周期
来源:互联网 发布:曰本武士刀淘宝 编辑:程序博客网 时间:2024/04/27 22:44
1.Spring概述
Spring的核心是控制反转(IoC)和面向切面(AOP)。简单来说,Spring是一个分层的JavaSE/EEfull-stack(一站式) 轻量级开源框架。
控制反转(IOC, Inverse Of Control):创建对象、维护对象之间关系的权利从程序转移到Spring容器;
依赖注入(DI, Dependency Injection):对象之间的依赖关系、属性值的注入。
面向切面(AOP, Aspect Oriented programming):Spring提供了面向切面编程的丰富支持,允许通过分离应用的业务逻辑与系统级服务进行内聚性的开发。应用对象只实现它们应该做的——完成业务逻辑——仅此而已。它们并不负责(甚至是意识)其它的系统级关注点,例如日志或事务支持。
2.第一个Spring简单程序
package com.service;public class ByeService {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public void SayBye(){System.out.println("ByeBye "+name);}}
package com.service;public class UserService {private String name;private ByeService byeService;public String getName() {return name;}public void setName(String name) {this.name = name;}public ByeService getByeService() {return byeService;}public void setByeService(ByeService byeService) {this.byeService = byeService;}public void sayHello(){System.out.println("Hello "+name);byeService.SayBye();}}
<?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 definitions here --> <!-- 在容器文件中配置bean(bean可以是service/dao/domain/action/数据源) --> <!-- bean元素的作用是:当我们的spring框架加载时,spring就会自动创建一个bean的对象,并放入内存 --> <!-- 下面的bean相当于以下两句话: UserService userService=new UserService(); userService.setName("zj"); --> <!-- 读xml文件,获取类名称,利用反射机制创建类的实例 --> <!-- 配置bean,注入属性,维护bean之间的关系 --> <bean id="userService" class="com.service.UserService"> <property name="name"> <value>zj</value> </property> <!-- 在userService中引用byeService的bean --> <property name="byeService" ref="byeService"> </property> </bean> <bean id="byeService" class="com.service.ByeService"> <property name="name"> <value>zj</value> </property> </bean></beans>
package com.test;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.core.io.ClassPathResource;import com.service.ByeService;import com.service.UserService;public class Test1 {public static void main(String[] args) {/* * 传统的方法: * UserService us=new UserService(); * us.sayHello(); *///方式1:ApplicationContext(没有特殊要求使用此种方法,项目中90%用此种方法)//beans.xml被加载时,文件中配置的作用域为singleton的bean也被实例化(作用域为其他的则不然)//优点:可以预先加载类缺点:耗内存ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");UserService us=(UserService)ac.getBean("userService");us.sayHello();ByeService bs=(ByeService)ac.getBean("byeService");bs.SayBye();//方式2:BeanFactory(适用于手持设备等内存比较小设备上的项目)//使用BeanFactory获取bean,当只是实例化该容器,那么容器里的bean不会被实例化//只有使用getBean取某个bean时,bean才被实例化//bean工厂只把bean的定义信息加载进来,用到的时候才会实时实例化//优点:节约内存缺点:速度慢BeanFactory bf=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));UserService us2=(UserService)bf.getBean("userService");us2.sayHello();}}3.bean的生命周期
bean在spring Bean应用上下文中的生命周期(针对singleton的bean):
1) 实例化(程序加载beans.xml文件时,bean被实例化到内存);
2) 调用set方法设置属性;
3) 如果bean实现了BeanNameAware接口,则可通过setBeanName方法获得bean的ID;
4)如果bean实现了BeanFactoryAware接口,则可通过setBeanFactory方法获得bean工厂;
5) 如果bean实现了ApplicationContextAware接口,则可通过setApplicationContext方法获得bean工厂的上下文;
6)如果bean与一个后置处理器(一个实现BeanPostProcessor接口的后置处理器)关联,则会自动调用的后置处理器的预初始化方法postProcessBeforeInitialization;
7)如果bean实现了InitializingBean接口,则会调用afterPropertiesSet方法;
8)如果配置了自己的初始化方法<bean init-method="init" />,则会调用自己的初始化方法;
9)调用的后置处理器的后初始化方法postProcessAfterInitialization;
10)使用bean;
11)容器关闭;
12)可以通过实现DisposableBean接口来调用destory方法;·
13)也可以通过配置beans.xml文件<bean destroy-method="myDestory"/> 定制自己的myDestory方法。
实际开发中的流程往往只有:1) ->2) ->6) ->9) ->10) ->11)
- Spring:Spring入门和bean的生命周期
- Spring入门Blog[三、Bean的懒加载和生命周期]
- Spring入门Blog[三、Bean的懒加载和生命周期]
- Spring入门07 - Bean生命周期
- Spring Bean的生命周期
- Spring Bean的生命周期
- spring - bean的生命周期
- Spring Bean的生命周期
- spring bean 的生命周期
- Spring bean的生命周期
- spring bean的生命周期
- Spring Bean的生命周期
- Spring Bean的生命周期
- Spring bean的生命周期
- Spring Bean的生命周期
- Spring Bean的生命周期
- Spring bean的生命周期
- Spring Bean的生命周期
- React js 入门总结
- Martian Dollar
- 黑马程序员——C基础之逻辑运算符
- 日期控件DropdownDatePicker和DatePicker
- 读DL论文心得之Fast RCNN
- Spring:Spring入门和bean的生命周期
- servlet
- 黑马程序员——C基础之递归函数
- jackson 通用解析json方法
- 10006---Memcached--安装
- opencv2 accumulate()函数的使用,用于求几幅图像的平均值
- Hadoop读书笔记(1)
- C语言 时间戳和标准格式的转换
- PHP7 新鲜分享,Xampps 2.1.0 正式发布