一、顾问

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

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. 【程序人生】程序员真会玩,工作996,生病ICU

    昨天Github上一个项目彻底爆红了网络,短短一天star数突破一万,Issues已破1800,大家纷纷说出有关企业的不合理加班行为,句句吐露程序员的心声,掀起了一波抵制加班潮,抵制996. 该项目里 ...

  2. Cortex_m7内核cache深入了解和应用

    一,cache概述 从下图可以看出,从M7内核才开始有的cache,这对于从M0,M3,M4一路走来的小伙伴来说,多了一个cache就多了一个障碍. Cortex-M7 core with 32K/3 ...

  3. ubuntu 安装 Java 开发环境

    可以使用命令 -jre-headless 或者使用:   本文链接:https://blog.csdn.net/sangewuxie/article/details/80958611 本人的ubunt ...

  4. C++ 去掉字符串的首尾空格和全部空格

    #include <iostream>#include <string>using namespace std; //去掉收尾空格string& ClearHeadTa ...

  5. Html CSS transform matrix3d 3D转场特效

    Html CSS transform matrix3d 3D转场特效 透视矩阵 2n/(r-l) 0 (r+l)/(r-l) 0 0 2n/(t-b) (t+b)/(t-b) 0 0 0 (n+f)/ ...

  6. Android简单闹钟设置

    利用AlarmManager实现闹钟设置 //设置本地闹钟,actiongString:闹钟标识 setLocAlarm(int week, String actionString) { Calend ...

  7. 只需五分钟-用Maven快速搭建Spring Cloud微服务

    Maven安装手册 1.准备安装包 安装包: apache-maven-3.5.4-bin.zip  (最好JDK 1.7及以上版本) 集成包: eclipse-maven3-plugin.zip 2 ...

  8. hive分区理念介绍

    一.背景 1.在Hive Select查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作.有时候只需要扫描表中关心的一部分数据,因此建表时引入了partition概念. 2.分区表指的是在创建表 ...

  9. drf-过滤组件|分页组件|过滤器

    目录 drf-过滤组件|分页组件|过滤器 群查接口各种筛选组件数据准备 drf过滤组件 搜索过滤组件 | SearchFilter 案例: 排序过滤组件 | OrderingFilter 案例: dr ...

  10. CaaSP4 - 命令记录

    worker01:~ # tree /var/lib/containers/storage/ -L 1 /var/lib/containers/storage/ ├── mounts ├── over ...