顾问和注解

一、顾问

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

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顾问自动代理

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="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 1;
    }
}
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();
    }
}
 
原文地址:https://www.cnblogs.com/wang01/p/11776774.html