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(); } } }
Spring——注解的更多相关文章
- spring注解源码分析--how does autowired works?
1. 背景 注解可以减少代码的开发量,spring提供了丰富的注解功能.我们可能会被问到,spring的注解到底是什么触发的呢?今天以spring最常使用的一个注解autowired来跟踪代码,进行d ...
- Spring注解
AccountController .java Java代码 1. /** 2. * 2010-1-23 3. */ 4. packag ...
- spring 注解的优点缺点
注解与XML配置的区别 注解:是一种分散式的元数据,与源代码耦合. xml :是一种集中式的元数据,与源代码解耦. 因此注解和XML的选择上可以从两个角度来看:分散还是集中,源代码耦合/解耦. 注解的 ...
- spring注解说明之Spring2.5 注解介绍(3.0通用)
spring注解说明之Spring2.5 注解介绍(3.0通用) 注册注解处理器 方式一:bean <bean class="org.springframework.beans.fac ...
- 使用Spring注解来简化ssh框架的代码编写
目的:主要是通过使用Spring注解的方式来简化ssh框架的代码编写. 首先:我们浏览一下原始的applicationContext.xml文件中的部分配置. <bean id="m ...
- spring注解scheduled实现定时任务
只想说,spring注解scheduled实现定时任务使用真的非常简单. 一.配置spring.xml文件 1.在beans加入xmlns:task="http://www.springfr ...
- [转]Spring 注解总结
原文地址:http://blog.csdn.net/wangshfa/article/details/9712379 一 注解优点?注解解决了什么问题,为什么要使用注解? 二 注解的来龙去脉(历史) ...
- eclipes的Spring注解SequenceGenerator(name="sequenceGenerator")报错的解决方式
eclipes的Spring注解SequenceGenerator(name="sequenceGenerator")报错的解决方式 右键项目打开Properties—>JA ...
- Spring注解【非单例】
花了至少一整天的时间解决了这个问题,必须记录这个纠结的过程,问题不可怕,思路很绕弯. 为了能说清楚自己的问题,我都用例子来模拟. 我有一个类MyThread是这样的: @Service public ...
- spring注解方式在一个普通的java类里面注入dao
spring注解方式在一个普通的java类里面注入dao @Repositorypublic class BaseDaoImpl implements BaseDao {这是我的dao如果在servi ...
随机推荐
- Java 代码运行顺序
1.静态代码块,只执行一次,从上到下,先父类后子类 2.父类构造代码块,从上到下,然后父类构造方法,执行次数与实例化次数相关 3.子类构造代码块,从上到下,然后子类构造方法,执行次数与实例化次数相关
- SQL With AS Expression
A. Creating a simple common table expression The following example shows the total number of sales o ...
- MVC中的Action过滤器
Action过滤器可以用在调用动作方法之前或之后,执行一些特殊的逻辑,比如用登录验证: Action过滤器实现IActionFilter接口,该接口有两个方法: public interface IA ...
- gcc/g++ 以及makefile
生成可执行文件 g++ mutiprocess.cpp -o test -fpic:产生位置无关码,位置无关码就是可以在进程的任意内存位置执行的目标码,动态链接库必须使用 -c : 只生成 .o ...
- css————关于margin:0px auto的几个居中问题
前言 margin:0px auto;适用于指定了固定宽度的div与其它元素,比如p,img等,使用 margin:0px auto,居中是大家在做css div定位时的最常用方法,但是据我自己的使用 ...
- 数组通常在JS中使用
数组通常在JS中使用,例如具有相同名称的多个输入.如果它们是动态生成的,则需要在提交时确定它们是否是数组.如果(文件).MyList.长度!=“未定义”)此用法不正确.正确的是如果(文件.MyList ...
- Groovy--使用模板引擎和GroovyShell执行插值字符串
package curveJudge import groovy.text.SimpleTemplateEngine /** * Created by Jxy on 2019/8/26 17:16 * ...
- Java学习笔记【十三、多线程编程】
概念 Java 给多线程编程提供了内置的支持.一个多线程程序包含两个或多个能并发运行的部分.程序的每一部分都称作一个线程,并且每个线程定义了一个独立的执行路径. 多线程是多任务的一种特别的形式,但多线 ...
- vlan linux内核数据流程
转:http://blog.sina.com.cn/s/blog_62bbc49c0100fs0n.html 一.前言 前几天做协议划分vlan的时候看了一些linux内核,了解不深,整理了下vlan ...
- Tomcat集成到MyEclipse
1.Tomcat集成到MyEclipse 使用MyEclipse配置服务器后,就可以使用MyEclipse来启动和停止服务器了.当然,你需要先安装好服务器(Tomcat),才能配置.MyEclipse ...