1、添加支持标注的spring中的jar包:

spring-context.jar

spring-context-support.jar

2、在xml中配置命名空间和schema

<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

3、启用基于注解的bean管理和依赖注入DI

    <context:component-scan base-package="com.spring1" />

表示,指定的基包以及子包都会纳入到spring的bean管理中。

4、Bean的注解(以下几种均可)

(1)dao里边的类一般用@Repository

(2)service里边的类一般用@Service

(3)其他的用@Component

aa

Spring mvc中的controller里边的类一般用@Controller

@Repository("dao")
@Scope("prototype")
@Lazy(true)
public class UserDao { public String showUser(){
System.out.println("show");
return "你好";
}
}

加上了注解,就可以在bean中去掉了。

5、JSR330的注解

需要导入javax.inject.jar

(1)@Named注解,只要加入Named就可以加入bean的管理了。

6、IOC,DI的注解(一下三种选一种均可)

(1)set注入用@AutoWired

    @Autowired
public void setUserDao(UserDao dao){
this.userDao = dao;
}

(2)将@AutoWired加到被注入的属性上边,可以将set方法去掉

@Named
public class UserService { @Autowired
private UserDao userDao; public void show(){
userDao.showUser();
} }

(3)jsr330中使用@Inject注入方法

@Named
public class UserService { @Inject
private UserDao userDao; public void show(){
userDao.showUser();
} }

(4)jsr250中使用@Resource注入方法

@Named
public class UserService { @Resource
private UserDao userDao; public void show(){
userDao.showUser();
} }

7、AOP注解

首先在xml中加入以下内容,加入aspect切面支持。

<aop:aspectj-autoproxy/>

(1)在切面类上首先用@Named注解或者其他的加入Bean管理,再加上@Aspect注解表示切面类

@Aspect
@Named
public class MyAspect { }

(2)定义切入点表达式,并加入@Pointcut(.......),包,类,方法,参数

@Aspect
@Named
public class MyAspect { @Pointcut("execution(* com.spring1.dao..*.*(..))")
public void pointcut(){} }

(3)定义通知

@Named
@Aspect
public class MyAspect { @Pointcut("execution(* com.spring1.dao..*.*(..))")
public void pointcut(){} @Before("pointcut()")
public void beforeAdvise(){
System.out.println("before");
}
@AfterReturning(pointcut="pointcut()",returning="o")
public void afterReturningAdvise(Object o){
System.out.println("after return");
} @After("pointcut()")
public void afterAdvise(){
System.out.println("finally");
}
@AfterThrowing(pointcut="pointcut()",throwing="e")
public void afterThrowningAdvise(Exception e){
System.out.println("throwning");
} }

(3)仅定义环绕通知

@Named
@Aspect
public class MyAspect { @Pointcut("execution(* com.spring1.dao..*.*(..))")
public void pointcut(){} @Around("pointcut()")
public void aroundAdvise(ProceedingJoinPoint pjp){
try{
System.out.println("after");
Object object = pjp.proceed();
System.out.println("after returning");
}catch(Throwable e){
System.out.println("after throwning");
}finally{
System.out.println("after");
}
}
}

spring 标注的更多相关文章

  1. spring 标注 详解

    http://snowolf.iteye.com/blog/578452 http://snowolf.iteye.com/blog/578452  非常棒的入门读物

  2. spring源码分析之cache注解

    Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如EHCache 或者 OSCache),而是一个对缓存使用的抽象 ...

  3. Spring如何对私有接口进行注入(转载)

    来自:http://didiluck.iteye.com/blog/1779640 Spring 标注@Autowired 如果做到自动装配私有变量而不使用set方法的原理  熟悉jdk的话就知道,方 ...

  4. 在eclipse中使用maven创建springMVC项目

    一.在eclipse中创建maven-archetype-webapp项目: 1.新建项目选择maven项目 2.默认,下一步 3.选择maven-archetype-webapp,其他保持默认即可 ...

  5. eclipse创建springmvc项目

    一.在eclipse中创建maven-archetype-webapp项目: 1.新建项目选择maven项目 2.默认,下一步 3.选择maven-archetype-webapp,其他保持默认即可 ...

  6. SpringMVC教程--eclipse中使用maven创建springMVC项目

    一.在eclipse中创建maven-archetype-webapp项目: 1.新建项目选择maven项目 2.默认,下一步 3.选择maven-archetype-webapp,其他保持默认即可 ...

  7. Spring中用@Component、@Repository、@Service和 @Controller等标注的默认Bean名称会是小写开头的非限定类名

    今天用调度平台去调用bean中的方法时,提示找不到bean.经查,发现是由于如果在标注上没有提供name属性值,则默认的bean名称是小写开头的,而不是大写开头的. 下面是其他文档参阅: 使用过滤器自 ...

  8. Spring MVC(二)基于标注的MVC

    1.基于标注的Spring MVC 1.1 建立一个项目导入jar包(ioc aop mvc) 拷贝容器对应的配置文件到src下 在WEB-INF建立一个login.jsp 1.2 在web.xml ...

  9. spring4笔记----“零配置”:spring提供的几个Annotation标注

    @Component  :标注一个普通的Spring Bean类 @Controller    :标注一个控制器组件器 @Service        :标注一个业务逻辑组件器 @Repository ...

随机推荐

  1. ubuntu安装jdk-6u45-linux-x64-rpm.bin

    1. 参考网址: http://www.xuebuyuan.com/2070575.html http://blog.csdn.net/csusunxgg/article/details/895602 ...

  2. mysql 事件调度器

    1.mysql事件调度器,也就是计划任务,计划做某事,有两种方式: 2.在某个时间点做某事,AT TIMESTAMP [+ INTERVAL INTERVAL] 某个时间点加上偏移. 3.定时地做某事 ...

  3. iOS开发之 Xcode 一个工程 Project 添加多个 target

    http://www.360doc.com/content/14/1203/11/19119980_430056974.shtml# 根据项目需求,同一个工程有多个版本,每个版本只有细微的不同.所以, ...

  4. hdu 1242 Rescue

    题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...

  5. java读取properties配置文件的方法

    app.properties mail.smtp.host=smtp.163.com mail.transport.protocol=smtp import java.io.InputStream; ...

  6. Three.js 3D特效学习

    一.Three.js基本介绍 Three.js是JavaScript编写的WebGL第三方库.提供了非常多的3D显示功能.Three.js 是一款运行在浏览器中的 3D 引擎,你可以用它创建各种三维场 ...

  7. OpenGL的glViewport视口变换函数详解[转]

    调用glViewPort函数来决定视见区域,告诉OpenGL应把渲染之后的图形绘制在窗体的哪个部位.当视见区域是整个窗体时,OpenGL将把渲染结果绘制到整个窗口. void glViewPort(G ...

  8. Akka学习博客

    http://www.iteblog.com/archives/1157 以示例介绍了actor模型.

  9. ASP.NET C#_HTML练习

    1. textarea和<input type=”text”>的区别是什么?  前者是多行输入框,后者是单行输入框 2. 如何让下拉框菜单支持多选? <select multiple ...

  10. Fragment 笔记

    1.getActivity()  为null问题 在Fragment基类里设置一个Activity mActivity的全局变量,在onAttach(Activity activity)里赋值,使用m ...