0
votes

I want to execute a very simple example which explains the IoC-concept in Spring-Boot.

For that I have create a Bean which gets @Autowired to a main-class, which has a method which does something with the bean.

The bean:

enter image description here

The main:

@Component
public class MyMain {

    @Autowired
    private MyBean bean1;

    public void usingTheBean()
    {
        bean1.setName("Thats my first bean!");
        bean1.setAttribute("And thats just an attribute");

        System.out.println(bean1);
    }
    public static void main(String[] args) {
        //MyMain main = new MyMain();
        //main.usingTheBean();
    }
}

My SpringBootApplication:

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        MyMain main = new MyMain();
        main.usingTheBean();
    }
}

How can I start the Main-class? with out getting the

java.lang.NullPointerException 

for the @Autowired Bean "MyBean" in the main?

I know that the reason for the NullPointer-Exception is that I created the Main-class with the "new" keyword.

But the question focuses more on the question "How can I start a main-class with spring-boot"

1
why can't you make your MyMain class as spring boot starter class . - shivam
How can I then call the "usingTheBean"-Method? - watchme
You can just check my answer in this post on how to create a basic spring boot application : stackoverflow.com/questions/43447798/… - VNT
@VNT this example uses the RestController-Annotation. Which means, that we want to accomplish slightly different things. But still thanks for the link, I did help me ! - watchme
Incidentally this is why I always recomend constructor based dependancy injection rather than individually annotating each member, it makes it really clear when spring is or isn't going to dependancy inject. If you call the constructor yourself you need to provide the dependancys - Richard Tingle

1 Answers

2
votes

Usually, you do not want to use the context directly to create a bean yourself. You should just let the context initialize and then just use the autowired beans. Most likely, the way you approach this problem is very different from the Spring-way of achieving it.

You should have a look at the following examples:

  • using the CommandLineRunner interface (see here) or
  • using the InitializingBean interface (see here)

Alternatively, you can solve this via configuration:

@Configuration
public class MyConfig {

  @Bean
  public MyBean myBean() {
    MyBean bean = new MyBean();
    bean.setName("...");
    bean.setAttribute("...");
    return bean;
  }
}

You can then simply use

@Autowired
MyBean myBean;

to autowire it.

Yet another alternative would be to inject the values from a config file (e.g. application.properties) if this is possible in your case:

@Component
public class MyBean {

  @Value("${my.config.value}")
  private String name;

  @Value("${my.config.attribute}")
  private String attribute;

  public MyBean(){

  }
...

Having the following entries in your application.properties:

my.config.value = Some value content
my.config.attribute = Some attribute content