AspectJ不会运行

问题描述 投票:1回答:1

所以,我已经开始学习弹簧靴,我遇到了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();
    }
}

Main Class看起来像这样

@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名称:Tilak raj异常引发com.tilak.aop.MainApp.main(MainApp中)com.tilak.aop.Student.printThrowException(Student.java:33)中线程“main”java.lang.IllegalArgumentException的异常。 Java的:24)

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

建议应该运行,但事实并非如此。我在这里失踪了什么?

更新:

enter image description here

java spring-boot aop
1个回答
2
投票

你在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类而不是方面。

© www.soinside.com 2019 - 2024. All rights reserved.