一、顾问

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

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. 使用Docker搭建svn服务器教程

    svn简介 SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Subv ...

  2. 创建 Python Virtualenv 虚拟隔离环境

    video:创建 Python Virtualenv 虚拟隔离环境 python 虚拟环境 venv 简单用法 - littlemore - 博客园 创建 Python Virtualenv 虚拟隔离 ...

  3. Java Embeded 包 与各个架构之间的关系

    Oracle Java Embedded Suite 7.0 for Linux x86        V37917-01.zip        Oracle Java Embedded Suite ...

  4. Qt4 和 Qt5 模块的分类

    Qt5 与 Qt4 其中的一个区别是底层架构进行了改变,Qt5 引入了更加详细的模块化的概念,将众多功能细分到几个模块之中,Qt4 则是一种粗略的划分.本文主要对 Qt5 和 Qt4的模块进行一个简单 ...

  5. intel ipp6.0安装过程

    由于最近看到一个代码中使用了intel ipp6.0库,了解到,ipp6.0是一个很强大的图像处理库,将其与opencv联合使用,还能够加速opencv的处理,在图像处理的过程中,是一个很重要的工具. ...

  6. javascript -- 时间转换

    function numFormat(num){ //时间处理 return ('00' + num).substr(-2);    #处理 日期前面有0的情况}function timeFormat ...

  7. orangepi香橙派安装VNC Viewer远程桌面

    用ssh连接实在没有图形界面操作的好,虽然命令会快,但是很多命令都记不住. 第一步: sudo apt-get install xfce4 第二步: sudo apt-get install vnc4 ...

  8. synchronize与lock

    1. synchronize的作用 synchronize是java最原始的同步关键字,通过对方法或者代码块进行加锁实现对临界区域的保护.线程每次进去同步方法或者代码块都需要申请锁,如果锁被占用则会等 ...

  9. axios使用API

    背景:请求失败后,因跨域引起的不能传递statusCode问题,通过设置前后台选项解决,这里先总结一下axios的使用 一.安装与配置: 安装: npm install axios axios使用AP ...

  10. C#基础 - 定义变量,输入输出

    本节课主要讲解C#的发展历史及部分C#语言基础, 主要内容有:控制台程序的创建,输出,输入,定义变量,变量赋值,值覆盖,值拼接,值       打印,两种数据类型,整形类型转换 本节重点难点:定义变量 ...