spring IOC容器

1.生命周期: init-method="init" destroy-method="destroy"在bean创建指定调用的方法,和销毁指定调用的方法
spring xml文件配置:

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下的代码:
public class hello {
private String massege;

public void getMassege() {
    System.out.println(this.massege);
}
public  void init(){
    System.out.println("我是初始化函数");
}
public  void destroy(){
    System.out.println("我是销毁函数");
}
public void setMassege(String massege) {
    this.massege = massege;
}

}

测试: 使用AbstractApplicationContext实现类调用context.registerShutdownHook();
public class test {
public static void main(String[] args) {
AbstractApplicationContext context=new ClassPathXmlApplicationContext("application.xml");
hello hello=(hello)context.getBean("test");
hello.setMassege("ni hao");
hello.getMassege();
context.registerShutdownHook();
}
}
2.spring后置处理器:BeanPostProcessor 对任何一个bean初始化之前之后进行一些逻辑操作,需要在xml文件加入实现BeanPostProcessor的bean的配置。
package com.shuai.hello;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class processorHello implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println(beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println(beanName);
return bean;
}
}
3.bean xml中的继承:parent






public class test {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
child c= (child) applicationContext.getBean("child");
c.getMessage2();
parent p= (parent) applicationContext.getBean("parent");
p.getMessage2();
p.getMessage3();
}
}
当parent调用getMessage2的时候调用的是child继承过来的方法,相当于parent类继承了child类

你可能感兴趣的:(spring IOC容器)