spring Annotation 笔记2.1
使用注解替代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 笔记2.1的更多相关文章
- 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- struts2,hibernate,spring整合笔记(2)
上一话struts2,hibernate,spring整合笔记(1) 接下来继续 配置完struts之后就要开始hibernate的配置 hibernate的环境并不依赖web开发环境,在我第一次配置 ...
- 不错的Spring学习笔记(转)
Spring学习笔记(1)----简单的实例 --------------------------------- 首先需要准备Spring包,可从官方网站上下载. 下载解压后,必须的两个包是s ...
- 【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回
作者:ssslinppp 时间:2015年5月26日 15:32:51 1. 摘要 本文讲解如何利用spring MVC框架,实现ajax异步请求以及json数据的返回. Spring MV ...
- 【Spring学习笔记-MVC-4】SpringMVC返回Json数据-方式2
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- 【Spring学习笔记-MVC-3】SpringMVC返回Json数据-方式1
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- spring boot 笔记--第三章
spring boot 笔记 第三章,使用Spring boot 构建系统: 强烈建议支持依赖管理的构建系统,Maven或Gradle 依赖管理: Spring Boot的每版本都会提供它支持的依赖列 ...
- 2.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:
转自:https://www.cnblogs.com/ssslinppp/p/4528892.html 个人认为,使用@ResponseBody方式来实现json数据的返回比较方便,推荐使用. 摘要 ...
- 3.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:
转自:https://www.cnblogs.com/ssslinppp/p/4528892.html 概述 在文章:<[Spring学习笔记-MVC-3]SpringMVC返回Json数据-方 ...
随机推荐
- Ubuntu 麒麟版下安装:Apache+php5+mysql+phpmyadmin.
摘要 LAMP是Linux web服务器组合套装的缩写,分别是Apache+MySQL+PHP.此文记录在Ubuntu上安装Apache2服务器,包括PHP5(mod_php)+MySQL+phpmy ...
- 校园招聘 - 比較easy的面试题
又到校园招聘的季节了, 自从和一些同事出版了<编程之美>一书之后, 我常常收到一些关于面试, 编程, 和"题库"的询问. 事实上我自己对算法没有什么研究, 有些问题都 ...
- JavaScript之将JS代码放在什么位置最合适
1.放到<head></head>标签里面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/ ...
- zoj 2067 White Rectangles
这题解决的算法处理,真的很难想清楚!!尤其是最后的正矩形如何处理.不过终于看懂了 #include<stdio.h> #include<stdlib.h> #include&l ...
- 枚举与define的区别
1.枚举enum的用途浅例 写程序时,我们常常需要为某个对象关联一组可选alternative属性.例如,学生的成绩分A,B,C,D等,天气分sunny, cloudy, rainy等等. ...
- Immediate Decodability
Description An encoding of a set of symbols is said to be immediately decodable if no code for one s ...
- BZOJ 2173: 整数的lqp拆分( dp )
靠着暴力+直觉搞出递推式 f(n) = ∑F(i)f(n-i) (1≤i≤n) (直接想大概也不会很复杂吧...). f(0)=0 感受一下这个递推式...因为和斐波那契有关..我们算一下f(n)+f ...
- JAVA语言规范和API网址
Java语言规范: http://docs.oracle.com/javase/specs/ Java API: http://docs.oracle.com/javase/8/docs/api/in ...
- ssh远程登录linux服务器
ssh远程登录linux服务器 用法: ssh -l user -p port server_ip 或者 ssh -p port user@server_ip 参数: -l 后接要登录的远程系统用户名 ...
- 射频识别技术漫谈(16)——Mifare UltraLight
Mifare UltraLight又称为MF0,从UltraLight(超轻的)这个名字就可以看出来,它是一个低成本.小容量的卡片.低成本,是指它是目前市场中价格最低的遵守ISO14443A协议的芯片 ...