使用注解实现ioc
@Component:实现Bean组件的定义
@Repository:标注dao类
@Service:标注业务类
@Controller:标注控制类

Bean的自动装配:
@Autowired 默认按类型匹配
@Qualifier 指定Bean的名称

使用注解实现AOP
AspectJ :面向切面的框架,扩展了java语言,定义了AOP语法,能够在编译器提供代码的织入
@AspectJ
AspectJ 5新增的功能,使用jdk5.0注解技术和正规的AspectJ切点表达式语言描述切面
Spring通过集成AspectJ实现了以注解的方式定义增强类,大大减少了配置文件中的工作量
利用轻量级的字节码处理框架asm处理@AspectJ中所描述的方法参数名
****使用@AspectJ,首先保证所用的jdk版本5.0或以上版本

下面用一个案例为大家解释

 1 package cn.kgc.springtest2.demo1.dao;
2
3 /**
4 * @author
5 * @create 2019-07-30 15:35
6 * dao层设计模拟数据访问层
7 **/
8 public interface UserDao {
9
10 void save(String date);
11 }
 1 package cn.kgc.springtest2.demo1.dao.impl;
2
3
4 import cn.kgc.springtest2.demo1.dao.UserDao;
5 import org.springframework.stereotype.Repository;
6
7 /**
8 * @author
9 * @create 2019-07-30 15:35
10 **/
11 @Repository
12 public class UserDaoImpl implements UserDao {
13 public void save(String date) {
14 System.out.println("数据存入中.....");
15 System.out.println("数据存储完成....内容为"+date);
16 }
17 }
1 package cn.kgc.springtest2.demo1.service;
2
3 public interface UserService {
4 /**
5 *
6 * @param date
7 */
8 void saveUser(String date);
9 }
 1 package cn.kgc.springtest2.demo1.service.impl;
2
3
4 import cn.kgc.springtest2.demo1.dao.UserDao;
5 import cn.kgc.springtest2.demo1.service.UserService;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.stereotype.Service;
8
9 import javax.annotation.Resource;
10 import javax.annotation.Resources;
11
12 /**
13 * @author
14 * @create 2019-07-30 15:38
15 **/
16
17 @Service("userService")
18 public class UserServiceImpl implements UserService {
19
20 @Resource
21 private UserDao userDao;
22
23 /**
24 * spring 注入方式中的设值注入
25 * @param userDao
26 */
27 /*public void setUserDao(UserDao userDao){
28 this.userDao = userDao;
29 }*/
30
31 /* public UserServiceImpl(UserDao userDao){
32 this.userDao = userDao;
33 }*/
34
35 /**
36 * @param date
37 */
38 public void saveUser(String date) {
39
40 //模拟业务逻辑层做一些判断
41
42 date = date.hashCode()+date;
43 System.out.println("业务逻辑层执行。。。。。。。。。。。。。");
44 userDao.save(date);
45 //模拟
46 throw new RuntimeException("你在这里少打了一个分号");
47 }
48 }
 1 package cn.kgc.springtest2.demo1.logger;
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.aspectj.lang.ProceedingJoinPoint;
5 import org.aspectj.lang.annotation.*;
6 import org.springframework.stereotype.Component;
7
8 /**
9 * @author
10 * @create 2019-07-31 17:31
11 **/
12 @Slf4j
13 @Aspect
14 @Component
15 public class ServiceLogger {
16
17 @Pointcut("execution(* cn.kgc.springtest2.demo1.service..*(..))")
18 public void pointcut(){
19
20 }
21 @AfterThrowing(value = "pointcut()",throwing = "c")
22 public void afterThrowing(RuntimeException c){
23 log.error("我们检测到报错情况,信息如下"+c.getMessage());
24 }
25
26 @After("pointcut()")
27 public void after(){
28 log.info("无论上面有没有报错我都执行");
29 }
30
31 /**
32 * 环绕增强
33 * @param point
34 */
35 @Around("pointcut()")
36 public void around(ProceedingJoinPoint point){
37 try {
38 log.info(point.getSignature().getName()+"目标方法执行前 我是前置增强");
39 Object result = point.proceed();
40 log.info("我是后置增强方法执行完成,返回值是"+result);
41 } catch (Throwable throwable) {
42
43 log.error("傻逼报错了 我是异常增强。。。。。信息是"+throwable.getMessage());
44 throwable.printStackTrace();
45 }finally {
46 log.info("傻逼要看见我了,我是最终增强");
47 }
48 }
49
50
51 }
 1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
6
7 <!--&lt;!&ndash;开启注解扫描&ndash;&gt;-->
8 <context:component-scan base-package="cn.kgc"/>
9 <!--开启aop注解扫描-->
10 <aop:aspectj-autoproxy/>
11
12 </beans>
@Test
public void saveUser() {
String resource = "springioc.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(resource);
UserService userService = context.getBean("userService", UserService.class);
userService.saveUser("testData");
}

打完上面的示例就可以看懂了

使用注解实现SpringIOC和SpringAOP的更多相关文章

  1. 【SpringAop】【统一日志处理】注解方式理解以及使用

    [注意:本次代码的demo会存在百度网盘,由于公司的保密,禁止上传,所以仅本人可见] 目前公司在做数据资产项目,数据质量部分使用到了springaop做统一日志处理,以前对这块有了解,有点模糊不清,今 ...

  2. SpringIOC框架简单实现(注解实现)

    SpringIOC框架简单实现(注解实现) 前情回顾 SpringIOE简单介绍 运用注解的方式来实现IOC 首先,让我们来创建一个Dog类 @Component("dog")// ...

  3. Spring:Spring注解大全

    @Controller 标识一个该类是Spring MVC controller处理器,用来创建处理http请求的对象. @Controller public class TestController ...

  4. Java注解的基本概念和原理及其简单实用

      一.注解的基本概念和原理及其简单实用 注解(Annotation)提供了一种安全的类似注释的机制,为我们在代码中添加信息提供了一种形式化得方法,使我们可以在稍后某个时刻方便的使用这些数据(通过解析 ...

  5. Spring-【高阶】注解-转载

    从Spring2.0以后的版本中,Spring也引入了基于注解(Annotation)方式的配置,注解(Annotation)是JDK1.5中引入的一个新特性,用于简化Bean的配置,某些场合可以取代 ...

  6. Spring注解实现原理

    ​[Spring如何使用注解机制完成自动装配] Java实例构造时会调用默认父类无参构造方法,Spring正是利用了这一点,让"操作元素的代码"得以执行.   [两种处理策略] ( ...

  7. Spring - SpringIOC容器详解

    一.什么是Spring IOC: Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想. 在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是 ...

  8. Spring对注解(Annotation)处理【转】

    1.从Spring2.0以后的版本中,spring也引入了基于注解(Annotation)方式的配置,注解(Annotation)是JDK1.5中引入的一个新特性,用于简化Bean的配置,某些场合可以 ...

  9. spring中注解的实现原理

    @Autowired和@Resource的区别: 在Java中使用@Autowired和@Resource注解进行装配,这两个注解分别是:1.@Autowired按照默认类型(类名称)装配依赖对象,默 ...

随机推荐

  1. 品味Spring Cache设计之美

    最近负责教育类产品的架构工作,两位研发同学建议:"团队封装的Redis客户端可否适配Spring Cache,这样加缓存就会方便多了" . 于是边查阅文档边实战,收获颇丰,写这篇文 ...

  2. 解决excel两表之间数据关联关系,知道这几招就够了

    用过SAP的凭证批量录入模板(Excel文件)的都知道,一个凭证由[抬头]和多个[行项目]组成,这是一个关于excel两表信息关联的典型场景. 这里头蕴藏着一个麻烦:当我们需要一次性录入多个凭证时,如 ...

  3. Android一句话 | ViewGroup事件分发

    ViewGroup中可重写的关于事件分发的事件有dispatchTouchEvent,onTouchEvent,onInterceptTouchEvent和requestDisallowInterce ...

  4. tmux安装配置与使用

    tmux安装 sudo apt-get install tmux tmux配置 在家目录下操作 cd git clone https://github.com/gpakosz/.tmux.git ln ...

  5. 云计算实验二 Docker实验-docker安装

    一.实验目的  1.了解Docker服务安装: 2.掌握Docker镜像操作 二.实验内容 1.Docker服务安装 查看内核版本 uname -r 安装依赖环境: yum install -y yu ...

  6. 【刷题-LeetCode】222. Count Complete Tree Nodes

    Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. Note: Definition ...

  7. 搭建服务器之www-向外提供视频服务by html5 video标签

    搭建好www服务器,主要目的有两个一个是试验下,另一个是想给女朋友个惊喜,给她个带视频的网页,嘿嘿当前测试下相应功能. 1,采用html5的视频功能:bideo标签. 源码如下: <!docty ...

  8. Mybatis插件,能做的事情真的很多

    大家好,我是架构摆渡人.这是实践经验系列的第九篇文章,这个系列会给大家分享很多在实际工作中有用的经验,如果有收获,还请分享给更多的朋友. Mybatis是我们经常用的一款操作数据库的框架,它的插件机制 ...

  9. Apple历代Mac系统汇总

    编号 系统名 版本号 名称 发布时间 01 macOS 11.0 Big Sur(大惊喜) 2020年6月 02 macOS 10.15 Catalina(卡特琳娜) 2019年9月 03 macOS ...

  10. 通过location.search来获取页面传来的参数

    获取页面传来的参数 <div> <script> function GetQueryString(name) { var reg = new RegExp("(^|& ...