标签:实现 配置 tor 包名 rgs round classpath execution style
一、IOC注解
1.用于向Spring容器中注入bean:
2.用于得到数据,实现Bean组件的装配
案例实现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注解
注解实现各种增强
(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(); } } }
标签:实现 配置 tor 包名 rgs round classpath execution style
原文地址:https://www.cnblogs.com/xiao-ran/p/11776149.html