spring 系列 1-bean标签

Spring是分层的Java SE/EE应用 ful-stack轻量级开源框架,以IoC(Inverse Of Control:反转控制)和AOP(Aspect Oriented Programming:面向切面编程)为内核,提供了展现层SpringMVC和持久层Spring JDBC以及业务层事务管理等众多的企业级应用技术,还能整合开源世界众多著名的第三方框架和类库,逐渐成为使用最多的Java EE 企业应用开源框架。

bean标签

    <!--配置service-->
    <bean id="accountService" class="com.mantishell.service.impl.AccountServiceImpl"
          scope="prototype" init-method="init" destroy-method="destroy">
    </bean>
    <!--配置dao-->
    <bean id="accountDao" class="com.mantishell.dao.impl.AccountDaoImpl"></bean>

作用

用于配置对象,让spring来创建对象,并存入ioc容器中。
默认情况下,它调用的是类中无参构造函数。如果没有无参构造函数则不能创建成功。

属性

id:给对象在容器中提供一个唯一标识。用于获取对象。
class:指定类的全限定类名。用于反射创建对象,默认调用无参构造函数。
scope:
    * singleton:默认值,单例
    * prototype:多例
    * request:WEB项目中,Spring创建一个Bean的对象,将对象存入到request域中
    * session:WEB项目中,Spring创建一个Bean的对象,将对象存入到session域中
    * global session:WEB 项目中,应用在Porduct环境.如果没有Porduct环境,那么globalSession相当于session
init-method:指定类中的初始化方法名称
destroy-method:指定类中销毁方法名称

bean的作用和生命周期

  • 单例对象:scope="singleton"
    • 一个应用只有一个对象的实例。作用范围是整个引用。
    • 生命周期:
      • 对象出生:当应用加载,创建容器时,对象就被创建了。
      • 对象或者:只要容器在,对象一直活着。
      • 对象死亡:当应用卸载,销毁容器时,对象就被销毁了。
  • 多例对象:scope="prototype"
    • 每次访问对象时,都会重新创建对象实例
    • 生命周期:
      • 对象出生:当使用对象时,创建新的对象实例。
      • 对象或者:只要对象在使用中,对象一直活着。
      • 对象死亡:当对象长时间不用时,被java的垃圾回收期收回。

假设配置文件叫bean.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.xsd">
    <!--配置service-->
    <bean id="accountService" class="com.mantishell.service.impl.AccountServiceImpl"
          scope="prototype" init-method="init" destroy-method="destroy">
    </bean>
    <bean id="accountDao" class="com.mantishell.dao.impl.AccountDaoImpl"></bean>
</beans>

创建对象

        //1、获取核心容器对象
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2、根据id获取Bean对象
        IAccountService as  = (IAccountService)ac.getBean("accountService");
        //也可以传入class字节码,这样就不需要强转了。
        IAccountDao adao = ac.getBean("accountDao",IAccountDao.class);

ApplicationContext的三个常用实现类

  • ClassPathXmlApplicationContext:它可以加载类路径下的配置文件,要求配置文件必须在类路径下。不在的话,加载不了。(更常用)
  • FileSystemXmlApplicationContext:它可以加载磁盘任意路径下的配置文件(必须有访问权限)
  • AnnotationConfigApplicationContext:它是用于读取注解创建容器

ApplicationContext和BeanFactory的区别

BeanFactory是Spring容器的顶层接口,ApplicationContext是它的子接口

  • ApplicationContext: 单例对象适用,它在构建核心容器时,创建对象采取的策略是采用立即加载的方式。也就是说,只要一读取完配置文件马上就创建配置文件中配置的对象。
  • BeanFactory: 多例对象使用,它在构建核心容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,什么时候根据id获取对象了,什么时候才真正的创建对象。

实例化Bean的三种方式

  • 使用默认无参数构造函数
    <!--默认情况下:它会根据默认无参构造函数来创建类的对象。如果bean中没有无参构造函数,则创建失败。-->
    <bean id="accountService" class="com.mantishell.service.impl.AccountServiceImpl"/>
  • spring管理静态工厂-使用静态工厂的方法创建对象
    public class StaticFactory{
        public static IAccountService createAccountService(){
            return new AccountServiceImpl();
        }
    }
    <!--使用StaticFactory类中的静态方法createAccountService创建对象,并存入spring容器。
        id属性:指定bean的id,用于从容器中获取
        class属性:指定静态工厂的全限定类名
        factory-method属性:指定生产对象的静态方法
    -->
    <bean id="accountService" class="com.mantishell.factory.StaticFactory" factory-method="createAccountService"/>
  • spring管理实例工厂-使用实例工厂的方法创建对象
/**
 * 模拟一个工厂类
 * 此工厂创建对象,必须先有工厂实例对象,再调用方法
 */
public class InstanceFactory {
    public IAccountService createAccountService(){
        return new AccountServiceImpl();
    }
}
<!--先把工厂的创建交给spring来管理,然后在使用工厂的bean来调用里面的方法,factory-bean属性:用于指定实例工厂bean的id;factory-method属性:用于指定实例工厂中创建对象的方法。-->
<bean id="accountService" class="com.mantishell.factory.StaticFactory" factory-method="getAccountService"/>
原文地址:https://www.cnblogs.com/mantishell/p/12548639.html