Santhosh
Santhosh

Reputation: 11774

spring: @bean CommandlineRunner. How it returns an object of type CommandlineRunner

I have seen in many places

@SpringBootApplication
public class Application {

    private static final Logger log =
            LoggerFactory.getLogger(Application.class);

    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner demo(UserRepository repo) {
        return (args) -> {
        };
    }

}

how does

@Bean
public CommandLineRunner demo(UserRepository repo) {
    return (args) -> {
    };
}

return an object of type CommandLineRunner

it return a function

(args) -> {
        };

I am not able to understand the syntax also.

Can someone help me to understand

Upvotes: 4

Views: 2842

Answers (3)

nicolass
nicolass

Reputation: 716

you should check functional interfaces of Java,

https://www.geeksforgeeks.org/functional-interfaces-java/

In syntheses,

A functional interface is a Java interface with only one abstract method (i.e., a method which has no instructions, only a name).

Functional interfaces are used together with lambda functions.

Suppose we have a functional interface

public interface JustAFunctionalInterface {
      void execute() {};
}

Notice we have only an abstract method, which does not do anything (no instructions).

This functional interface can be implemented like in your code above:

public class Main {
      public static void main(String[] args) {
            JustAFunctionalInterface variable = justAFunctionalInterface();
            variable.execute() // This will print "Hi"
      };

      public static JustAFunctionalInterface justAFunctionalInterface() {
          return (args) -> {System.out.println('Hi')} 
      }; // This chunk of code says that this method will return an interface of type 
         // JustAFunctionalInterface already implemented, where the abstract method "execute" is assigned the lambda function returned in here
}

Upvotes: 2

Tarun
Tarun

Reputation: 723

CommandLineRunner is an interface used to indicate that a bean should run when it is contained within a SpringApplication. A Spring Boot application can have multiple beans implementing CommandLineRunner. These can be ordered with @Order.

It has one abstract method :

    void run(String... args) throws Exception

Consider below example:

@Component
public class MyCommandLineRunner implements CommandLineRunner {

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

    @Override
    public void run(String... args) throws Exception {
        logger.info("Loading data..." + args.toString());

    }
}

Since CommandLineRunner contains only only abstract method which return nothing so it automatically becomes the functional interface i.e. we can write lambda expression for it.

Above class can be written as :

(args) -> { 
    logger.info("Loading data..." + args.toString()) 
};

Coming to Your example :

@Bean
public CommandLineRunner demo(String... args) {
    return (args) -> {
    };
}

You are expecting to register a bean in Spring Container which implements CommandLineRunner interface so it can converted to lambda expression.

(args) -> {};

Hope this would clarify it enough.

Upvotes: 4

Tal Glik
Tal Glik

Reputation: 510

CommandLineRunner is a functional interface with one method that return void.

That is why when you write: { }; everything is OK.

Upvotes: 0

Related Questions