Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not autowire field ParameterNameDiscoverer

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.

like image 892
Soumya Avatar asked Sep 02 '25 16:09

Soumya


2 Answers

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.

like image 135
leeor Avatar answered Sep 04 '25 04:09

leeor


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.

like image 38
James Baxter Avatar answered Sep 04 '25 05:09

James Baxter