好几日没有读东西了,今天本来要读mybatis原理部分,但是看到作者讲,只是学会用不用学那么深,遂直接开干spring,工作中一直用springboot,框架都是领导搭好的,每天的任务就是增删改查,挺无脑的。
本周把spring重新拾起,唤醒我的记忆,以前都是跟着视频课吭呲吭呲学过。记得感觉hibernate和sturt2很难,没办法都得学,争取在一个月内搞定。其实最大的敌人是我的记忆力,很多知识记过都忘了,所以我依照樊登所讲,绘制脑图。学完ssm+ssh,向算法进攻。
spring是一个容器,是一个大管家,从前我们需要手动new对象,使用spring后,所以的bean对象统一由大管家管理,我们需要时就向他要,他会把所需要的bean对象主动注入。
spring入门第一步运行起一个最简单demo
spring运行核心的4个jar包
- spring-beans-4.0.0.RELEASE.jar
- spring-context-4.0.0.RELEASE.jar
- spring-core-4.0.0.RELEASE.jar
- spring-expression-4.0.0.RELEASE.jar
- commons-logging-1.2.jar 注意这个日志包不是spring中lib所带的,需要独立下载,今天在公司捣鼓项目,就没运行起来,看了别人博客才知道这个也是必须的。
spring生命周期
- 把实现了BeanPostProcessor接口的Bean,重写接口方法并将Bean加入到配置文件中,可以非常明显的打印两个方法中内容,postProcessBeforeInitialization()在前,postProcessAfterInitialization()在后,书中说这个时候bean就完成了初始化,生存Spring IOC容器之中
- 所有的Spring IOC容器的最低要求是BeanFactory接口,如果采用了非ApplicationContext子类创建的IOC容器,那么即使Bean实现了ApplicationContextAware接口实现了setApplicationContext()方法也不在生命周期中被调用。
代码演示:
//实现了BeanPostProcessor的类,必须加入xml文件bean中 public class BeanPostProcessorImpl implements BeanPostProcessor { @Override public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException { System.out.println("【"+arg0.getClass().getSimpleName()+"】对象"+"实例化完成"); return arg0; } @Override public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException { System.out.println("【"+arg0.getClass().getSimpleName()+"】对象"+arg1+"开始实例化"); return arg0; } } //sourou形容一杯果汁 public class Source { private String size; private String sugar; private String Fruit; public String getSize() { return size; } public void setSize(String size) { this.size = size; } public String getSugar() { return sugar; } public void setSugar(String sugar) { this.sugar = sugar; } public String getFruit() { return Fruit; } public void setFruit(String fruit) { Fruit = fruit; } @Override public String toString() { return "Source [size=" + size + ", sugar=" + sugar + ", Fruit=" + Fruit + "]"; } } //实现了生命周期中相关的接口的bean public class JuiceMaker2 implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean,DisposableBean{ public JuiceMaker2() { System.out.println("hello"); } private String beverageShop = null; private Source source = null; public String getBeverageShop() { return beverageShop; } public void setBeverageShop(String beverageShop) { this.beverageShop = beverageShop; } public Source getSource() { return source; } public void setSource(Source source) { this.source = source; } public String makeJuiec() { // TODO Auto-generated method stub String juice = "这是一杯由"+beverageShop+"饮品店,提供的"+source.getSize()+source.getSugar()+source.getFruit(); return juice; } public void init() { System.out.println("【"+this.getClass().getSimpleName()+"】"+"执行自定义初始化方法"); } public void mydestory() { // TODO Auto-generated method stub System.out.println("【"+this.getClass().getSimpleName()+"】"+"执行自定义销毁方法"); } @Override public void setBeanName(String arg0) { System.out.println("【"+this.getClass().getSimpleName()+"】"+"的setBeanName方法"); } @Override public void setBeanFactory(BeanFactory arg0) throws BeansException { System.out.println("【"+this.getClass().getSimpleName()+"】"+"的setBeanFactory方法"); } @Override public void setApplicationContext(ApplicationContext arg0) throws BeansException { System.out.println("【"+this.getClass().getSimpleName()+"】"+"的setApplicationContext方法"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("【"+this.getClass().getSimpleName()+"】"+"的afterPropertiesSet方法"); } @Override public void destroy() throws Exception { System.out.println("【"+this.getClass().getSimpleName()+"】"+"的destroy方法"); } } //xml文件 <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="" class="com.houjun.pojo.BeanPostProcessorImpl"></bean> <bean id="source" class="com.houjun.pojo.Source"> <property name="fruit" value="橙汁"></property> <property name="sugar" value="少糖"></property> <property name="size" value="大杯"></property> </bean> <bean class="com.houjun.pojo.JuiceMaker2" id="juiceMaker2" init-method="init" destroy-method="mydestory"> <property name="beverageShop" value="贡茶"></property> <property name="source" ref="source"></property> </bean> </beans> //程序入口 @Test public void test() { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); JuiceMaker2 juice = applicationContext.getBean("juiceMaker2",JuiceMaker2.class); System.out.println(juice.makeJuiec()); applicationContext.close(); }