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 层 ) 的类标识为 ...
随机推荐
- java 实现HTTP连接(HTTPClient)
在实习中,使用到了http连接,一直理解的很模糊,特地写个分析整理篇.分析不到位的地方请多多指教. Http 目前通用版本为 http 1.1 . Http连接大致分为2种常用的请求——GET,POS ...
- BZOJ 4066 kd-tree 矩形询问求和
第一次遇见强制在线的题目 每个操作都和前面的ans有关 所以不能直接离线做 在这个问题中 kdtree更像一个线段树在一维单点修改区间询问的拓展一样 如果区间被询问区间完全包含 就不用继续递归 插入时 ...
- Can't connect to MySQL server on 'localhost' (10061)的解决办法!
Can't connect to MySQL server on 'localhost' (10061)的解决办法! http://blog.sina.com.cn/s/blog_52ebca1f01 ...
- JavaScript -- 节点操作, 事件触发, 表单伸缩
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Guest CPU model configuration in libvirt with QEMU/KVM
每个hypervisor对于guest能看到的cpu model定义都不同,Xen 提供host pass through,所以guest能看到的cpu和host完全相同. QEMU/KVM中gues ...
- UML类图(一)-------概述+结构
类图用于描述系统中所包含的类以及它们之间的相互关系,帮助人们简化对系统的理解,它是系统分析和设计阶段的重要产物,也是系统编码和测试的重要模型依据. 1. 类 类(Class)封 ...
- 177. Nth Highest Salary
问题描述 解决方案 CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN declare number int; set numbe ...
- Educational Codeforces Round 33 (Rated for Div. 2)A-F
总的来说这套题还是很不错的,让我对主席树有了更深的了解 A:水题,模拟即可 #include<bits/stdc++.h> #define fi first #define se seco ...
- 25 python socket网络编程
一 客户端/服务器架构 1.硬件C/S架构(打印机) 2.软件C/S架构 互联网中处处是C/S架构 如黄色网站是服务端,你的浏览器是客户端(B/S架构也是C/S架构的一种) 腾讯作为服务端为你提供视频 ...
- SpringCloud教程 | 第五篇: 路由网关(zuul)
在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现.服务消费.负载均衡.断路器.智能路由.配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统.一个简答的微服务系统如下图: ...