史上最全Spring教程,从零开始带你深入♂学习,三面拼多多_spring 教程




> ### []( )**衍生注解**



[领取资料]( )  

**@Component三个衍生注解  

为了更好的进行分层,Spring可以使用其它三个注解,功能一样,目前使用哪一个功能都一样。  

@Controller:web层(控制层)  

@Service:service层(服务层)  

@Repository:dao层(数据访问层)**




@Component //将这个类标注为Spring的一个组件,放到容器中!

public class Dog {

public String name = "dog";

}//加群1025684353一起吹水聊天




> ### []( )**作用域**



**@scope**



*   **singleton:默认的,Spring会采用单例模式创建这个对象。关闭工厂 ,所有的对象都会销毁。**

*   **prototype:多例模式。关闭工厂 ,所有的对象不会销毁。内部的垃圾回收机制会回收**




@Controller(“user”)

@Scope(“prototype”)

public class User {

@Value("张三")

public String name;//加群1025684353一起吹水聊天

}




> ### []( )**基于Java类进行配置**



[领取资料]( )




@Configuration //代表这是一个配置类

@Import(MyConfig2.class) //导入合并其他配置类,类似于配置文件中的inculde标签

public class MyConfig {

@Bean //通过方法注册一个bean,这里的返回值就Bean的类型,方法名就是bean的id!

public Dog dog(){

    return new Dog();//加群1025684353一起吹水聊天

}

}




[]( )AOP

==================================================================



**AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。**  

[领取资料]( )



| 通知类型 | 连接点 | 实现接口 |

| --- | --- | --- |

| 前置通知 | 在方法执行前执行 | org.spirngframework.aop.MethodBeforeAdvice |

| 后置通知 | 在方法执行后执行 | org.springframework.aop.AfterReturningAdvice |

| 环绕通知 | 在方法执行前后都执行 | org.aopalliance.intercept.MethodInterceptor |

| 异常抛出通知 | 在方法抛出异常后执行 | org.springframework.aop.ThrowsAdvice |

| 引介通知 | 在目标类中添加一些新的方法和属性 | org.springframework.aop.IntroductionInterceptor |



> ### []( )**使用Spring实现Aop**



**导入依赖**




org.aspectj

aspectjweaver

1.9.6




> ### []( )**第一种实现方式:**



**1、编写接口**  

[领取资料]( )




package com.sutdy.service;

public interface UserService {

public void add();

public void delete();

public void update();//加群1025684353一起吹水聊天

public void select();

}




**2、编写接口实现类**




package com.study.service;

public class UserServiceImpl implements UserService{

@Override

public void add() {

    System.out.println("增加了一个用户");

}



@Override

public void delete() {

    System.out.println("删除了一个用户");

}



@Override

public void update() {

    System.out.println("更新了一个用户");

}//加群1025684353一起吹水聊天



@Override

public void select() {

    System.out.println("查询了一个用户");

}

}




**3、编写增强类**



**前置增强:**




package com.study.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

//方法执行前执行该类

public class BeforeLog implements MethodBeforeAdvice {

//method:要执行的目标方法

//objects:参数

//o:目标对象

@Override

public void before(Method method, Object[] objects, Object o) throws Throwable {

    System.out.println(o.getClass().getName()+"的"+method.getName()+"被执行了");

}//加群1025684353一起吹水聊天

}

领取资料




**后置增强:**




package com.study.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

//方法执行后执行该类

public class AfterLog implements AfterReturningAdvice {

//returnValue 返回值

//method被调用的方法

//args 被调用的方法的对象的参数

//target 被调用的目标对象

@Override

public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {

    System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);//加群1025684353一起吹水聊天

}

}




**4、编写配置文件:**




<?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:aop="http://www.springframework.org/schema/aop"

   xsi:schemaLocation="http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans.xsd

   http://www.springframework.org/schema/aop

最后我们该如何学习?

1、看视频进行系统学习

这几年的Crud经历,让我明白自己真的算是菜鸡中的战斗机,也正因为Crud,导致自己技术比较零散,也不够深入不够系统,所以重新进行学习是很有必要的。我差的是系统知识,差的结构框架和思路,所以通过视频来学习,效果更好,也更全面。关于视频学习,个人可以推荐去B站进行学习,B站上有很多学习视频,唯一的缺点就是免费的容易过时。

另外,我自己也珍藏了好几套视频资料躺在网盘里,有需要的我也可以分享给你:

1年半经验,2本学历,Curd背景,竟给30K,我的美团Offer终于来了

2、读源码,看实战笔记,学习大神思路

“编程语言是程序员的表达的方式,而架构是程序员对世界的认知”。所以,程序员要想快速认知并学习架构,读源码是必不可少的。阅读源码,是解决问题 + 理解事物,更重要的:看到源码背后的想法;程序员说:读万行源码,行万种实践。

Spring源码深度解析:

1年半经验,2本学历,Curd背景,竟给30K,我的美团Offer终于来了

Mybatis 3源码深度解析:

  • 24
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值