使用注解替代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的更多相关文章

  1. Spring Annotation Processing: How It Works--转

    找的好辛苦呀 原文地址:https://dzone.com/articles/spring-annotation-processing-how-it-works If you see an annot ...

  2. Java 第六天 Spring Annotation 和其它

    Annotation,是Java语言中的一种特殊的元数据语法,Spring支持使用annotation来进行对象实例化和装配 使用Annotation在Spring的配置xml中添加context命名 ...

  3. [转载]Spring Annotation Based Configuration

    Annotation injection is performed before XML injection, thus the latter configuration will override ...

  4. Spring Annotation注解进行aop的学习

    使用Maven管理项目,pom文件为: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...

  5. spring annotation功能备注

      @Autowired @Autowired 注释可以在 setter 方法中被用于自动连接 bean.以type方式进行匹配. 一个构造函数 @Autowired 说明当创建 bean 时,即使在 ...

  6. Spring Annotation(注解)

    Spring Boot Annotation @SpringBootApplication 必须作用在main 方法所在类 @RequestMapping @GetMapping @PostMappi ...

  7. spring annotation简述

    一.Annotation基本概念 Annotation是jdk5以后出现的新特性,在jdk中,其内置了许多自己的Annotation,例如@Override,@SuppresWarning,@Depr ...

  8. hibernate spring annotation setup

    First step setup for the pom.xml with hibernate dependency , hibernate dependency need to before the ...

  9. spring Annotation 笔记2.1

    使用注解替代xml 在前几章的笔记基础上添加使用注解的形式 1.配置applicationContext 添加context schema <?xml version="1.0&quo ...

  10. spring Annotation 组分注塑

    spring 注意分类 启动spring自己主动扫描功能 <context:component-scan/> 1.@Repository: 它用于将数据訪问层 (DAO 层 ) 的类标识为 ...

随机推荐

  1. Codeforces Round #374 (Div. 2) A , B , C 水,水,拓扑dp

    A. One-dimensional Japanese Crossword time limit per test 1 second memory limit per test 256 megabyt ...

  2. 国内的Git比GitHub快

    GitHub的速度简直受不了! 被微软收购之后就堕落了! 用Gitee也挺好的,学习用吧!

  3. Python给数字前固定位数加零

    python中有一个zfill方法用来给字符串前面补0,非常有用 n = " s = n.zfill(5) " zfill()也可以给负数补0 n = "-123&quo ...

  4. aodh M版本新特性 - queue between alarm evaluator and alarm notifier

    之前alarm evaluator service and alarm notifier services之间的通信采用RPC的方式,消耗较大,增加work queue的方式可以获得更好的性能, + ...

  5. Linux上SFTP用法

    SFTP简介 sftp是一个交互式的文件传输协议,类似于ftp,但它进行加密传输,比ftp更安全. SFTP用法 localhost 从远程主机获取文件或目录到本地目录下 sftp>get /h ...

  6. 微软面向高并发应用而推出的新程序库——TPL Dataflow

    TPL Dataflow库的几个扩展函数 TPL Dataflow是微软面向高并发应用而推出的新程序库.借助于异步消息传递与管道,它可以提供比线程池更好的控制.本身TPL库在DataflowBlock ...

  7. EF切换到Mysql数据库,更改web.config

    1)引用: MySql.Data.dll,MySql.Data.Entity.dll,MySql.Data.Entity.EF6.dll 2)添加: <system.data> <D ...

  8. 《Advanced Bash-scripting Guide》学习(十二):占位符":"及其他

    本文所选的例子来自于<Advanced Bash-scripting Gudie>一书,译者 杨春敏 黄毅 : 在一个二元命令中提供一个占位符 例1. : ${username=`whoa ...

  9. Element 'beans' cannot have character [children], because the type's content type is element-only

    这个小问题快搞死我了,找了大半个小时. Element 'beans' cannot have character [children], because the type's content typ ...

  10. 前端 jQuery副本

    jQuery介绍 jQuery是一个轻量级的.兼容多浏览器的JavaScript库. jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方便地进行Ajax交互, ...