2

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?

12
  • Use a logger (log4j) and @autowire an instance into your main class Commented Jul 8, 2018 at 23:32
  • are you getting any exception? or your spring boot application is starting successfully? @user Commented Jul 8, 2018 at 23:33
  • there are no errors when it runs, but it just outputs: "Process finished with exit code 0" Commented Jul 8, 2018 at 23:34
  • 2
    Why in the world is Boot involved here? Just scrap the run call and you'll get exactly the same behavior. Commented Jul 9, 2018 at 0:12
  • 1
    @Dog if this is your first Java app, it might be easier to start with basics and just develop a Java app with a main() method and get this working first. There's nothing in your example code so far that is using or needs Spring Boot. Once you've grasped some Java basics it will be easier to step up to Spring Boot next Commented Jul 9, 2018 at 0:52

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.