1.先看看项目的整体结构。

2.上代码
applicationContext.xml核心配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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/context http://www.springframework.org/schema/context/spring-context.xsd
               http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--  通过bean元素声明所需要的spring创建对象  id:创建的对象名  class:需要创建类对象的类路径   name:对象的属性名-->
<!--扫描包中注解标注的类文件-->
<!--    context:component-scan:扫描注解   base-package:需要扫描的包的路径-->
    <context:component-scan base-package="com.yang.dao,com.yang.service"></context:component-scan>

</beans>

entity类

package com.yang.entity;
public class Address {

    private Integer id;
    private String contact;
    private String addressDesc;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public String getAddressDesc() {
        return addressDesc;
    }

    public void setAddressDesc(String addressDesc) {
        this.addressDesc = addressDesc;
    }
}

dao接口类

package com.yang.dao;

import com.yang.entity.Address;
public interface AddressDao {

    public void addAddress(Address address);
}

dao实现类

package com.yang.dao;

import com.yang.entity.Address;
import org.springframework.stereotype.Repository;
@Repository //注解实现 dao层类组件
public class AddressDaoImpl implements AddressDao{
    @Override
    public void addAddress(Address address) {
        System.out.println("模拟数据库增删改查,新增成功!!!");
    }
}

service接口类

package com.yang.service;

import com.yang.entity.Address;
public interface AddressService {
    public void addAddress(Address address);
}

service实现类

package com.yang.service;

import com.yang.dao.AddressDao;
import com.yang.entity.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("service")
public class AddressServiceImpl implements AddressService{
    //直接在对象的属性上自动装配
    @Autowired
    private AddressDao addressDao;
    //通过set方法注入
//    @Autowired
//    public void setAddressService(AddressService addressService) {
//        this.addressService = addressService;
//    }

    @Override
    public void addAddress(Address address) {
    addressDao.addAddress(address);
    }
}

aop

package com.yang.aop;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import java.util.Arrays;
public class AddressAop {
    private static final Logger logger = Logger.getLogger(AddressAop.class);

    //创建切入点方法  @Pointcut切点的注解
    @Pointcut("execution(* com.yang.service.AddressService.*(..))")
    public void pointcut(){}

    //前增强处理
    @Before("pointcut()")
    public void before(JoinPoint jp){
        logger.debug("调用"+jp.getTarget()+"的"
                +jp.getSignature()+"方法,方法参数:"+ Arrays.toString(jp.getArgs()));
    }

    //后增强方法处理(pointcut指明切点方法   returning指明返回参数)
    @AfterReturning(pointcut ="pointcut()",returning ="result" )
    public void after(JoinPoint jp ,Object result){
        logger.debug("调用"+jp.getTarget()+"的"
                +jp.getSignature()+"方法,方法结果:"+ result);
    }

}

测试类

package com.yang.test;

import com.yang.entity.Address;
import com.yang.service.AddressService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAddress {
    @Test
    public void testAdd(){
        ApplicationContext context =  new ClassPathXmlApplicationContext("ApplicationContext.xml");
        AddressService addressService = (AddressService) context.getBean("service");
        Address address = new Address();
        address.setId(1);
        address.setContact("zhangsan");
        address.setAddressDesc("北京市东城区");
        addressService.addAddress(address);

    }
}

3.相关注解:
@Component 用来实现bean组件的注解定义(entity包下面)
@Repository 用来实现dao层的dao层组件(dao包下)
@Service 实现service层组件(service包下)
@Controller 实现控制器组件(controller、servlet包下)
@Aspect 设置切面类
@Pointcut("execution(* com.yang.service.UserService.*(..))") 创建切入点方法
@Before("pointcut()") 设置前增强方法
@AfterReturning(pointcut ="pointcut()",returning ="result" ) 设置后增强方法

posted on 2022-04-19 21:21  bug鸭~  阅读(18)  评论(0编辑  收藏  举报