Spring类加载(注解方式)


通过 Spring 注册的类一共只有三种加载方式!

环境: 
spring-context 4.2.6 
jdk 7 
Eclipse Neon for j2ee

最简单的配置

<?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: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-4.2.xsd  
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

    <!-- 扫描注解 -->
    <context:component-scan base-package="org.foo" />

</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

入口方法:

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringEntrance {
    public static void main(String[] args){
        ClassPathXmlApplicationContext resource = new ClassPathXmlApplicationContext(
                new String[]{
                        "applicationContext.xml"
                        }); 
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

项目结构:

src
└── main
   └── java
      ├── applicationContext.xml
      └── org
          └── foo
              ├── service
              │   ├── TestDemo.java
              │   └── Test.java
              └── SpringEntrance.java
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

单例+预加载(默认)

//项目一启动就产生一个且仅一个实例,即单例。
//并且,通过 @Autowired 只能获得这个单例。new Test()则不受单例限制
@Component
public class Test{

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

单例+懒加载

//项目启动时不加载
//仅当 TestDemo 类的 @Autowired Test 被扫描到时,才生成 Test 类的一个且仅一个实例。
//并且,通过 @Autowired 只能获得这个单例。new Test()则不受单例限制
@Lazy
@Component
public class Test{

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

  正确的加载时机

package org.foo.service;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TestDemo {
//禁止使用 @Autowired 标签加载 @Lazy 的类
//  @Autowired 
//  TestSingle testSingle;

    //通过 BeanFactory 接口创建实例
    @Autowired
    BeanFactory beanFactory;

    public void doSth(){
        //test 是 Test 类名首字母小写!一定要首字母小写!
        //只有运行到这里 Test 类才被实例化,延迟加载成功
        TestSingle ts=(TestSingle) beanFactory.getBean("test");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

  错误的加载时机:

//懒加载无效
//项目启动时 TestDemo 被加载时,扫描 @Autowired 标签,生成 Test 的单例
@Component
public class TestDemo{
    @Autowired // 项目启动时,这里就会创建 Test 的单例,Test 类的懒加载无效
    Test test; 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

多例+懒加载(仅支持懒加载)

定义

//每个 @Autowired 生成一个实例,可以有多个实例
@Scope("prototype")
//@Lazy //无论加不加 @Lazy,被 @Scope("prototype") 修饰的类都会 懒加载。
@Component
public class Test{

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

调用:

package org.foo.service;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TestDemo {
//禁止使用 @Autowired 标签加载 @Lazy 的类
//  @Autowired 
//  TestSingle testSingle;

    //通过 BeanFactory 接口创建实例
    @Autowired
    BeanFactory beanFactory;

    public void doSth(){
        Test test = null;
        for(int i=0;i<10;i++) {
            //test 是 Test 类名首字母小写!一定要首字母小写!
            //只有运行到这里 Test 类才被实例化,延迟加载成功
            test = (Test) beanFactory.getBean("test");
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

spring beanfactory类高级用法

  反射方式加载类

beanFactory.getBean(
            task.getHandler() + "UrlSpider", // 反射获得子类
            AbstractUrlSpider.class ); // 返回父类型
  • 1
  • 2
  • 3

需要注意的问题

构造函数里不支持 @Autowired 的实例

package org.foo.service;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TestDemo {
    @Autowired
    BeanFactory beanFactory;

    public TestDemo (){
        beanFactory.getBean("test",Test.class);
    }
}

猜你喜欢

转载自blog.csdn.net/wang1988081309/article/details/81004851
今日推荐