一、顾问

通知的一种表现方式(顾问包装通知/增强)

Advisor:
  名称匹配方法:
    NameMecthMethodPointcutAdvisor

    1.定义了一个业务类

package cn.spring.advisor;
/**
* 业务接口
*/
public interface IService {
//业务方法
public void doSome(); public void say();
}

    2、定义里一个增强类,实现了增强接口

package cn.spring.advisor;

public class IServiceImpl implements IService{
@Override
public void doSome() {
System.out.println("=============真实业务=============");
} @Override
public void say() {
System.out.println("=============say=============");
}
}

    3.applicationContext.xml

        将业务类和增强类注入到Spring容器当中

    <!--注入业务Bean-->
<bean id="iServiceImpl" class="cn.spring.advisor.IServiceImpl"></bean>
<!--切面/通知-->
<bean id="myAdvisor" class="cn.spring.advisor.MyAdvisor"></bean>

        利用顾问包装增强

<bean id="advisor" class="org.springframwork.aop.support.NameMecthMethodPointcutAdvisor">
<property name="advice" ref="MyAdvisor"/>
<property name="mapperNames" value="*do*"/>
</bean>

        利用代理工程生成代理对象 

<bean id="proxyFactory" class="ProxyFactoryBean">
<property name="in" value="advisor"/>
<property name="target" ref="IService"/>
</bean>

  顾问自动代理:
    默认顾问自动代理

      applicationContext.xml

1.定义了一个业务类
2.定义了一个增强类 实现 增强接口
3.applicationContext.xml
3.1将业务类和增强类注入到Spring容器当中
3.2利用顾问包装增强
<bean id="advisor" class="org.springframwork.aop.support.RegrexMethodPointcutAdvisor">
<property name="advice" ref="MyAdvisor"/>
<property name="pattens" value=".*do.*"/>
</bean>
3.3顾问自动代理
<bean class="DefaultAdvisorAutoProxyCreator"/>

BeanName顾问自动代理

.定义了一个业务类
.定义了一个增强类 实现 增强接口
.applicationContext.xml
.1将业务类和增强类注入到Spring容器当中
.2利用顾问包装增强
<bean id="advisor" class="org.springframwork.aop.support.RegrexMethodPointcutAdvisor">
<property name="advice" ref="MyAdvisor"/>
<property name="pattens" value=".*do.*"/>
</bean>
.3顾问自动代理
<bean class="BeanNameAutoProxyCreator">
<property name="beanNames" value="iService"/>
<property name="Inters" value="advisor"/>
</bean>

二.IOC注解
  IOC注解需要需要在大配置文件中,扫描包 <context:compoent-scan base-package="cn.spring"/>
  注入:向Spring容器注入Bean的
    @Compoent 实现业务Bean的注入
    @Repository 专门注入Dao 基于@Compoent
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Component
    public @interface Repository {

    /**
    * The value may indicate a suggestion for a logical component name,
    * to be turned into a Spring bean in case of an autodetected component.
    * @return the suggested component name, if any (or empty String otherwise)
    */
    @AliasFor(annotation = Component.class)
    String value() default "";

    }
    @Service 专门注入Service
    @Controller 专门定位控制层

    定位:
    @Resource 默认根据Bean名称定位,如果名称为空,则根据Type定位
    @Autowired 默认根据ByType注入 一旦出现多个兼容类型的,那么我们需要根据名称区分@Qualifier

案例:

  Dao层接口

public interface IUserInfoMapper {
public int addUser(UserInfo info);
}

  IUserInfoMapperImpl类实现了Dao接口

@Repository
public class IUserInfoMapperImpl implements IUserInfoMapper { @Override
public int addUser(UserInfo info) {
System.out.println("添加成功");
return ;
}
}

  IUserInfoService接口

public interface IUserInfoService {
public int addUser(UserInfo info);
}

  IUserInfoServiceImpl类实现了IUserInfoService接口

/**
* @Service代表Service实现类的 标识,要让Spring管理Service
*/
@Service("iUserInfoServiceImpl")
public class IUserInfoServiceImpl implements IUserInfoService{ //植入Dao层
//@Resource默认是根据ByName的方式,但是一旦名字为空,就根据ByType
@Autowired
private IUserInfoMapper iUserInfoMapper; @Override
public int addUser(UserInfo info) {
return iUserInfoMapper.addUser(info);
}
}

  applicationContext.xml

<!--扫描注解:包扫描器-->
<context:component-scan base-package="cn.spring"/>

三、注解增强

  业务类

package cn.spring.aop;

import org.springframework.stereotype.Service;

/**
* 业务类
*/
@Service("IdoSomeService")
public class IdoSomeService {
public void doSome(){
System.out.println("业务当中的doSome方法");
} public void say(){
System.out.println("业务当中的say方法");
}
}

  增强类

package cn.spring.aop;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; /**
* 增强类
*/
@Aspect
@Component
public class MyAdvice { //定义一个空方法,为了可以应用切点表达式
@Pointcut("execution(* *..aop.*.do*(..))")
public void pointCut(){ } //自定义增强方法
@Before("pointCut()")
public void before(){
System.out.println("=================前置增强===================");
} //自定义方法增强
@AfterReturning("pointCut()")
public void after(){
System.out.println("===============后置增强==============");
}
}

  applicationContext.xml

<!--开启AOP注解支持-->
<aop:aspectj-autoproxy/>

  Test测试类

public class AopTest {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
IdoSomeService bea=(IdoSomeService)ctx.getBean("IdoSomeService");
bea.doSome();
}
}

   

Spring顾问、IOC注解和注解增强的更多相关文章

  1. Spring 的IOC容器之注解的方式

    1. 环境搭建 1.1 导入所需 jar 包 引入 IOC 容器必须的6个jar包; spring-aop-4.3.10.RELEASE.jar, Spring 框架的AOP的jar包; 1.2 创建 ...

  2. Spring框架(3)---IOC装配Bean(注解方式)

    IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...

  3. Spring系列(三):Spring IoC中各个注解的理解和使用

    原文链接:1. http://www.cnblogs.com/xdp-gacl/p/3495887.html       2. http://www.cnblogs.com/xiaoxi/p/5935 ...

  4. Spring IoC中各个注解的理解和使用

    一.把在Spring的xml文件中配置bean改为Spring的注解来配置bean 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的 ...

  5. 死磕Spring之IoC篇 - BeanDefinition 的解析过程(面向注解)

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...

  6. Spring IOC之基于注解的容器配置

    Spring配置中注解比XML更好吗?基于注解的配置的介绍提出的问题是否这种途径比XML更好.简单来说就是视情况而定. 长一点的答案是每一种方法都有自己的长处也不足,而且这个通常取决于开发者决定哪一种 ...

  7. (转)java之Spring(IOC)注解装配Bean详解

    java之Spring(IOC)注解装配Bean详解   在这里我们要详细说明一下利用Annotation-注解来装配Bean. 因为如果你学会了注解,你就再也不愿意去手动配置xml文件了,下面就看看 ...

  8. Spring的IOC注解开发入门2

    注解方式设置属性的值 在我们IOC基于xml属性注入的方式中有(一般推荐set方法) 构造方法注入普通值:<constructor-arg>的使用 set方法注入普通值:<prope ...

  9. Spring总结四:IOC和DI 注解方式

    首先我们要了解注解和xml配置的区别: 作用一样,但是注解写在Bean的上方来代替我们之前在xml文件中所做的bean配置,也就是说我们使用了注解的方式,就不用再xml里面进行配置了,相对来说注解方式 ...

  10. 七 Spring的IOC的注解方式

    Spring的IOC的注解方式入门 引入注解约束,配置组件扫描 类上的注解: @Conponent  @Controller @Service @Repository 普通属性的注解   @value ...

随机推荐

  1. 稀疏数组(java实现)

    1.稀疏数组 当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组. 稀疏数组的处理方法是: 1.1记录数组一共有几行几列,有多少个不同的值 1.2把具有不同值的元素的行列 ...

  2. (零)linux 学习 -- 从 shell 开始

    The Linux Command Line 读书笔记 - 部分内容来自 http://billie66.github.io/TLCL/book/chap02.html 文章目录 前言 什么是 she ...

  3. sendmail邮箱部署设置

    前言:在使用一些shell脚本进行监控时需要通过发送报警邮件来提醒,下面通过部署简单的sendmail来实现简单的邮件发送. 1.安装 mailx 和 sendmail: yum install ma ...

  4. AJAX调用案例随笔(个人观看使用)

    <script type="text/javascript"> /*var contextpath = "http://192.168.0.103:8080/ ...

  5. iview前台端口设置,跨域端口设置

    前台启动默认端口: 跨域端口: 完毕

  6. Verilog HDL

    https://wenku.baidu.com/view/9943b7acf524ccbff1218463.html https://hdlbits.01xz.net/wiki/Main_Page h ...

  7. nexus3上传jar包

    1.选择仓库位置 2.填写jar包信息 3.查看上传的jar包信息 上传成功. 4.maven的settings.xml完整配置 <?xml version="1.0" en ...

  8. 前端面试记录NO.1

    后端转前端,离职后第一次面试,技术面试的时候还是比较虚的,因为基础不是很扎实.主要问了工作中用过哪些技术,主流框架的区别,jQuery的掌握情况,Ajax的掌握情况,cookie的基本内容,还有浏览器 ...

  9. 每日一题-——LeetCode(46)全排列

    题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列.输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ...

  10. Oracle基础知识【1】

    目录: 1.Oracle创建库.表 2.Oracle数据怎删查改操作 3.Oracle数据约束条件 Oracle创建库.表: create user user05 identified by 1234 ...