【问题标题】:AspectJ won't runAspectJ 不会运行
【发布时间】:2018-09-04 19:49:18
【问题描述】:

所以,我开始学习spring boot,我遇到了AOP。

我给自己做了一个看起来像这样的方面

@Aspect
public class Logging {
    @Pointcut("execution(* com.tilak.*.*(..))")
    private void selectAll(){}


    @Before("selectAll()")
    private void beforeAdvice(){
        System.out.println("Going to set up student profile");
    }

    @After("selectAll()")
    private void afterAdvice(){
        System.out.println("student profile has been set.");
    }

    @AfterReturning(pointcut = "selectAll()" , returning = "retVal")
    private void afterReturningAdvice(Object retVal){
        System.out.println("Returning: "+   retVal.toString());
    }


    @AfterThrowing(pointcut = "selectAll()" , throwing = "ex")
    private void afterThrowingAdvice(IllegalArgumentException ex){
        System.out.println("There has been an exception: " + ex.toString());
    }

}

我还有一班学生,看起来像这样

@Component
public class Student {
    private Integer age;
    private String game;


    public Integer getAge() {
        System.out.println("Age : " + age );
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGame() {
        System.out.println("Name : " + game);
        return game;
    }

    public void setGame(String game) {
        this.game = game;
    }


    public void printThrowException(){
        System.out.println("Exception raised");
        throw new IllegalArgumentException();
    }
}

主类是这样的

@SpringBootApplication
public class MainApp {
    public static void main(String... z) {
        SpringApplication.run(MainApp.class, z);

        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanMe.class);
        Student student = (Student) applicationContext.getBean("student");



        student.getAge();
        student.getGame();
        student.printThrowException();

    }
}

Bean类是这样的

@Configuration
public class BeanMe {
    @Bean(name = "student")
    public Student studentBean(){
        Student student = new Student();
        student.setAge(24);
        student.setGame("Tilak raj");
        return student;
    }


    @Bean("logging")
    public Logging loggingBean(){
        return new Logging();
    }
}

Pom 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.springbootexample</groupId>
    <artifactId>firstspringboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <game>firstspringboot</game>
    <description>Practise project for spring boot
    </description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/>
        <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter-data-jpa</artifactId>-->
        <!--</dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>



        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

我的输出:

年龄:24 姓名:提拉克·拉吉 异常在线程“main”中引发异常 java.lang.IllegalArgumentException 在 com.tilak.aop.Student.printThrowException(Student.java:33) 在 com.tilak.aop.MainApp.main(MainApp.java:24)

我认为我已经包含了运行此运行所需的所有依赖项,但我没有得到预期的输出。

建议应该运行,但它们不是。我在这里错过了什么?

更新:

【问题讨论】:

  • @EnableAspectJAutoProxy ?
  • @SachinVerma 我应该把它放在哪里?
  • 将@Configuration 添加到Aspect 类,并请公开所有方面的方法。您可以将切入点设为私有,但方面不能设为私有,方面应该是公开的
  • @EgorKravchenko 做到了,还是一样的错误
  • 那么你的切入点也是错误的。 com.tilak.*.*(..) 匹配 com.tilak 包中的所有类和方法。它不匹配其他包中的所有类。第一个.* 是包中的所有类,第二个.* 是所有方法。您希望第一个 .*..*(注意 ..),其中还包括子包。

标签: java spring-boot aop


【解决方案1】:

main 方法中的代码做错了事。 SpringApplication.run(MainApp.class, z); 已经返回 ApplicationContext 您正在再次构建它,并且仅使用您的部分配置。缺少的部分是它没有启用 aspectj。

但是,当您重新加载已加载的上下文时,请不要这样做,请以正确的方式使用 Spring Boot。

@SpringBootApplication
public class MainApp {

    public static void main(String... z) {
        ApplicationContext ctx = SpringApplication.run(MainApp.class, z);
        Student student = ctx.getBean("student", Student.class);

        student.getAge();
        student.getGame();
        student.printThrowException();
    }
}

这将加载应用程序、检索 bean 并调用方法。

接下来你的切入点表达式也是错误的。 execution(* com.tilak.*.*(..)) 表示 com.tilak 包中类的所有方法的例外。由于您的课程在不匹配的 com.tilak.aop 包中。

使用execution(* com.tilak..*.*(..))` orexecution(* com.tilak.aop..(..))either will work. The first includes sub packages due to the..` 另一个使用完整包。

但是这样做会导致另一个问题,方面也会创建适用于自身(启动应用程序时您会注意到这一点)。因此,您希望将其限制为Student 类或排除使用@Aspect 注释的类。

要进行测试,您可以使用execution(* com.tilak.aop.Student.*(..)) 作为切入点,因为它只匹配Student 类而不匹配方面。

【讨论】:

  • 您自己在强制错误...您仍然应该看到以这种方式调用的方面。如果不是您发布的代码不是您实际使用的代码。
  • 我已经粘贴了所有我拥有的东西
  • 我还想提两件事,我在日志记录方面没有配置注释,而且我也在 mybean 类中制作了一个 bean。我从我的 bean 类中删除了 bean。我在 Logging 类上添加了一个配置注释。这个切入点也是错误。现在它正在工作。
  • 不要在方面添加@Configuration。它不是配置,如果有的话,它是一个组件。
  • 好的。谢谢。春季靴子有什么好的资源吗?我看到的只是一些复杂的东西。
猜你喜欢
  • 2021-07-10
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-09
  • 2023-03-27
相关资源
最近更新 更多