Could not autowire field ParameterNameDiscoverer

912 Views Asked by At

I have a spring boot application wherein there is an aspect which logs method names and arguements.

@Component
@Aspect
public class LoggingAspect {

  @Autowired
  private ParameterNameDiscoverer parameterNameDiscoverer;

  private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);

  @Pointcut("within(@org.springframework.stereotype.Service *)")
  public void beanAnnotatedWithServiceOrASpecializationOfIt() {}    
  ...
  ...

}

But when I deploy the application it says

Could not autowire field: private org.springframework.core.ParameterNameDiscoverer

Following is a part of pom.xml file that is being used

<properties>
    <java.version>1.8</java.version>
    <start-class>SampleApplication</start-class>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.M5</version>
</parent>

I want to log the method arguement names and their values as an aspect. I am getting the method arguement values but not their names.

2

There are 2 best solutions below

0
leeor On BEST ANSWER

Assuming you are using java config, you need to make sure you have a class annotated somewhere in your code with an @Configuration and something like this:

@Bean
public ParameterNameDiscoverer parameterNameDiscoverer() {
    return new DefaultParameterNameDiscoverer();
}

Note: there may be a more appropriate implementation of ParameterNameDiscoverer for your particular use case, you should check the spring documentation for details.

0
James Baxter On

Do you actually have a ParameterNameDiscoverer in the spring context? If you expect this to be created as a part of the spring boot auto configuration, you will be able to see it mentioned in the auto configuration logs that are spat out by Boot on startup.

It might be you have to configure your own ParameterNameDiscoverer as a Bean, or add an extra classpath dependency in order for spring boot to create you one during auto configuration.