spring教程笔记2
IOC(inverse of controll)
<?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"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- 配置bean -->
<bean id="student" scope="singleton" class="com.hsp.ioc.Student">
<property name="name" value="小猪" />
</bean>
</beans>
package com.hsp.ioc;//java类都有一个默认的无参构造函数
public class Student {String name;public Student(){System.out.println("student被创建");}public String getName() {return name;}public void setName(String name) {//this.name = name;System.out.println("输入值"+name);}}
package com.hsp.ioc;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.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;public class App1 {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stub//从ApplicationContext中取bean//ApplicationContext ac=new ClassPathXmlApplicationContext("com/hsp/ioc/beans.xml");//当我们去实例化beans.xml,该文件中配置的bean被实例(该bean scope是 singleton)//从bean中取出student//通过文件路径来获取ApplicationContext ac=new FileSystemXmlApplicationContext("src\\com\\hsp\\ioc\\beans.xml");//获取两个studentStudent s1=(Student) ac.getBean("student");Student s2=(Student) ac.getBean("student");System.out.println(s1+" "+s2);//如果我们使用beanfactory去获取bean,当你只是实例化该容器, 那么//容器的bean不被实例化,只有当你去使用getBean某个bean时,才会实时的创建.// BeanFactory factory = new XmlBeanFactory(
// new ClassPathResource("com/hsp/ioc/beans.xml"));
// factory.getBean("student");//软件发展<硬件}}
结论:
1.如果使用ApplicationContext ,则配置的bean如果是 singlton不管你用不用,都被实例化.(好处就是可以预先加载,缺点就是耗内存)
2.如果是 BeanFactory ,则当你获取beanfacotry时候,配置的bean不会被马上实例化,当你使用的时候,才被实例(好处节约内存,缺点就是速度)
3.规定: 一般没有特殊要求,应当使用ApplicatioContext完成(90%)
bean的生命周期
为什么总是一个生命周期当做一个重点?
Servlet -> servlet生命周期 init() destory()
java对象生命周期.
往往笔试,面试总喜欢问生命周期的问题
<?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"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><bean id="personService" destroy-method="mydestory" class="com.hsp.beanlife.PersonService">
<!-- 这里注入我们属性,前提就是有setName才能ok -->
<property name="name">
<value>xiaoming</value>
</property>
</bean><!-- 配置我们的自己后置处理器(有点类似我们的filter) -->
<bean id="myBeanPostProcessor" class="com.hsp.beanlife.MyBeanPostProcessor" />
</beans>
package com.hsp.beanlife;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;public class App1 {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stub// ApplicationContext ac=new ClassPathXmlApplicationContext("com/hsp/beanlife/beans.xml");BeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/hsp/beanlife/beans.xml"));PersonService ps=(PersonService) factory.getBean("personService");ps.sayHi();}}
package com.hsp.beanlife;import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;public class PersonService implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean {private String name;private Integer age;public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getName() {return name;}public PersonService(String abc){System.out.println("PersonService 函数");}public PersonService(){System.out.println("PersonService 函数");}public void setName(String name) {System.out.println("setName(String name) 函数");this.name = name;}public void sayHi(){System.out.println("hi "+ name);}//该方法可以给arg0表示正在被实例化得bean idpublic void setBeanName(String arg0) {// TODO Auto-generated method stubSystem.out.println("setBeanName 被调用 值"+arg0);}//该方法可以传递beanFactroypublic void setBeanFactory(BeanFactory arg0) throws BeansException {// TODO Auto-generated method stubSystem.out.println("setBeanFactory "+arg0);}//该方法传递ApplicationContextpublic void setApplicationContext(ApplicationContext arg0)throws BeansException {// TODO Auto-generated method stubSystem.out.println("setApplicationContext"+arg0);}//也可以通过注解的方式来配置哪个方法init-method@PostConstructpublic void init(){System.out.println("我自己的init方法");}// public void afterPropertiesSet() throws Exception {// TODO Auto-generated method stubSystem.out.println("afterPropertiesSet()");}//定制我们的销毁方法@PreDestroypublic void mydestory(){System.out.println("释放各种资源");}}
装配bean
spring教程笔记2相关推荐
- 动力节点—2020最新Spring教程笔记(上)
文章目录 1 Spring 概述 2 IOC 控制反转 2.1 基于XML的DI 2.1.1 注入分类 2.1.1.1 set注入 2.1.1.2 构造注入(了解) 2.1.2 自动注入 2.2 基于 ...
- Java基础知识 廖雪峰教程笔记
Java基础知识 Java基础知识 java简介 名词解释 运行Java程序 Java基本数据类型 模块 包装类型 记录类 异常处理 Java异常 使用Commons Logging 使用log4j ...
- 【Spring学习笔记-MVC-13.2】Spring MVC之多文件上传
作者:ssslinppp 1. 摘要 前篇文章讲解了单文件上传<[Spring学习笔记-MVC-13]Spring MVC之文件上传>http://www.cnblogs.co ...
- Spring读书笔记——bean创建(下)
有关Spring加载bean系列,今天这是最后一篇了,主要接上篇对于从Spring容器中获取Bean的一些细节实现的补充. <Spring读书笔记--bean加载>--Spring如何加载 ...
- Spring学习笔记(三) AOP_annotation,AOP_XML
在学习课程以前,听说AOP有种很神秘的感觉,好像很好深的技术.其实原理很简单,使用动态代理的方式给程序增加逻辑.与此相似的有struts2中的filter拦截器. 再讲AOP之前先把需求说一下: 同S ...
- 台湾国立大学郭彦甫Matlab教程笔记(22) Cramer's method(Inverse matrix逆矩阵法)
台湾国立大学郭彦甫Matlab教程笔记(22) Cramer's method(Inverse matrix) matrix left division左除:\ or mldivide() solvi ...
- 台湾国立大学郭彦甫Matlab教程笔记(21)linear equations(高斯消去法和追赶法)
台湾国立大学郭彦甫Matlab教程笔记(21) today: linear equation 线性方程 linear system 线性系统 我们先看第一部分 linear equation 假定一个 ...
- 台湾国立大学郭彦甫Matlab教程笔记(20) root finding(numeric)
台湾国立大学郭彦甫Matlab教程笔记(20) root finding(numeric) symbolic vs. numeric符号法和数值法的区别对比 symbolic 1)advantages ...
- 台湾国立大学郭彦甫Matlab教程笔记(17)numerical integration
台湾国立大学郭彦甫Matlab教程笔记(17)numerical integration 数值积分 calculating the numerical value of a definite inte ...
- 台湾国立大学郭彦甫Matlab教程笔记(16) 数值微分 numerical differentiation
台湾国立大学郭彦甫Matlab教程笔记(16) 数值微分 numeric differentiation 复习:diff()函数用来计算vector前后 entry的差异 数值微分继续 various ...
最新文章
- Redis常见面试题总结
- 【QM-06】Material Specification(物料说明)续
- python函数使用易错点_Python易错例题
- Boost:基于Boost的阻塞tcp echo的测试程序
- 关于SET和UNORDER_SET
- log4j 标准配置模板:
- 程序员面试金典 - 面试题 04.06. 后继者(循环中序遍历)
- iPhone SE 3售价曝光:搭载A15芯片 价格依旧香
- Atitit q2016 q4 doc list on home ntpc.docx
- windows slim read/write lock 原理剖析
- python爬斗鱼直播房间名和主播名_python3爬取斗鱼某些版块的主播人气
- 《植物大战僵尸》游戏存档修改
- 苹果推送通知办事教程 Apple Pushnb
- 2012网易校园招聘笔试题
- 麒麟系统云打印安装指导
- put请求400状态码
- Java服务器页面:JSP
- 【Apple Music如何开通学生会员:使用学校邮箱辅助验证】
- 字符串关键字的散列映射
- linux-ubuntu常用命令(深圳文鹏)