Spring积累

【Spring-Framework】积累与发现 Spring-context AOP 细说Spring——AOP详解(AOP概览) 详解Spring框架的AOP机制 Bean Spring中构造器、init-method、@PostConstruct、afterPropertiesSet孰先孰后,自动注入发生时间以及单例多例的区别、SSH线程安全问题 ... 阅读详情

Spring积累

<tx:annotation-driven/>  (Spring的XML配置里两大Bean的声明之一)


 

那我们是否就可以在程序中所有被spring管理的类(@Controller、@Service、@Component等等)上都可以使用@Transactional注解了呢,在Service上可以使用@Transactional 注解这个是肯定的了,那总有些人也想弄明白能否在Controller 使用?答案显然是“不一定”的(与时间配置有关),下面做下解释:

在 spring-framework-reference.pdf 文档上有这样一段话:

<tx:annotation-driven/> only looks for @Transactional on beans in the same application context it is defined in. This means that, if you put <tx:annotation-driven/> in a WebApplicationContext for a DispatcherServlet, it only checks for @Transactional beans in your controllers, and not your services. 

意思就是:<tx:annoation-driven/>只会查找和它在相同的应用上下文件中定义的bean上面的@Transactional注解,如果你把它放在Dispatcher的应用上下文中,它只检查控制器(Controller)上的@Transactional注解,而不是你services上的@Transactional注解。

 

Bean初始化


 

总结:

1、Spring为bean提供了两种初始化bean的方式,实现InitializingBean接口,实现afterPropertiesSet方法,或者在配置文件中通过init-method指定,两种方式可以同时使用。

2、实现InitializingBean接口是直接调用afterPropertiesSet方法,比通过反射调用init-method指定的方法效率要高一点,但是init-method方式消除了对spring的依赖。

3、如果调用afterPropertiesSet方法时出错,则不调用init-method指定的方法。

 

 

后置处理器:


BeanPostProcessor API
public interface BeanPostProcessor {  
  
    /** 
     * Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean 
     * initialization callbacks (like InitializingBean's {@code afterPropertiesSet} 
     * or a custom init-method). The bean will already be populated with property values.    
     */  
    //实例化、依赖注入完毕,在调用显示的初始化之前完成一些定制的初始化任务  
    Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;  
  
      
    /** 
     * Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean 
     * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}   
     * or a custom init-method). The bean will already be populated with property values.       
     */  
    //实例化、依赖注入、初始化完毕时执行  
    Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;  
  
}

 

由API可以看出:
1:后置处理器的postProcessorBeforeInitailization方法是在bean实例化,依赖注入之后及自定义初始化方法(例如:配置文件中bean标签添加init-method属性指定Java类中初始化方法、
@PostConstruct注解指定初始化方法,Java类实现InitailztingBean接口)之前调用
2:后置处理器的postProcessorAfterInitailization方法是在bean实例化、依赖注入及自定义初始化方法之后调用

 

注意:
   1.BeanFactory和ApplicationContext两个容器对待bean的后置处理器稍微有些不同。ApplicationContext容器会自动检测Spring配置文件中那些bean所对应的Java类实现了BeanPostProcessor
接口,并自动把它们注册为后置处理器。在创建bean过程中调用它们,所以部署一个后置处理器跟普通的bean没有什么太大区别。

      2.BeanFactory容器注册bean后置处理器时必须通过代码显示的注册,在IoC容器继承体系中的ConfigurableBeanFactory接口中定义了注册方法

 

注意:
   1.BeanFactory和ApplicationContext两个容器对待bean的后置处理器稍微有些不同。ApplicationContext容器会自动检测Spring配置文件中那些bean所对应的Java类实现了BeanPostProcessor
接口,并自动把它们注册为后置处理器。在创建bean过程中调用它们,所以部署一个后置处理器跟普通的bean没有什么太大区别。

      2.BeanFactory容器注册bean后置处理器时必须通过代码显示的注册,在IoC容器继承体系中的ConfigurableBeanFactory接口中定义了注册方法

 

/**  
     * Add a new BeanPostProcessor that will get applied to beans created  
     * by this factory. To be invoked during factory configuration.  
     * <p>Note: Post-processors submitted here will be applied in the order of  
     * registration; any ordering semantics expressed through implementing the  
     * {@link org.springframework.core.Ordered} interface will be ignored. Note  
     * that autodetected post-processors (e.g. as beans in an ApplicationContext)  
     * will always be applied after programmatically registered ones.  
     * @param beanPostProcessor the post-processor to register  
     */    
    void addBeanPostProcessor(BeanPostProcessor beanPostProcessor);

 Spring如何调用多个BeanPostProcessor实现类:

    我们可以在Spring配置文件中添加多个BeanPostProcessor(后置处理器)接口实现类,在默认情况下Spring容器会根据后置处理器的定义顺序来依次调用。

Spring配置文件:

 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">
    <!-- bean定义 -->    
    <bean id="narCodeService" class="com.test.service.impl.NarCodeServiceImpl">
    </bean>
    <bean id="postProcessor" class="com.test.spring.PostProcessor"/>
    <bean id="postProcessorB" class="com.test.spring.PostProcessorB"/>
</beans>

BeanPostProcessor实现类:

package com.test.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
 * bean后置处理器
 * @author zss
 *
 */
public class PostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean,
            String beanName) throws BeansException {
        System.out.println("后置处理器处理bean=【"+beanName+"】开始");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean,
            String beanName) throws BeansException {
        System.out.println("后置处理器处理bean=【"+beanName+"】完毕!");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return bean;
    }
}
----------------------------------------------------------------------------------------------------------------------------------------
package com.test.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class PostProcessorB implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean,
            String beanName) throws BeansException {
        System.out.println("后置处理器开始调用了");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean,
            String beanName) throws BeansException {
        System.out.println("后置处理器调用结束了");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return bean;
    }
}

测试:

package com.test.spring;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class T {
    AbstractApplicationContext applicationcontext=null;
    @Before
    public void before() {
        System.out.println("》》》Spring ApplicationContext容器开始初始化了......");
        applicationcontext= new ClassPathXmlApplicationContext(new String[]{"test1-service.xml"});
        System.out.println("》》》Spring ApplicationContext容器初始化完毕了......");
    }
    @Test
    public void  test() {
        applicationcontext.registerShutdownHook();   
    }
}

  在Spring机制中可以指定后置处理器调用顺序,通过让BeanPostProcessor接口实现类实现Ordered接口getOrder方法,该方法返回一整数,默认值为 0,优先级最高,值越大优先级越低

package com.test.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
/**
 * bean后置处理器
 * @author zss
 *
 */
public class PostProcessor implements BeanPostProcessor,Ordered{

    @Override
    public Object postProcessBeforeInitialization(Object bean,
            String beanName) throws BeansException {
        System.out.println("后置处理器处理bean=【"+beanName+"】开始");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean,
            String beanName) throws BeansException {
        System.out.println("后置处理器处理bean=【"+beanName+"】完毕!");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return bean;
    }

    @Override
    public int getOrder() {
        return 1;
    }
}
----------------------------------------------------------------------------
package com.test.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;

public class PostProcessorB implements BeanPostProcessor,Ordered {

    @Override
    public Object postProcessBeforeInitialization(Object bean,
            String beanName) throws BeansException {
        System.out.println("后置处理器开始调用了");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean,
            String beanName) throws BeansException {
        System.out.println("后置处理器调用结束了");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return bean;
    }

    @Override
    public int getOrder() {
        return 0;
    }
}

测试结果:

 

PlayerCap 3.0:不截图不抠色的歌词捕捉工具详解 在数字音乐场景中,歌词的保存与整理长期依赖截图、抠色、OCR等图像处理手段,不仅效率低,而且受播放器皮肤、背景渐变和字体影响,结果难以复用。借助系统媒体接口与播放器元数据,现代工具可以直接从播放器读取歌曲名和歌词文本,将流程简化为“获取元数据→文本清洗→导出LRC”。这种基于结构化数据的歌词捕捉方式,在跨播放器兼容性、歌词准确率和自动化批量整理方面具备显著优势。无论是建立个人歌词库、制作双语字幕,还是维护本地音乐资料,都离不开稳定高效的歌词捕捉工具。PlayerCap 3.0 正是面向这一需求的设计,它不依 阅读详情

相关推荐

Codesys电子凸轮Cam表实战:从可视化配置到程序化构建

本文详细介绍了Codesys电子凸轮Cam表的实战应用,从可视化配置到程序化构建的全过程。通过包装机和印刷机案例,解析Cam表的基础设置、调试技巧及动态构建方法,帮助工程师快速掌握电子凸轮技术,提升运动控制精度和效率。

weixin_29309785的博客 298

spring问题积累(1)

占位…… 待续……

AmyChris&鱼乐的博客 237

Dell Precision 7920 Tower开机点不亮?别慌,看电源灯闪烁密码快速定位故障(附BIOS恢复指南)

本文详细解析了Dell Precision 7920 Tower工作站开机点不亮时的电源灯闪烁密码,帮助用户快速定位故障。从内存问题到BIOS恢复,再到CPU配置错误,提供了全面的诊断和修复指南,并附有实用的维护建议和工具推荐。

weixin_30892037的博客 484

Spring 积累

三种实例化bean的方式   1.使用类构造器实例化 &lt;bean id=“orderService" class="cn.itcast.OrderServiceBean"/&gt; 2.使用静态工厂方法实例化 &lt;bean id="personService" class="cn.itcast.service.OrderFactory" factory-method="crea...

不积跬步 无以至千里 不积小流 无以成江海 130

基于决策树的英雄联盟比赛胜负预测(含完整代码)

在代码中设置随机种子(例如 RANDOM_SEED = 2024)的主要目的是为了确保每次运行代码时,随机过程的结果是一致的。通过绘制不同参数组合下的准确率图,直观地展示了参数对模型性能的影响,帮助读者更好地理解和选择合适的参数。通过设置相同的随机种子(伪随机数),可以确保每次运行代码时,随机数生成器生成的随机数序列是相同的。如果在开发过程中遇到问题,设置固定的随机种子可以帮助你更容易地定位问题,因为每次运行代码时的数据和结果都是一样的。在科学研究和实验中,结果的可重复性是非常重要的。

oisflo的博客 1919

MIKE21桥墩模拟实战:抠网格与构筑物法对比与选型指南

本文详细对比了MIKE21软件中抠网格法与构筑物法在桥墩模拟中的应用,包括操作步骤、适用场景及优缺点分析。通过实际案例数据展示两种方法的精度与效率差异,并提供选型决策树和常见问题解决方案,帮助工程师根据项目需求选择最优模拟方法。

weixin_42656416的博客 322

Spring的注解积累

转载:http://yijiesuifeng.iteye.com/blog/2221858 用注解来向Spring容器注册Bean。 需要在applicationContext.xml中注册: Java代码 收藏代码 [code="java"]。 [/code] 如:在base-package指明一个包 Java代码 收藏代码 [code="java"] [/...

 対苩 175

Imatest-Star模块(西门子星图)

星图分析指南:基于ISO12233标准的MTF测试方法,通过放射状射线和正弦灰度变化评估相机SFR性能。

weixin_64706696的博客 1566

spring 知识积累

1:BeanFactory  bf = XmlBeanFactory(new ClassPathResource(new Resource(“applicationContext.xml”))); Sping 从xml到bean对象,总共经历以下阶段:定位,加载,注册,后置初始化(包含增强) Spring 1:定位,resource实现类去查找xml文件,用XmlBeanDefinitionR...

Java_Jsp_Ssh的博客 159

解决win11无法安装.net framework 3.5(包括.net2.0和3.0)错误代码:0x80070005

解决win11无法安装.net framework 3.5(包括.net2.0和3.0)错误代码:0x80070005

zeng699304的博客 2万+

【数据湖仓】:Iceberg、Hudi 和 Paimon 对比

适用于需要多计算引擎支持和复杂查询的离线批处理场景。其中立性和灵活性使其成为构建新型数据平台的理想选择。:在需要高频数据更新和高效小文件管理的场景中表现出色,适合实时数据处理和增量数据更新的业务需求。:专为实时流式数据处理设计,适用于对数据实时性要求高的业务场景,如实时数据分析和实时指标监控。通过以上分析,可以看出 Apache Iceberg、Apache Hudi 和 Apache Paimon 各有其独特的优势和适用场景。企业应根据自身业务需求和技术栈选择合适的数据湖仓方案,以充分发挥其价值。

欢迎来到我的博客,一起探索代码里的世界! 1610

【Spring123】Spring Cloud 学习积累

入门阅读:https://martinfowler.com/articles/microservices.html 单体应用与微服务架构的优缺点: 单体: 复杂性高: 技术债务: 部署频率低: 可靠性差: 扩展能力受限: 阻碍技术创新: 微服务: :) 易于开发和维护: 单个微服务启动较快: 局部修改容易部署: 技术栈不受限: 按需伸缩: :( ...

weixin_30703911的博客 73

UG\NX二次开发 使用UF_CURVE_create_ocf_feature函数创建“在面上偏置”特征

使用UF_CURVE_create_ocf_feature函数创建“在面上偏置”特征。

王牌飞行员_里海的博客 850

Spring积累总结

1.spring 的优点:   1.降低了组件之间的耦合性 ,实现了软件各层之间的解耦    2.可以使用容易提供的众多服务,如事务管理,消息服务等   3.容器提供单例模式支持   4.容器提供了AOP技术,利用它很容易实现如权限拦截,运行期监控等功能   5.spring的DI机制降低了业务对象替换的复杂性 2. 三种注入方式,构造器、接口、set注入,我们常用的是s...

weixin_30563319的博客 63

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

spring知识点积累

spring知识点

weixin_44279264的博客 176

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

关于Spring中scope的认识积累

如果一个bean是prototype的,并且这个bean要被注入到其它bean或者你通过getBean这样的方式获得这个bean的时候,spring容器会创建一个新的实例给你。 singleton模式指的是对某个对象的完全共享,包括代码空间和数据空间,也就是说,singleton会让所有线程共享他的成员变量。prototype则不会这样。 此外singleton的生命周期由容器

张波的技术积累 725

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Spring知识积累

1.Spring中的设计模式 https://mp.weixin.qq.com/s/1d0w7wffe97fiDSWyKcBrA 2.Spring boot面试题整理 https://blog.youkuaiyun.com/yuzongtao/article/details/84295732

yanghenan19870513的专栏 161

spring 注解积累

用注解来向Spring容器注册Bean。 需要在applicationContext.xml中注册: 1:@Repository 标注在持久层,即dao,用于标注数据访问。 2:@Service 标注在业务层,即service,用于标注业务逻辑。 3:@Controller 标注在控制层。 4:@Com...

b052718的专栏 198

【积累】SPRING TOOL SUITE规范

STS常用方法

SomeWay 182
上一篇: ForkJoinPool
下一篇: Servlet容器初始化IOC容器
Art&Code
博客等级 码龄7年 77粉丝 554原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值