spring 标注
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 标注的更多相关文章
- spring 标注 详解
http://snowolf.iteye.com/blog/578452 http://snowolf.iteye.com/blog/578452 非常棒的入门读物
- spring源码分析之cache注解
Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如EHCache 或者 OSCache),而是一个对缓存使用的抽象 ...
- Spring如何对私有接口进行注入(转载)
来自:http://didiluck.iteye.com/blog/1779640 Spring 标注@Autowired 如果做到自动装配私有变量而不使用set方法的原理 熟悉jdk的话就知道,方 ...
- 在eclipse中使用maven创建springMVC项目
一.在eclipse中创建maven-archetype-webapp项目: 1.新建项目选择maven项目 2.默认,下一步 3.选择maven-archetype-webapp,其他保持默认即可 ...
- eclipse创建springmvc项目
一.在eclipse中创建maven-archetype-webapp项目: 1.新建项目选择maven项目 2.默认,下一步 3.选择maven-archetype-webapp,其他保持默认即可 ...
- SpringMVC教程--eclipse中使用maven创建springMVC项目
一.在eclipse中创建maven-archetype-webapp项目: 1.新建项目选择maven项目 2.默认,下一步 3.选择maven-archetype-webapp,其他保持默认即可 ...
- Spring中用@Component、@Repository、@Service和 @Controller等标注的默认Bean名称会是小写开头的非限定类名
今天用调度平台去调用bean中的方法时,提示找不到bean.经查,发现是由于如果在标注上没有提供name属性值,则默认的bean名称是小写开头的,而不是大写开头的. 下面是其他文档参阅: 使用过滤器自 ...
- Spring MVC(二)基于标注的MVC
1.基于标注的Spring MVC 1.1 建立一个项目导入jar包(ioc aop mvc) 拷贝容器对应的配置文件到src下 在WEB-INF建立一个login.jsp 1.2 在web.xml ...
- spring4笔记----“零配置”:spring提供的几个Annotation标注
@Component :标注一个普通的Spring Bean类 @Controller :标注一个控制器组件器 @Service :标注一个业务逻辑组件器 @Repository ...
随机推荐
- 简单模拟Spring管理Bean对象
1: 首先我们要利用dom4j进行xml的解析,将所有的bean的配置读取出来. 2:利用java的反射机制进行对象的实例化. 3: 直接获得对象 package cn.Junit.test; imp ...
- SAP研究贴之--发票校验提示移动平均价为负
近日,应付岗密集出现发票校验时移动平均价为负值导致过账失败的情况,采购经理又是拍桌子.又是摔杯子的.财务经理安排任务彻底清查,找出问题原因.哎,毫无头绪啊...测试机模拟业务吧流程:合同(系统外)-采 ...
- struts2标签之列求和
struts2标签之列求和 <table width="100%" border="0" cellpadding="0" cellsp ...
- [转载] 使用异步 I/O 大大提高应用程序的性能
原文: http://www.ibm.com/developerworks/cn/linux/l-async/ Linux® 中最常用的输入/输出(I/O)模型是同步 I/O.在这个模型中,当请求发出 ...
- 僵尸进程学习 & 进程状态列表 & Linux信号学习
参考这篇文章: http://www.mike.org.cn/articles/treatment-of-zombie-processes-under-linux/ 在Linux进程的状态中,僵尸进程 ...
- 基础2 JVM
1. 内存模型以及分区,需要详细到每个区放什么. //运行时数据区域 方法区 Method Area 各个线程共享的内存区域 存储已被虚拟机加载的类信息 常量 静态变量 即时编译器编译后的代码 虚拟机 ...
- iOS——Xcode中添加第三方库
一.只有.h和.a文件的库 1.向项目中添加三方库文件 如果添加的第三方库只有.h和.a文件,直接把文件夹拖进项目下面,这时会弹出下面的提示框,一定要勾选下面选择的选项: 这里要注意,在Add to ...
- Maven最佳实践:划分模块
http://juvenshun.iteye.com/blog/305865 ************************************* "分天下为三十六郡,郡置守,尉,监& ...
- shelve模块
#coding:utf-8 __author__ = 'similarface' #email:similarface@outlook.com ''' shelve模块: 映射容器 存储对象,被存储的 ...
- 串行通讯之UARTLoopback
目录 第1章串行通讯之UARTLoopback 2 1 USB转串口 2 2 USB Accessory 2 3 连入手机 3 4 代码改进 4 5 打开串口 4 ...