Spring框架入手学习(二)

前言

继续上一篇的Spring话题学习,这篇将深入讲解Spring的核心API跟Bean的几种实例方法。


正文

1.1 核心API

图片说明

BeanFactory :这是一个工厂,用于生成任意bean。
采取延迟加载,第一次getBean时才会初始化Bean
ApplicationContext:是BeanFactory的子接口,功能更强大。(国际化处理、事件传递、Bean自动装配、各种不同应用层的Context实现)。当配置文件被加载,就进行对象实例化。
ClassPathXmlApplicationContext 用于加载classpath(类路径、src)下的xml,加载xml运行时位置 –> /WEB-INF/classes/…xml
FileSystemXmlApplicationContext 用于加载指定盘符下的xml,加载xml运行时位置 –> /WEB-INF/…xml
通过java web ServletContext.getRealPath() 获得具体盘符

    @Test
    public void demo02(){
        //使用BeanFactory  --第一次条用getBean实例化
        String xmlPath = "com/itheima/b_di/beans.xml";

        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(xmlPath));

        BookService bookService = (BookService) beanFactory.getBean("bookServiceId");

        bookService.addBook();

    }

1.2 Bean实例化方式

  3种bean实例化方式:默认构造、静态工厂、实例工厂

1.2.1 默认实例化方式

<bean id="" class="">  必须提供默认构造  

 这里就不在多说了,前一篇,一般情况下, 采用的就是默认构造,不需要添加过多的代码。

1.2.2 静态工厂实例化方式

 常用与spring整合其他框架(工具)
 静态工厂:用于生成实例对象,所有的方法必须是static

<bean id=""  class="工厂全限定类名"  factory-method="静态方法">

具体代码:

public interface UserService {
   public void addUser();
}
public class UserServiceImpl implements UserService{

    @Override
    public void addUser() {
        // TODO Auto-generated method stub
        System.out.println("b_static_factory");
    }
}

工厂类:

public class MyBeanFactory {

    /**
     * 静态工厂
     * @return
     */
    public static UserService createUserService(){
        return new UserServiceImpl();
    }
}

创建一个静态工厂类,里面的方法都是static静态方法,返回一个实例

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.xsd">
    <!-- 
           将静态工厂创建实例交给spring
           class  静态工厂全限定类名
           factory-method 确定静态方法
    -->
    <bean id="userServiceId"         
      class="com.shao.c_inject.b_static_factory.MyBeanFactory"  
      factory-method="createUserService"></bean>
</beans>

1.2.3 实例工厂实例化方式
 实例工厂:必须先有工厂实例对象,通过实例对象创建对象。提供所有的方法都是“非静态”的。

工厂类:

public class MyBeanFactory {

    /**
     * 实例工厂
     * @return
     */
    public  UserService createUserService(){
        return new UserServiceImpl();
    }
}

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.xsd">
    <!-- 创建工厂实例交给spring -->
    <bean id ="myBeanFactoryId"   
        class="com.shao.c_inject.c_factory.MyBeanFactory"></bean>
    <!-- 
                获取userService
          factory-bean 获取工厂实例
          factory-method 确定普通方法
    -->

    <!-- 获得userService -->
    <bean id="userServiceId" factory-bean = "myBeanFactoryId" 
    factory-method="createUserService"></bean>
</beans>

2 .Bean种类

普通bean:之前操作的都是普通bean。 ,spring直接创建A实例,并返回FactoryBean:是一个特殊的bean,具有工厂生成对象能力,只能生成特定的对象。
bean必须使用 FactoryBean接口,此接口提供方法 getObject() 用于获得特定bean。
先创建FB实例,使用调用getObject()方法,并返回方法的返回值
FB fb = new FB();
return fb.getObject();


BeanFactory 和 FactoryBean 对比?
BeanFactory:工厂,用于生成任意bean。
FactoryBean:特殊bean,用于生成另一个特定的bean。例如:ProxyFactoryBean ,此工厂bean用于生产代理。 获得代理对象实例。AOP使用

2.1 作用域
作用域:用于确定spring创建bean实例个数

图片说明
取值:
singleton 单例,默认值。
prototype 多例,每执行一次getBean将获得一个实例。例如:struts整合spring,配置action多例。
配置信息:

<bean id="userServiceId" class="com.shao.c_inject_scope.UserServiceImpl"  
    scope= "prototype"></bean>
public class TestIoc {

     @Test
     public void demo1(){
         //之前定的方法
         UserService userService = new UserServiceImpl();
         userService.addUser();
     }

     @Test
     public void demo2(){
         //
         String xmlPath = "com/shao/c_inject_scope/beans.xml";
         ApplicationContext applicationContext = new  
                      ClassPathXmlApplicationContext(xmlPath);
         UserService userService = (UserService) 
                      applicationContext.getBean("userServiceId");
         UserService userService2 = (UserService) 
                      applicationContext.getBean("userServiceId");
         System.out.println(userService); 
         System.out.println(userService2);
     }
}

输出:

八月 16, 2017 10:48:47 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3abfe836: startup date [Wed Aug 16 10:48:47 CST 2017]; root of context hierarchy
八月 16, 2017 10:48:47 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/shao/c_inject_scope/beans.xml]
com.shao.c_inject_scope.UserServiceImpl@9660f4e
com.shao.c_inject_scope.UserServiceImpl@5a8806ef
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值