其他分享
首页 > 其他分享> > SpringAOP第一个案例

SpringAOP第一个案例

作者:互联网

1.Aop解决的问题

3.Aop的专有名词

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.2</version>
        </dependency>
public interface UserService {
    int insertUser();
    int update(SuperUser user);
}
/**
 * @author ngLeede
 * @version 1.0
 * @Desc service的实现类
 * @date 2021/6/10 22:07
 */
public class UserServiceImpl implements UserService {
    
    private UserMapper userMapper;
    
    public UserMapper getUserMapper() {
        return userMapper;
    }
    
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
    
    @Override
    public int insertUser() {
        userMapper.insertUser();
        return 0;
    }
    
    @Override
    public int update(SuperUser user) {
        userMapper.update(user);
        return 0;
    }
}
/**
 * @desc mapper接口
 * @author ngLeede
 * @date 2021/6/11 22:27
 */
public interface UserMapper {
    int insertUser();
    
    void update(SuperUser user);
}
/**
 * @author ngLeede
 * @version 1.0
 * @Desc mapper接口的实现类,对应的行为是SSM框架里的mapper.xml 
 * @date 2021/6/10 22:06
 */
public class UserMapperImpl implements UserMapper {
    @Override
    public int insertUser() {
        System.out.println("--插入数据--mapper接口--");
        return 0;
    }
    
    @Override
    public void update(SuperUser user) {
        System.out.println("---修改超人信息---");
    }
}
/**
 * @author ngLeede
 * @version 1.0
 * @Desc 增强类
 * @date 2021/6/10 22:46
 */
public class MyAdvice implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        String mt = method.getName();
        System.out.println("前置增强"+mt+"方法");
    }
}
<?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"
       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">
    <!--第一个aopyi 实例-->
    <bean id="userMapper" class="com.ngLeede.aopyi.mapper.impl.UserMapperImpl"></bean>
    <bean id="userService" class="com.ngLeede.aopyi.service.impl.UserServiceImpl">
        <property name="userMapper" ref="userMapper" />
    </bean>
    <bean id="myadvice" class="com.ngLeede.aopyi.advice.MyAdvice"></bean>
    <aop:config>
        <!--切点表达式 增强service下的方法-->
        <aop:pointcut id="ponitcut" expression="execution(* com.ngLeede.aopyi.service.*.*(..))"/>
        <!--织入-->
        <aop:advisor advice-ref="myadvice" pointcut-ref="ponitcut"/>
    </aop:config>
</beans>
public class AppTest 
{
    /**
     * @desc 測試第一Aop實例
     * @author ngLeede
     * @date 2021/6/10 22:12
     */
    @Test
    public void testAopyi(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) applicationContext.getBean("userService");
        userService.insertUser();
        SuperUser user = new SuperUser();
        user.setName("超人");
        user.setPhone("167777");
        userService.update(user);
        
    }
}

标签:第一个,int,insertUser,userMapper,案例,user,AOP,SpringAOP,public
来源: https://www.cnblogs.com/ngLee/p/14876730.html