美文网首页
spring笔记

spring笔记

作者: 我是电饭煲 | 来源:发表于2020-03-30 19:43 被阅读0次

AOP实例讲解

https://www.jianshu.com/p/5b9a0d77f95f

AOP事务实例

image.png

AOP基本概念

image.png

AOP通知类型

image.png

AOP原理

image.png

gclib步骤

image.png

IOC(本质)

image.png

被IOC类的必要条件

  • 无参构造(有参构造需要特殊处理)
  • 属性set方法

依赖注入四种方式

接口注入
注解注入
set注入
构造函数注入

https://zhuanlan.zhihu.com/p/34405799

spring IOC容器主要方法(使用到了反射)

image.png

依赖注入:BeanFactory的生命周期

image.png

依赖注入:ApplicationContext的生命周期

  • 引入的第三方包@Autowired 不能自动加载解决办法

在主类加上注解:
@ComponentScan("包名")
@ComponentScan

  • spring文档

http://spring.io/
http://spring.io/projects/spring-framework

  • spring使用ApplicationListener初始化程序demo

@Component
public class InitApp implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

        // 防止重复执行
        if (event.getApplicationContext().getParent() != null) {
              return;
        }
      // 业务逻辑
    }
}

  • 加载resources资源文件

public class testAdapterApp {
    static final ApplicationContext context
            = new FileSystemXmlApplicationContext(testAdapterApp.class.getResource("applicationContext.xml").getFile());

    public static void main(String[] args) {
    }
}
  • spring applicationContext.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" xmlns:context="http://www.springframework.org/schema/context"
       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.xsd">
    <description> esper demo cfg </description>

    <context:component-scan base-package="github.nashse" />
    
</beans>
  • spring:同一个类中,三个属性变量指向同一个地址

@Component
public class Init {
    @Autowired
    NettyClient mdClient;
    @Autowired
    NettyClient emsClient;
    @Autowired
    NettyClient omsClient;
}
  • 解决办法:在xml中实例化,id为对应变量名字

<bean>
    <bean id="mdClient" class="NettyClient" >
    </bean>

    <bean id="emsClient" class="NettyClient">
    </bean>

    <bean id="omsClient" class="NettyClient">
    </bean>

</beans>
image.png

相关文章

网友评论

      本文标题:spring笔记

      本文链接:https://www.haomeiwen.com/subject/qcbgkqtx.html