zoukankan      html  css  js  c++  java
  • Spring框架的IOC/AOP注解

    Spring_IOC/AOP注解

    一、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("applicaton.xml");
            AopServices aopServices = (AopServices)context.getBean("aopServices");
            try {
                aopServices.doSome();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    HDU 1985 Conversions
    HDU 1708 Fibonacci String
    HDU 3501 Calculation 2
    HDU 4163 Stock Prices
    HDU 2391 Filthy Rich
    HDU 1996 汉诺塔VI
    HDU 2824 The Euler function
    HDU 1787 GCD Again
    HDU 2393 Higher Math
    HDU 1286 找新朋友
  • 原文地址:https://www.cnblogs.com/rzbwyj/p/11778117.html
Copyright © 2011-2022 走看看