spring Annotation
使用注解替代xml
在前几章的笔记基础上添加使用注解的形式
1.配置applicationContext 添加context schema
<?xml version="1.0" encoding="UTF-8"?>
<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"
> <!-- 注解只需要这一句.指的是组件扫描开始的包,我们就写总包,从这个包及其子包 -->
<context:component-scan base-package="com.kaishengit"/>
<!-- aop注解还要添一句 -->
<aop:aspectj-autoproxy/> </beans>
2.Bean的注解()
这三个注解用哪个都一样,但是人为规定dao的用Repository,service的用Service,其他用Component
@Component(组件)
@Service(服务)
@Repository (存储,持久化)
---------------------------------------------
然后这就行了?
没有这句话啊<bean id="userDao" class="com.kaishengit.dao.UserDao"></bean>
怎么做到的?注意,默认的就是跟类型相同,首字母小写
不一样的时候@Repository("xxx")
@Repository
public class UserDao implements IUserDao {
} @Repository
@Scope("prototype")// 单例
public class UserDao implements IUserDao {
} @Repository
@Lazy(true)// lazy
public class UserDao {}
=======================================================================
=======================================================================
JSR 330 Standard Annotation这个组织 定义了注解@Named好处是sun官方的
跟上面三个功能一样
导入javax.inject.jar(源自JavaEE6)
import javax.inject.Named;
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
然后就可以使用named代替上面的三个注解
@Named
public class UserDao {}
--------------------------------------------------------
--------------------------------------------------------
解决ioc,依赖注入
IOC Annotation
@Autowired
@Inject
@Resource
//首先是bean管理
@Named
public class UserService {
/*依赖注入,首先是byName(,默认属性名首字母小写),找不到就byType,然后不需要写set*/
@Autowired
private IUserDao userDao; public void save() {
userDao.save();
} public void find() {
userDao.findName();
} }
JSR 330就可以用@inject注入
@Inject
private UserDao userDao;
========================================================
========================================================
aop注解
导入jar
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
配置中添加一句
<aop:aspectj-autoproxy/>
@Aspect//等同于<aop:aspect ref="myAspect">
@Named
public class MyAspect {
/*定义切入点表达式,随便定义一个方法,不需要有内容什么的,主要是能在这个方法上面加注解
*/
@Pointcut("execution(* com.kaishengit.dao..*.*(..))")
public void pointcut(){}
/*指定方法很简单,因为我在哪个方法上面加就是指定哪个方法
指定接入点表达式就是写上面定义切入点表达式的那个方法*/
@Before("pointcut()")
public void beforeAdvice() {
System.out.println("前置通知....");
} @AfterReturning(pointcut="pointcut()",returning="obj")
public void afterAdvice(Object obj) {
System.out.println("后置通知..." + obj);
} @AfterThrowing(pointcut="pointcut()",throwing="ex")
public void exceptionAdvice(Exception ex) {
System.out.println("异常通知..." + ex.getMessage());
} @After("pointcut()")
public void finallyAdvice() {
System.out.println("最终通知....");
} // 这是环绕通知,用于替代上面四个通知
@Around("pointcut()")
public void aroundAdvice(ProceedingJoinPoint jp) { try {
System.out.println("前置通知");
Object obj = jp.proceed();
System.out.println("后置通知");
} catch (Throwable e) {
e.printStackTrace();
System.out.println("异常通知");
} finally {
System.out.println("最终通知");
} }
spring Annotation的更多相关文章
- Spring Annotation Processing: How It Works--转
找的好辛苦呀 原文地址:https://dzone.com/articles/spring-annotation-processing-how-it-works If you see an annot ...
- Java 第六天 Spring Annotation 和其它
Annotation,是Java语言中的一种特殊的元数据语法,Spring支持使用annotation来进行对象实例化和装配 使用Annotation在Spring的配置xml中添加context命名 ...
- [转载]Spring Annotation Based Configuration
Annotation injection is performed before XML injection, thus the latter configuration will override ...
- Spring Annotation注解进行aop的学习
使用Maven管理项目,pom文件为: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...
- spring annotation功能备注
@Autowired @Autowired 注释可以在 setter 方法中被用于自动连接 bean.以type方式进行匹配. 一个构造函数 @Autowired 说明当创建 bean 时,即使在 ...
- Spring Annotation(注解)
Spring Boot Annotation @SpringBootApplication 必须作用在main 方法所在类 @RequestMapping @GetMapping @PostMappi ...
- spring annotation简述
一.Annotation基本概念 Annotation是jdk5以后出现的新特性,在jdk中,其内置了许多自己的Annotation,例如@Override,@SuppresWarning,@Depr ...
- hibernate spring annotation setup
First step setup for the pom.xml with hibernate dependency , hibernate dependency need to before the ...
- spring Annotation 笔记2.1
使用注解替代xml 在前几章的笔记基础上添加使用注解的形式 1.配置applicationContext 添加context schema <?xml version="1.0&quo ...
- spring Annotation 组分注塑
spring 注意分类 启动spring自己主动扫描功能 <context:component-scan/> 1.@Repository: 它用于将数据訪问层 (DAO 层 ) 的类标识为 ...
随机推荐
- [转载]spring security 的 logout 功能
原文地址:security 的 logout 功能">spring security 的 logout 功能作者:sumnny 转载自:http://lengyun3566.iteye ...
- python-字符串应用
例1:用python程序将DNA的一条链翻译出来s1=’ATTACGGC‘ rule={'A':'T','T':'A','C':'G','G':'C'} s1='ATTACGGC' s2=[rule[ ...
- adb 解说
ADB是一个 客户端-服务器端 程序, 其中客户端是你用来操作的电脑, 服务器端是android设备. 先说安装方法, 电脑上需要安装客户端. 客户端包含在sdk里. 设备上不需要安装, 只需要在手机 ...
- poj 2478 Farey Sequence 欧拉函数前缀和
Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Description The Farey Sequence Fn for ...
- Ngnix学习笔记
一.Ngnix介绍 1.概念 一个强大的Web服务器软件. 2.功能 1)处理高并发的http请求. 2)作为反向代理服务器来进行负载均衡. 3)数据压缩和解压缩处理 3.优势 高性能,轻量级,内存消 ...
- 将datagridview数据保为xml或txt文件
using System.IOpublic void SaveFile() { //实例化一个保存文件对话框 SaveFileDialog s ...
- java.lang.Exception: No runnable methods at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
使用方法测试时出现以下错误 java.lang.Exception: No runnable methods at org.junit.runners.BlockJUnit4ClassRunner.v ...
- virualbox andirodx86
背景 谷歌提供的andriod sdk模拟器在windows平台上很卡,是因为sdk是针对arm处理器架构的(就是嵌入式),而我们的windows系统是 x86架构 或者是 AMD架构,所以安卓模拟器 ...
- IT从业人员必看的10个论坛(转)
IT方面的论坛太多了,有综合,有专业,有行业,在各个论坛里混了几年,体会颇深,以前是论坛哪里人多,往哪里去,新浪论坛,网易是经常去的,人多啊,好几十万,去了以后才发现没有意思,没有共同的语言,于是逛专 ...
- 本地安装phpcms步骤
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u012145816/article/details/72183032 http://download ...