彻底搞懂Spring类加载(注解方式)

摘要:
单例预加载默认单例懒加载  正确的加载时机  错误的加载时机多例懒加载仅支持懒加载springbeanfactory类高级用法  反射方式加载类需要注意的问题通过Spring注册的类一共只有三种加载方式!newTest()则不受单例限制@Lazy@ComponentpublicclassTest{}正确的加载时机packageorg.foo.service;importjavax.annotation.PostConstruct;importorg.springframework.beans.factory.BeanFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;@ServicepublicclassTestDemo{//禁止使用@Autowired标签加载@Lazy的类//@Autowired//TestSingletestSingle;//通过BeanFactory接口创建实例@AutowiredBeanFactorybeanFactory;publicvoiddoSth(){//test是Test类名首字母小写!
  • 单例预加载默认
  • 单例懒加载
    •   正确的加载时机
    •   错误的加载时机
  • 多例懒加载仅支持懒加载
  • spring beanfactory类高级用法
    •   反射方式加载类
  • 需要注意的问题

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

环境:
spring-context 4.2.6
jdk 8
Eclipse 4.7

最简单的配置

<?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>

入口方法:

importorg.springframework.context.support.ClassPathXmlApplicationContext;

public classSpringEntrance {
    public static voidmain(String[] args){
        ClassPathXmlApplicationContext resource = newClassPathXmlApplicationContext(
                newString[]{
                        "applicationContext.xml"}); 
    }
}

项目结构:

src
└── main
   └── java
      ├── applicationContext.xml
      └── org
          └── foo
              ├── service
              │   ├── TestDemo.java
              │   └── Test.java
              └── SpringEntrance.java
单例+预加载(默认)
//项目一启动就产生一个且仅一个实例,即单例。
//并且,通过 @Autowired 只能获得这个单例。new Test()则不受单例限制
@Component
public classTest{

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

}

正确的加载时机

packageorg.foo.service;

importjavax.annotation.PostConstruct;

importorg.springframework.beans.factory.BeanFactory;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;

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

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

    public voiddoSth(){
        //test 是 Test 类名首字母小写!一定要首字母小写!
        //只有运行到这里 Test 类才被实例化,延迟加载成功
        TestSingle ts=(TestSingle) beanFactory.getBean("test");
    }
}

错误的加载时机:

//懒加载无效
//项目启动时 TestDemo 被加载时,扫描 @Autowired 标签,生成 Test 的单例
@Component
public classTestDemo{
    @Autowired //项目启动时,这里就会创建 Test 的单例,Test 类的懒加载无效
Test test; 
}
多例+懒加载(仅支持懒加载)

定义

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

}

调用:

packageorg.foo.service;

importjavax.annotation.PostConstruct;

importorg.springframework.beans.factory.BeanFactory;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;

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

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

    public voiddoSth(){
        Test test = null;
        for(int i=0;i<10;i++) {
            //test 是 Test 类名首字母小写!一定要首字母小写!
            //只有运行到这里 Test 类才被实例化,延迟加载成功
            test = (Test) beanFactory.getBean("test");
        }
    }
}
spring beanfactory类高级用法

  反射方式加载类

beanFactory.getBean(
            task.getHandler() + "UrlSpider", //反射获得子类
            AbstractUrlSpider.class ); //返回父类型
需要注意的问题

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

packageorg.foo.service;

importorg.springframework.beans.factory.BeanFactory;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;

@Service
public classTestDemo {
    @Autowired
    BeanFactory beanFactory;

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

免责声明:文章转载自《彻底搞懂Spring类加载(注解方式)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇VM安装后没有桥链接协议解决方法[转]FPGA使用LVDS差分信号的一些注意事项下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Linux中计划任务执行脚本crontab-简洁版

我使用的是ubuntu16,所以在ubuntu中一切正常,在其他linux系统中应该都差不多。   1 计划任务,crontab命令选项:     -u指定一个用户,     -l列出某个用户的任务计划,     -r删除某个用户的任务,     -e编辑某个用户的任务   2 cron文件语法:     分     小时   日       月    ...

Postgresql在线备份和恢复

1.实验环境 OS: RedHat Linux Enterprisedb 6.3 DB: postgresql 9.3 PGHOME: /opt/PostgreSQL/9.3 PGDATA: /opt/PostgreSQL/9.3/data 归档目录:/opt/pg_archive 基础备份目录:/opt/base_archive --生产环境中归档和数...

alertmanager

alertmanager主要用于接收prometheus发送的告警信息; wget下载,解压, 配置alertmanager.yml,内容如下; 在prometheus文件下添加rules.yml内容如下: groups: - name: test-rules rules: - alert: InstanceDown expr: up == 0 for:...

EBS的性能调优

metalink    Tuning performance on eBusiness suite (Doc ID 744143.1) 这篇文档描述了如何调查电子商务套件的整体性能下降。特别是,我们强调最普遍的等待时间和如何在AWR/ Statspack 报表中理解它们。在最后,我们提供了在数据库层/应用层性能调优的最佳实践。 1. 确保对eBusines...

java——testNG——工作复习——xml详解

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="suitename"junit="false"verbose="3"parallel="false"thr...

深入Razor,你准备好了吗?(兼谈我的学习方法和定位)

目录 1 学习方法论         1下定义-针对不同的定义做不同的学习         2如何学习        3 知其所以然的学习    3MVC中页面引擎的入口 4我的定位  (主观的看法很多,说的不好不对的地方请多多指教) 1学习方法 给你要做的事下个定义        我的博士导师跟我说: 如果给问题下准了定义,那么问题就算解决了60%...