Spring——注解

时间:2019-11-01
本文章向大家介绍Spring——注解,主要包括Spring——注解使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、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();
        }

    }
}

原文地址:https://www.cnblogs.com/xiao-ran/p/11776149.html