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深入探究08-连接池,分页
1.连接池 1)自定义连接池 思路:定义一个类Pool->添加4个属性(最大连接数,初始化连接数,当前连接数,用来存放连接对象的LinkList集合对象)->定义一个createConne ...
- CentOS 7 安装 docker-machine
https://github.com/docker/machine/releases/ 指令: curl -L https://github.com/docker/machine/releases/d ...
- EntityFramework 学习 一 Validate Entity
可以为实体实现自定义验证,重写DBContext中的个ValidateEntity方法 protected override System.Data.Entity.Validation.DbEntit ...
- vs 2010 mvc 3.0安装软件
下载链接如下:MVC 3安装包:http://www.microsoft.com/downloads/zh-cn/details.aspx?familyid=d2928bc1-f48c-4e95-a0 ...
- Mac键盘图标与对应快捷按键标志汇总 分类
Mac键盘图标与对应快捷按键 ⌘——Command () win键 ⌃ ——Control ctrl键 ⌥——Option (alt) ⇧——Shift ⇪——Caps Lock fn——功能键就是 ...
- 好的SQL写法
DECLARE @beginTime VARCHAR(20)= '2017-12-20 00:00:00';DECLARE @endTime VARCHAR(20)= '2017-12-26 00:0 ...
- 集成 ActiveMQ 到应用服务器
本章知识点 集成 ActiveMQ 和 Apache Tomcat 集成 ActiveMQ 和 Jetty 集成 ActiveMQ 和 Apache Geronimo 集成 ActiveMQ 和 JB ...
- java:Writer/Reader字符流操作
字符流的操作: 字节流的操作,是直接映射文件的:file->文件 字符流的操作是需要存在缓存区的:file->缓冲区->文件 (中文处理,一般用字符流) public static ...
- MVC3 ajax功能
微软mvc3框架的项目使用微软自带的ajax 必须引用下面 <script src="/Scripts/jquery.unobtrusive-ajax.js" type=&q ...
- Multiprovide 实践理解
“多重提供器”是一种“信息提供者”,它综合了多个“信息提供者”的数据以用于报告.“多重提供者”本身不包含任何数据.“多重提供者”的数据由所基于的“信息提供者”专门提供,这些“信息提供者”组合在一起进行 ...