<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd" default-init-method="init" default-destroy-method="destory” > <!--<bean id="helloWorld" class="demo.demo" init-method="initmethod" destroy-method="destroymethod">--> <bean id="helloWorld" class="demo.demo"> <property name="message" value="Hello World!"/> </bean> <bean id="hello meitian" parent="helloWorld"> <property name="message" value="hello meitian"/> </bean> </beans>
属性 | 描述 |
id | bean的名字,容器中通过getBean()获取对象的入参标识 |
class | 绑定的具体实现类的位置;这个属性是强制性的,并且指定用来创建 bean 的 bean 类。 |
name | 这个属性指定唯一的 bean 标识符。在基于 XML 的配置元数据中,你可以使用 ID 和/或 name 属性来指定 bean 标识符。 |
scope | 这个属性指定由特定的 bean 定义创建的对象的作用域,它将会在 bean 作用域的章节中进行讨论。 |
constructor-arg | 它是用来注入依赖关系的,构造函数初始化需要参数的时候用到这个 |
properties | 它是用来注入依赖关系的 |
autowiring mode | 它是用来注入依赖关系的 |
lazy-initialization mode | 延迟初始化的 bean 告诉 IoC 容器在它第一次被请求时,而不是在启动时去创建一个 bean 实例。 |
initialization 方法 | 在 bean 的所有必需的属性被容器设置之后,调用回调方法。 |
destruction 方法 | 当包含该 bean 的容器被销毁时,使用回调方法。 |
parent |
可以指定一个父bean,当前这个bean没有定义的属性赋值等操作,都会使用parent指定bean的内容
当然如果当前bean已经覆盖的方法,会优先用自己的
|
AbstractApplicationContext context = new FileSystemXmlApplicationContext("//opt/script/zzappcicd/src/main/resources/Beans.xml"); demo obj = (demo) context.getBean("helloWorld”);
1)Bean单独的初始化方法:init-method,销毁方法:destroy-method
public class initbean implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object o, String s) throws BeansException { System.out.println("BeforeInitialization : " + s); return o; } @Override public Object postProcessAfterInitialization(Object o, String s) throws BeansException { System.out.println("postProcessAfterInitialization : " + s); return o; } }
原文:https://www.cnblogs.com/meitian/p/13339389.html