一、IOC注解

  1.用于向Spring容器中注入bean:

  • @Component:向Spring容器中注入bean
  • @Repository:用于标注Dao层
  • @Service:用于标注Service业务层
  • @Controller:用于标注控制器类

  2.用于得到数据,实现Bean组件的装配

  • @Autowired:默认ByType方式,如果出现同名类,则不能按照Type进行注入 需要使用@Qualifier 指明ID
  • .@Resource: 默认ByName方式,如果name确实默认按照ByType方式注入

  案例实现IOC注解

    (1).大配置文件中加入包扫描仪,扫描包中的注解

 <context:component-scan base-package="cn.spring.aop"/>

         base-package中填写的是包名,多个包名用‘,’区分

    (2).定义相应的接口及实现类

@Repository("mapperImpl")
public class MapperImpl implements Mapper {
    @Override
    public int add(StudentBean stu) {
        System.out.println("123");
        return 0;
    }
}
@Service("StuServiceImpl")
public class StuServiceImpl implements StuService {
    @Resource
    private Mapper mapper;
    @Override
    public int add(StudentBean stu) {
        return mapper.add(stu);
    }
}

    (3).测试类

@Test
    public  void IocTestBy(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        StuService stuServiceImpl = (StuService)context.getBean("StuServiceImpl");
        stuServiceImpl.add(new StudentBean());
    }

二、AOP注解

  • @Aspect :声明切面
  • @Ponitcut :声明公共的切点表达式
  • @Before :前置增强
  • @AfterReturning: 后置增强
  • @Around :环绕增强
  • @AfterThrowing :异常抛出增强
  • @After: 最终增强

  注解实现各种增强

    (1).大配置文件中开启aop注解

 <aop:aspectj-autoproxy/>

    (2).接口实现

@Service("aopServices")
public class AopServices{
    public void doSome() throws Exception{
      /* int i=1/0;*/
        System.out.println("Service层业务");
    }
}

    (3).增强类

@Aspect
@Component
public class Advices {
    //设定切入点表达式
    @Pointcut("execution(* *..aop.*.*(..))")
    public  void  Pointcut(){

    }

    //前置增强
    @Before("Pointcut()")
    public void before(){
        System.out.println("=======前置=========");
    }

    //后置增强
    @AfterReturning("Pointcut()")
    public  void afterReturning(){
        System.out.println("=======后置==========");
    }

    //环绕增强
    @Around("Pointcut()")
    public Object Around(ProceedingJoinPoint jp) throws Throwable {

        System.out.println("=======环绕前==========");
        Object proceed = jp.proceed();
        System.out.println("=======环绕后==========");
        return proceed;
    }

    //异常增强
    @AfterThrowing(pointcut = "execution(* *..aop.*.*(..))",throwing = "ex")
    public void AfterThrowing(Exception ex){
        System.out.println("发生异常");
    }

    //最终增强
    @After("Pointcut()")
    public void After(){
        System.out.println("=======最终增=========");
    }
}

    (4).测试类

public class AopTest {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        AopServices aopServices = (AopServices)context.getBean("aopServices");
        try {
            aopServices.doSome();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Spring——注解的更多相关文章

  1. spring注解源码分析--how does autowired works?

    1. 背景 注解可以减少代码的开发量,spring提供了丰富的注解功能.我们可能会被问到,spring的注解到底是什么触发的呢?今天以spring最常使用的一个注解autowired来跟踪代码,进行d ...

  2. Spring注解

    AccountController .java Java代码   1.        /** 2.         * 2010-1-23 3.         */ 4.        packag ...

  3. spring 注解的优点缺点

    注解与XML配置的区别 注解:是一种分散式的元数据,与源代码耦合. xml :是一种集中式的元数据,与源代码解耦. 因此注解和XML的选择上可以从两个角度来看:分散还是集中,源代码耦合/解耦. 注解的 ...

  4. spring注解说明之Spring2.5 注解介绍(3.0通用)

    spring注解说明之Spring2.5 注解介绍(3.0通用) 注册注解处理器 方式一:bean <bean class="org.springframework.beans.fac ...

  5. 使用Spring注解来简化ssh框架的代码编写

     目的:主要是通过使用Spring注解的方式来简化ssh框架的代码编写. 首先:我们浏览一下原始的applicationContext.xml文件中的部分配置. <bean id="m ...

  6. spring注解scheduled实现定时任务

    只想说,spring注解scheduled实现定时任务使用真的非常简单. 一.配置spring.xml文件 1.在beans加入xmlns:task="http://www.springfr ...

  7. [转]Spring 注解总结

    原文地址:http://blog.csdn.net/wangshfa/article/details/9712379 一 注解优点?注解解决了什么问题,为什么要使用注解? 二 注解的来龙去脉(历史) ...

  8. eclipes的Spring注解SequenceGenerator(name="sequenceGenerator")报错的解决方式

    eclipes的Spring注解SequenceGenerator(name="sequenceGenerator")报错的解决方式 右键项目打开Properties—>JA ...

  9. Spring注解【非单例】

    花了至少一整天的时间解决了这个问题,必须记录这个纠结的过程,问题不可怕,思路很绕弯. 为了能说清楚自己的问题,我都用例子来模拟. 我有一个类MyThread是这样的: @Service public ...

  10. spring注解方式在一个普通的java类里面注入dao

    spring注解方式在一个普通的java类里面注入dao @Repositorypublic class BaseDaoImpl implements BaseDao {这是我的dao如果在servi ...

随机推荐

  1. Codeforces 1236A. Stones

    传送门 注意到两种操作都要消耗中间的石头,并且两种操作每次都会得到 $3$ 个石头 那么显然优先做操作二是最优的,因为操作二只会消耗中间堆的一个石头 如果有剩下再进行操作 $1$ ,那么可以保证总操作 ...

  2. Nginx、OpenResty和Kong的基本概念与使用方法

    Nginx.OpenResty和Kong的基本概念与使用方法 2018年10月10日 22:46:08 李佶澳 阅读数 322更多 分类专栏: kubernetes   版权声明:本文为博主原创文章, ...

  3. exclipe怎么设置编码为UTF-8

    如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文输出,则最好使 Java文件使用UTF-8编码.然而,Eclipse工作空间(workspace)的缺省字符编码是操作系统缺省的编码,简 ...

  4. Dreamweaver CS6 破解安装

    安装 双击Dreamweaver.dmg文件,然后Command+N,新建一个Finder,接着将Adobe Dreamweaver CS6拖到新建Finder的应用程序中.   在Finder中应用 ...

  5. 关于同一台服务器上两个PHP项目相互访问超时的问题

    当一台服务器部署多个PHP项目,各自运行时并无干扰, 即使都使用 9000端口来跑php 但是有一种情况,当其中一个项目需要调用另一个php项目的接口时,便会超时,这是因为php是单线程的同步的 也许 ...

  6. deep_learning_Function_np.newaxis参数理解

    功能:np.newaxis是用来给数组a增加维度的格式:a[np.newaxis和:的组合],如a[:,np.newaxis],a[np.newaxis, np.newaxis, :]详解:np.ne ...

  7. ProgressDialog 进度条的初步认识

    public class MainActivity extends Activity implements View.OnClickListener{ private ProgressBar prog ...

  8. Linux系统组成和获取命令帮助4

    Linux文件系统:        1.文件名名称严格区分字符大小写        2.文件可以使用除/以外任意字符        3.文件名长度不能超过255字符        4.以.开头的文件为 ...

  9. Python3.8新特性-- 海象操作符

    “理论联系实惠,密切联系领导,表扬和自我表扬”——我就是老司机,曾经写文章教各位怎么打拼职场的老司机. 不记得没关系,只需要知道:有这么一位老司机, 穿上西装带大家打拼职场! 操起键盘带大家打磨技术! ...

  10. debian docker环境搭建

    环境(阿里): 登陆到系统: 我们主要看执行结果截图(所有命令都进行复制) 卸载旧版本: 使用 APT 安装: 这里 输入 y  然后等待执行结束 添加软件源的 GPG 密钥. 一开始我是手打的命令, ...