Dog
Dog

Reputation: 2916

Spring Boot application terminated with exit code 1

I'm new to Java and started a Spring application. However, when I instantiate a class in a different class and run a method the method does not output.

The class where the method is being called is SpringIn5StepsApplication.java and it looks like:

package com.example.springin5steps;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringIn5StepsApplication {

    public static void main(String[] args) {

        BinarySearchImpl binarySearch = new BinarySearchImpl();
        int result = binarySearch.binarySearch(new int[] {12, 4, 6}, 3);

        System.out.println(result);

        SpringApplication.run(SpringIn5StepsApplication.class, args);
    }
}

The BinarySearchImpl class looks like:

package com.example.springin5steps;

public class BinarySearchImpl {
    public static void main(String[] args) {

    }
    public int binarySearch(int[] numbers, int numberToSearchFor) {
        return 3;
    }
}

As the binarySearch method is being called, the expected output is 3, but instead nothing is output when I run the program. What am I doing wrong and how can I get the 3 to output to IntelliJ?

Upvotes: 1

Views: 8385

Answers (2)

Ameya Pandilwar
Ameya Pandilwar

Reputation: 2778

Since you are using Spring Boot, the application is already leveraging many features of the Spring Framework. One of them is that you don't have to explicitly create instances of beans as it manages that using something called as Inversion of Control (IoC) which is also more commonly known as Dependency Injection (DI). There is more information about that on that here. You should modify the main method as follows -

package com.example.springin5steps;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringIn5StepsApplication {
    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(SpringIn5StepsApplication.class, args);

        BinarySearchImpl binarySearch = applicationContext.getBean(BinarySearchImpl.class);
        int result = binarySearch.binarySearch(new int[] { 12, 4, 6 }, 3);
        System.out.println(result);
    }
}

Also, make sure your BinarySearchImpl class has the @Component annotation as follows -

package com.example.springin5steps;

import org.springframework.stereotype.Component;

@Component
public class BinarySearchImpl {
    public int binarySearch(int[] numbers, int numberToSearchFor) {
        return 3;
    }
}

@Component is a generic stereotype for any Spring-managed component.

Upvotes: 1

JUAN CALVOPINA M
JUAN CALVOPINA M

Reputation: 3984

When you use Spring you are using Dependency Injection, so you don't need to create new instances manually, instead you should use @Autowire, in base of your description I saw that you are use public static void main(String[] args)... in both classes, this is only used in the main class, a simply solution is to remove the main method from BinarySearchImpl, this should look like this:

package com.example.springin5steps;

public class BinarySearchImpl {
    public int binarySearch(int[] numbers, int numberToSearchFor) {
        return 3;
    }
}

Upvotes: 1

Related Questions