本章不讲案例只讲概念 和一些spring属性 配置文件的其他配置
1.0 Sping 的 BeanFactory 容器
这是一个最简单的容器,它主要的功能是为依赖注入 (DI) 提供支持,这个容器接口在 org.springframework.beans.factory.BeanFactor 中被定义。
在资源宝贵的移动设备或者基于 applet 的应用当中, BeanFactory 会被优先选择。否则,一般使用的是 ApplicationContext,除非你有更好的理由选择 BeanFactory。
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory
(new ClassPathResource("Beans.xml"));
HelloWorld obj = (HelloWorld) factory.getBean("helloWorld");
obj.getMessage();
}
2.0 Spring ApplicationContext 容器
Application Context 是 spring 中较高级的容器。和 BeanFactory 类似,它可以加载配置文件中定义的 bean,将所有的 bean 集中在一起,当有请求的时候分配 bean。 另外,它增加了企业所需要的功能,比如,从属性文件从解析文本信息和将事件传递给所指定的监听器。
这个容器在 org.springframework.context.ApplicationContext interface 接口中定义。
ApplicationContext 包含 BeanFactory 所有的功能,一般情况下,相对于 BeanFactory,ApplicationContext 会被推荐使用。
BeanFactory 仍然可以在轻量级应用中使用,比如移动设备或者基于 applet 的应用程序。
最常被使用的 ApplicationContext 接口实现:
- FileSystemXmlApplicationContext():该容器从 XML 文件中加载已被定义的 bean。在这里,你需要提供给构造器 XML 文件的完整路径
ApplicationContext context = new FileSystemXmlApplicationContext
("C:/Users/ZARA/workspace/HelloSpring/src/Beans.xml");
ClassPathXmlApplicationContext():该容器从 XML 文件中加载已被定义的 bean。在这里,你不需要提供 XML 文件的完整路径只需正确配置 CLASSPATH 环境变量即可,因为,容器会从 CLASSPATH 中搜索 bean 配置文件。
用法上一节已经用过了WebXmlApplicationContext():该容器会在一个 web 应用程序的范围内加载在 XML 文件中已被定义的 bean。
Spring Bean 定义
Bean 定义
被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的。
bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象。
这些 bean 是由用容器提供的配置元数据创建的,例如,已经在先前章节看到的,在 XML 的表单中的 定义。
bean 定义包含称为配置元数据的信息,下述容器也需要知道配置元数据:
如何创建一个 bean
bean 的生命周期的详细信息
bean 的依赖关系
上述所有的配置元数据转换成一组构成每个 bean 定义的下列属性。
属性 描述
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 的容器被销毁时,使用回调方法。
AbstractApplicationContext只有 在进行调用销毁函数时候采用他作为应用程序上下文
具体的看下面xml
<?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-3.0.xsd">
<!-- 正常的bean -->
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- 延迟加载 -->
<bean id="..." class="..." lazy-init="true">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!--
下面的初始化和销毁方法需要返回的上下文是AbstractApplicationContex如下:
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
context.registerShutdownHook();
}
-->
<!-- 初始化的时候执行start()方法 -->
<bean id="..." class="..." init-method="start">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- 销毁的时候执行destroy()方法 -->
<bean id="..." class="..." destroy-method="destroy">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!--
同一个的意思是 Student stu = new Student();
Student stu1;
stu1 = stu;
新的意思是 Student stu = new Student();
Student stu1 = new Student();
-->
<!-- 下面是 singleton 作用域的配置 表明返回的对象是同一个 -->
<bean id="helloWorld"
class="com.tutorialspoint.HelloWorld"
scope="singleton">
</bean>
<!-- 下面是 prototype 作用域的配置 表明返回的对象是新的 -->
<bean id="helloWorld"
class="com.tutorialspoint.HelloWorld"
scope="singleton">
</bean>
</beans>
3.0 Spring——Bean 后置处理器
自己没有做测试 资料来源https://www.w3cschool.cn/wkspring/xs181ici.html
我的理解就是就是一种aop方法 可以方便的设置在bean的初始化前执行的函数和初始化完成后执行的函数
这里是 HelloWorld.java 文件的内容:
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
public void init(){
System.out.println("Bean is going through init.");
}
public void destroy(){
System.out.println("Bean will destroy now.");
}
}
这是实现 BeanPostProcessor 的非常简单的例子,它在任何 bean 的初始化的之前和之后输入该 bean 的名称。你可以在初始化 bean 的之前和之后实现更复杂的逻辑,因为你有两个访问内置 bean 对象的后置处理程序的方法。
这里是 InitHelloWorld.java 文件的内容:
package com.tutorialspoint;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.BeansException;
public class InitHelloWorld implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("BeforeInitialization : " + beanName);
return bean; // you can return any other object as well
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("AfterInitialization : " + beanName);
return bean; // you can return any other object as well
}
}
下面是 MainApp.java 文件的内容。在这里,你需要注册一个在 AbstractApplicationContext 类中声明的关闭 hook 的 registerShutdownHook() 方法。它将确保正常关闭,并且调用相关的 destroy 方法。
package com.tutorialspoint;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
context.registerShutdownHook();
}
}
下面是 init 和 destroy 方法需要的配置文件 Beans.xml 文件:
<?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-3.0.xsd">
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld"
init-method="init" destroy-method="destroy">
<property name="message" value="Hello World!"/>
</bean>
<bean class="com.tutorialspoint.InitHelloWorld" />
</beans>
一旦你创建源代码和 bean 配置文件完成后,我们就可以运行该应用程序。如果你的应用程序一切都正常,将输出以下信息:
BeforeInitialization : helloWorld
Bean is going through init.
AfterInitialization : helloWorld
Your Message : Hello World!
Bean will destroy now.
可以看出来 我们并没有调用InitHelloWorld方法,仅仅是在配置文件中注册了这个类,他就在被例化的bean初始化前后分别执行了
4.0 spring Bean定义继承
<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-3.0.xsd">
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
<property name="message1" value="消息1!"/>
<property name="message2" value="消息2!"/>
</bean>
<bean id="helloIndia" class="com.tutorialspoint.HelloIndia" parent="helloWorld">
<property name="message3" value="消息3!"/>
<property name="message4" value="消息4!"/>
</bean>
<!--
bean模板 必须有abstract="true"属性 说明是模板可以充当父模板 不能被实例化
在其他bean 中加上parent="beanTeamplate"即可继承
-->
<bean id="beanTeamplate" abstract="true">
<property name="message1" value="Hello World!"/>
<property name="message2" value="Hello Second World!"/>
<property name="message3" value="Namaste India!"/>
</bean>
</beans>
这里com.tutorialspoint.HelloIndia类中有message1 或着 message2 之一或者全部虽然在他所对应的bean中没有没有
为他赋值 但是由于继承了 上一个bean所以 也可以为他里面的message1或者message2赋值