小编max*_*lay的帖子

使用@Autowired与AspectJ和Springboot

我想将@Autowired注释用于"Aspect".我想在我的方面注入一个存储库但是当我尝试调用我的自动连接类的方法时,会发生NullPointException.

@Aspect
public class AspectSecurity {

@Autowired
private UserRepository userRepository;


@After("execution(public * dash.*.*Controller.*(..))")
public void authentication(JoinPoint jp) throws UnauthorizedException {
    System.out.println("SECURITY !");

    System.out.println(userRepository.findAll().toString());

   }
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试@Component在我的方面类上面添加,但我有同样的错误.

如果我不使用方面类,但@Controller我可以毫无问题地调用我的存储库.

一些文档说明了使用xml文件的spring配置,但是使用spring boot我没有这些文件.

这是我的pom.xml的一部分,它调用了aspectJ插件:

            <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.4</version>
            <configuration>
                <showWeaveInfo>true</showWeaveInfo>
                <source>1.6</source>
                <target>1.6</target>
                <Xlint>ignore</Xlint>
                <complianceLevel>${compiler.version}</complianceLevel>
                <encoding>UTF-8</encoding>
                <verbose>false</verbose>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjrt</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
            </dependencies>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

这是我的Application类:

package dash;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import …
Run Code Online (Sandbox Code Playgroud)

java spring aspectj autowired spring-boot

10
推荐指数
1
解决办法
1万
查看次数

标签 统计

aspectj ×1

autowired ×1

java ×1

spring ×1

spring-boot ×1