HCspring笔记

Spring入门

SpringFramework 核心 底层
SpringBoot 必须掌握
SpringDate 和数据有关
SpringSecurity 安全

1.SpringFramework

1 loc

1.1ioc

ioC:inversion of control 控制反转

​ 控制反转:在服务器启动阶段时把所有的对象实例化,当程序中调用时直接返回已实例化过的对象

作用:解决对象生命周期管理,减少应用跟jvm(虚拟机)的频繁交互

ioc用了设计模式中的工厂模式和单例模式

//BeanFactory[Bean工厂]   管理实现类的工厂(懒汉)
//ApplicationContext 管理实现类的工厂(饿汉:在服务器启动时把所有对象实例化)   extends BeanFactory

使用方法:

  1. 将实现类写入到ioc容器中

    <bean id="iocService" class="com.javakc.spring.ioc.service.impl.IocServiceImpl"/>
    
  2. 实例化ioc容器,读取ioc容器

    ApplicationContext context = new ClassPathXmlApplicationContext("spring-ioc.xml");
    
  3. 获取ioc容器里的bean对象

    context.getBean("iocService");
    

设置懒汉饿汉:

lazy-init="true"设置成懒汉模式,默认是饿汉,添加到bean标签里
1.2 DI

DI:Dependency Injection 依赖注入

​ 依赖注入:基于控制反转实现类的实例化,把对象之间的关系建立映射,当程序调用时无需获取对象!

依赖注入的事项方式?

  1. setter注入
  2. 构造器注入 (官方推荐)
  3. lookup接口注入 (不用) 侵入式

使用方法:

1.构造方法注入

  1. 方法注入

    <bean id="iocService" class="com.javakc.spring.ioc.service.impl.IocServiceImpl">
        <!--        构造方法注入-->
        <constructor-arg ref="iocDao"/>
    </bean>2
    
  2. 注入

    IocDao iocDao;
    //构造方法注入
    public IocServiceImpl(IocDao iocDao) {
          
        this.iocDao=iocDao;
    }
    

2.set方法注入

  1. 方法注入

    <property name="iocDao123" ref="iocDao"/>
    
  2. 注入

    public void setIocDao(IocDao iocDao) {
          
        this.iocDao = iocDao;
    }
    
1.3 基于注解实现方式
  1. @RunWith(SpringJUnit4ClassRunner.class)    //运行,让spring去读取ioc
    @ContextConfiguration("classpath:spring-ioc-annotation.xml")    //读取配置文件,ioc容器的配置文件在哪路径,需要和@RunWith注解配合使用
    
  2. //扫描包
    <context:component-scan base-package="com.javakc.spring"/>
    

    use-default-filters="false"设成false不扫描那4个注解,默认值为ture扫描

  3. //表现层
       @Controller
       //逻辑层
       @Service
       //数据层
       @Repository
       //通用组件
       @Component//都继承这个
    

    不写id时,默认类的名字就是id名(首字母小写) :@Controller(“user”)

  4. 注入注解

    //依赖注入
    @Resource		//jkd提供的;需要引入依赖
    //默认根据名称装配(默认名称为类的名字首字母小写)
    
    //根据名称装配
    @Resource(name="iocDao")
    //1.只根据名称匹配ioc容器中bean的id(找不到报错)
    
    //根据类型装配
    @Resource(type=iocDao.class)
    //哪个类实现了iocDao接口 (可以是 接口.class,实现类.class,)接口都多个实现类时会报错
    
    //优先装配
    @Primary
    
    //自动装配
    @Autowired		//spring框架提供的
    //1.首先按照@Primary优先装配
    //2.其次按照名称匹配
    //3.最终按照类型匹配
    
    //只按名称装配
    @Autowired
    @Qualifier		//让autowired只按名称装配
    
properites配置文件

读取配置文件

<!--读取properties配置文件-->
<util:properties id="properties" location="classpath:spring-ioc.properties"/>

@Value(“#{id.key}”)
获取properties文件配置信息

常用注解

@Lazy //延迟实例化

@Value //基本类型

@Order //装配顺序,数字越小优先级越高

@Bean //交由ioc容器管理实例

@PostConstrust //初始化方法@PreDestory //销毁方法

@DependsOn //指定优先实例化

@Scheduled //定时器
//fixedDelay:固定间隔
//fixedRate:固定频率
//cron:秒 分 时 日 月 周 年

@EnableAsync //修饰类(当前类包含异步方法)

@Async //修饰方法(当前方法为异步方法)

配置属性
属性名称 默认值 属性作用
id null 唯一标识符用于获取示例
name null 别名 可以定义多个(别名)name=“a,b”
class null 有ioc容器负责实例化的对象
lazy-init false false:单例 饿汉
true:单例 懒汉
init-method null ioc容器在对象全部实例化完成后调用
destroy-method null ioc容器在对象销毁前调用的方法
primary false 优先装配
depends-on null 优先实例化
autowire null byName:根据属性名称自动装配
byType:根据属性类型自动装配
constructor:根据构造方法primary name type
scope singleton 1.singleton:单例模式
2.prototype:原型模式
3.request:请求模式
4.session:会话模式
5.application:应用模式
6.websocket:web通信模式
常用注解
控制反转
@Controller 声明表现层实现类
@Service 声明逻辑层实现类
@Repository 声明数据层实现类
@Component 声明通用组件
依赖注入
@Resourc java提供的依赖注入
@Autowired spring提供的依赖注入
@Qualifier
@Value
属性注解
@Primary 优先装配
@Lazy 改变单例模式为懒汉模式
@Scope 改变类的实现化方式原型
测试注解
@Test 修饰测试方法
@Before 测试方法前调用
@RunWith 运行spring-test方法
@ContextConfiguration 读取配置文件

jdk9以后引入模块


2.AOP面向切面编程

2.1静态代理

一般不用


2.2动态代理
2.2.1配置形式

1.以注解为切入点 和 以方法作为切入点

<!--扫描并实例化-->
<context:component-scan base-package="com.javakc.spring"/>

<!--aop动态代理配置-->
<aop:config>
    <!--aop切面-->
    <!--ref注入代理类-->
    <aop:aspect ref="loggerAdvice">
        <!--aop切入点-->

        <!--1.以注解为切入点-->
        <!--<aop:pointcut id="" expression="@annotation(org.springframework.stereotype.Controller)"/>-->

        <!--2.以方法作为切入点-->
        <!--execution(方法的路径),支持正则表达式,id唯一标识符 -->
        <!--<aop:pointcut id="methodPointcut" expression="execution(* com.javakc.spring.aop.service.impl.AopServiceImpl.insert(String, int))"/>
            <aop:pointcut id="methodPointcut" expression="execution(* IocServiceImpl.insert(String, int))"/>-->
        <aop:pointcut id="methodPointcut"
                      expression="execution(* com.javakc.spring.*.service.impl.*ServiceImpl.insert(..))"/>
        <!-- * 表示所有     第一个 * 叫方法返回类型(表示任意的返回类型)-->
        <!-- .. 两个点表示任意的参数个数(包括无参)和任意的参数类型-->
        <!--切入点只负责拦截-->

     <!--建立 连接点JointPoint 与 通知实现类方法 映射-->
        <!--after 事后消息处理 , before 预处理-->
        <aop:after method="logger" pointcut-ref
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值