zoukankan      html  css  js  c++  java
  • 使用注解实现SpringIOC和SpringAOP

    使用注解实现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");
    }

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

  • 相关阅读:
    为什么要用全文搜索引擎:全文搜索引擎 VS 数据库管理系统
    大数据学习路线之hive存储格式
    web测试教程之JavaScript中的变量
    Java学习中面向过程与面向对象的优缺点
    Java教程之Java反射
    Python技术基础知识点:OS模块的应用
    软件测试教程——概念解析及常用方法概说
    UI设计师必备技能 网页中的色彩搭配(色彩篇)
    UI技术分享 如何提高自己的设计视野
    JavaScript学习指南分享
  • 原文地址:https://www.cnblogs.com/love-menglong/p/11295268.html
Copyright © 2011-2022 走看看