Spring 学习笔记03

Posted 月光莫里亚

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 学习笔记03相关的知识,希望对你有一定的参考价值。

Spring 容器

spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory的子接口,它们都代表Spring容器,

spring 容器是生产Bean实例的工厂,并管理容器中的Bean。

1.BeanFactory

BeanFactory 的常用实现类是DefaultListableBeanFactory,下面是简单的使用:

public class SpringContainer {

    public static void main(String[] args) {
        Resource rs = new ClassPathResource("bean.xml");
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        //加载xml
        new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions(rs);
        Driver driver = beanFactory.getBean("driver",Driver.class);
        driver.driverCar();
    }
}

配置文件:

<?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:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"
        >
    <bean id="driver" class="com.huan.spring.bean.Driver">
        <constructor-arg ref="car"/>
    </bean>
    <bean id="car" class="com.huan.spring.bean.Car"/>


</beans>

Driver类和Car类:

public class Driver {

    private Car car;

    public Driver(Car car) {
        this.car = car;
    }

    public void driverCar() {
        System.err.println(car.carName());
    }
}
public class Car {

    public String carName() {
        return "this is Jeep";
    }
}

2.ApplicationContext

大多数情况下都是使用applicationContext作为spring容器,它拥有Beanfactory的所有功能且拥有额外的功能:

如:默认情况下会预初始化所有的singleton Bean、继承MessageSource接口提供国际化、事件机制、

同时加载多个配置文件等

修改代码验证预初始化:

public class Driver {

    private Car car;

    public Driver(Car car) {
        System.out.println("=====构造器设值===="+car);
        this.car = car;
    }

    public void driverCar() {
        System.err.println(car.carName());
    }
}
public class Car {

    public Car(){
        System.out.println("=========预初始化car======");
    }

    public String carName() {
        return "this is Jeep";
    }
}
package com.huan.spring.base;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.huan.spring.bean.Driver;

public class SpringContainer {

    public static void main(String[] args) {
        //userBeanFactory();
        userApplicationContext();
    }


    private static void userApplicationContext() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    }


    private static void userBeanFactory() {
        Resource rs = new ClassPathResource("bean.xml");
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        //加载xml
        new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions(rs);
        Driver driver = beanFactory.getBean("driver",Driver.class);
        driver.driverCar();
    }
}

SpringContainer 中创建ApplicationContext成功后并没有获取Bean

可见使用ApplicationContext创建容器后会调用无参构造并注入依赖,而使用Beanfactory在不获取Bean的情况下是不会

进行调用无参构造以及注入依赖的

如果需要阻止Spring容器初始化容器中的singleton Bean,可以在<bean>中设置属性lazy-init="true"

3.ApplicationContext事件机制

ApplicationContext的事件机制主要ApplicationEvent类和ApplicationListener接口实现

  • ApplicationEvent:容器事件,必须由ApplicationContext发布
  • ApplicationListener:监听器,可有任何监听器Bean担任

 

public class EmailEvent extends ApplicationEvent{

    private String address;
    private String text;

    public EmailEvent(Object source) {
        super(source);
    }

    public EmailEvent(Object source, String address, String text) {
        super(source);
        this.address = address;
        this.text = text;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }



}
public class EmailNotifier implements ApplicationListener<EmailEvent>{

    @Override
    public void onApplicationEvent(EmailEvent event) {
        System.out.println("邮件地址:==="+event.getAddress());
        System.out.println("邮件内容==="+event.getText());
    }

}

 

public class EventTest {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
        //创建event
        EmailEvent emailEvent = new EmailEvent("test", "[email protected]", "test event");
        ctx.publishEvent(emailEvent);
    }
}

 

<?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:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"
        >
    <bean id="driver" class="com.huan.spring.bean.Driver" >
        <constructor-arg ref="car"/>
    </bean>
    <bean id="car" class="com.huan.spring.bean.Car"/>

    <bean  class="com.huan.spring.base.EmailNotifier"/>

</beans>

当然spring中也内置了一些事件,如RequestHandledEvent。

4.Bean如何获取Spring容器

获取spring容器可以实现BeanFactoryAware接口或者ApplicationContextAware接口

public class Book implements ApplicationContextAware {

    private ApplicationContext ctx;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.ctx = applicationContext;
    }

    public void printCtx() {
        System.out.println(ctx);
    }

}
public class BookTest {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
        Book book = ctx.getBean("book", Book.class);
        System.out.println(ctx);
        book.printCtx();
    }
}
<?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:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"
        >
    <bean id="driver" class="com.huan.spring.bean.Driver" >
        <constructor-arg ref="car"/>
    </bean>
    <bean id="car" class="com.huan.spring.bean.Car"/>

    <bean  class="com.huan.spring.base.EmailNotifier"/>
    <bean id="book" class="com.huan.spring.base.Book"/>

</beans>

 

以上是关于Spring 学习笔记03的主要内容,如果未能解决你的问题,请参考以下文章

Spring-IOC学习笔记-03DI依赖注入

Spring-AOP学习笔记-03通知

[原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等(代码片段

spring学习笔记AOP

Spring 学习笔记

Spring学习笔记--Spring IOC