使用注解实现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. asp.net core 中优雅的进行响应包装

    目录 摘要 正常响应/模型验证错误包装 实现按需禁用包装 如何让 Swagger 识别正确的响应包装 禁用默认的模型验证错误包装 使用方法以及自定义返回结构体 SourceCode && ...

  2. JAVA实现对阿里云DNS的解析管理

    1.阿里云DNS的SDK依赖 <dependency> <groupId>com.aliyun</groupId> <artifactId>tea-op ...

  3. 微信小程序云开发指南

    一.初识云开发 官方文档 小程序·云开发是微信团队联合腾讯云推出的专业的小程序开发服务. 开发者可以使用云开发快速开发小程序.小游戏.公众号网页等,并且原生打通微信开放能力. 开发者无需搭建服务器,可 ...

  4. Solon Web 开发

    Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...

  5. manjaro20初始配置

    输入法配置 安装完以后需要注销或重启,然后配置fctx 注意如果刚开始 export GTK_IM_MODULE=fcitx export QT_IM_MODULE=fcitx export XMOD ...

  6. vector自实现(一)

    vector.h: #ifndef __Vector__H__ #define __Vector__H__ typedef int Rank; #define DEFAULT_CAPACITY 3 t ...

  7. gorm概述与快速入门

    特性 全功能 ORM 关联 (Has One,Has Many,Belongs To,Many To Many,多态,单表继承) Create,Save,Update,Delete,Find 中钩子方 ...

  8. 有向图子图 DAG 数量

    考虑 \(\tt DP\),朴素的想法是令 \(f_S\) 表示 \(S\) 这个导出子图将边定向集合构成 \(\tt DAG\) 的方案数. 转移可以考虑剥去所有入度为 \(0\) 的点,那么我们就 ...

  9. git命令log与reflog的比较

    感谢原文作者:杨鲜生 原文链接:https://blog.csdn.net/u013252047/article/details/80230781 用git命令,想看到自己的操作记录,则可以使用log ...

  10. Redis源码简要分析

    转载请注明来源:https://www.cnblogs.com/hookjc/ 把所有服务端文件列出来,并且标示出其作用:adlist.c //双向链表ae.c //事件驱动ae_epoll.c // ...