spring中的各种注解

1.spring中bean的注解

<!--启用classpath路径扫描-->
<context:component-scan base-package="com.offcn.user"></context:component-scan>

注解分层:@Component @Controller @Service @Repository
引用对象:@Autowired

2.事务注解

<!--配置事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>
<!--启用事务注解-->
<tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>

引用事务注解:

@Transactional(propagation= Propagation.REQUIRED,isolation= Isolation.READ_COMMITTED,
timeout = -1,readOnly = false,rollbackFor = Exception.class)

3.SpringMVC中mvc注解

<!--映射处理器启用注解 http://localhost:8080/user/login-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!--处理器适配器启用注解-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!--当使用下面代码时可以不写映射处理器和处理器适配器启用注解-->
<mvc:annotation-driven></mvc:annotation-driven>

@RequestMapping(value,method,params) 设置请求路径,请求方法,请求参数要求
@RequestParam(value,required,defaultValue) 将请求参数区的数据映射到功能处理方法的参数上
@RequestVariable(value) 将请求中模板变量映射到功能处理方法的参数上
@CookieValue(value)将请求的Cookie数据映射到功能处理方法的参数上
@RequestHeader(value)将请求头信息区数据映射到功能处理方法的参数上
@ModelAttribute("") 绑定请求参数到命令对象、暴露@RequestMapping方法返回值为模型数据
@ResponseBody和@RequestBody 需要导入依赖,将请求或响应的数据以JSON格式传递到服务端或客户端

4.Aop注解

<!--classpath扫描-->
<context:component-scan base-package="com.alibaba.spring"></context:component-scan>
<!--AspectJ-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

注解切面类:@Aspect
全局切入点:@Pointcut("")public void pointcut(){} 方法名就是全局切入点的id
局部切入点:在通知中定义切入点

前置通知:

局部无参:@Before("execution(* com.alibaba.spring.service.DeptServiceImpl.insertNoParam(..))")
局部有参:@Before("execution(* com.alibaba.spring.service.DeptServiceImpl.insert(..)) and args(userName,userAge,birth)")
全局无参:@Pointcut("execution(* com.alibaba.spring.service.DeptServiceImpl.insertNoParam(..))")
	   public void beforePointCut(){}
	  @Before("beforePointCut()")
全局有参:@Pointcut("execution(* com.alibaba.spring.service.DeptServiceImpl.insertParam(..)) and args(userName,userAge,birth)")
	   public void beforePointCut(){}
	  @Before("beforePointCut() && args(userName,userAge,birth)")

后置通知:

局部无返回值:@AfterReturning(pointcut ="execution(* com.alibaba.spring.service.DeptServiceImpl.selectNoReturn(..))")
局部有返回值:@AfterReturning(pointcut ="execution(* com.alibaba.spring.service.DeptServiceImpl.select(..))",returning = "list")
全局无返回值:@Pointcut("execution(* com.alibaba.spring.service.DeptServiceImpl.selectNoReturn(..))")
	       public void afterReturningPointCut(){}
	      @AfterReturning("afterReturningPointCut()")
全局由返回值:@Pointcut("execution(* com.alibaba.spring.service.DeptServiceImpl.select(..))")
 	       public void afterReturningPointCut(){}
	      @AfterReturning(value = "afterReturningPointCut()",returning = "list")

环绕通知:

局部有参有返回值:@Around("execution (* com.alibaba.spring.service.DeptServiceImpl.select(..)) and args(id)")
全局有参有返回值:@Pointcut("execution (* com.alibaba.spring.service.DeptServiceImpl.select(..)) and args(id)")
		   public void aroundPointCut(){}
		  @Around("aroundPointCut() && args(id)")

异常通知:

局部异常通知:@AfterThrowing("execution (* com.alibaba.spring.service.DeptServiceImpl.insertExcption(..))")
全局异常通知:@Pointcut("execution (* com.alibaba.spring.service.DeptServiceImpl.insertExcption(..))")
	      public void exceptionPointCut(){}
	      @AfterThrowing("exceptionPointCut()")

最终通知:

局部最终通知:@After("execution (* com.alibaba.spring.service.DeptServiceImpl.insertExcption(..))")
全局最终通知:@Pointcut("execution (* com.alibaba.spring.service.DeptServiceImpl.insertExcption(..))")
	       public void afterPointCut(){}
	      @After("afterPointCut()")
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值