Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency injection in a simple Spring application does not work (injection Service into Controller variable)

I have a really simple project.

It has the main class:

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@ComponentScan
@EnableAutoConfiguration
public class Application {

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

}

A controller:

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class Controller {

    @Autowired
    MyService myService;

    @RequestMapping("/application")
    public String getApp(ModelMap model) {
        return "application";
    }

    @RequestMapping("/application/home")
    public String do(@RequestParam(value="input", required=false) String input, ModelMap model) {
        if (!StringUtils.isEmpty(input)) {
            model.addAttribute(INPUT_ATT, input);
            model.addAttribute(OUTPUT_ATT, myService.do(input));
        }
        return "home";
    }

}

And a service (interface, implementation):

package com.example;

public interface MyService {

    String do(String input);
}

.

package com.example;

import org.springframework.stereotype.Service;

@Service
public class MyServiceImpl implements MyService {

    @Override
    public String do(String input) {
        return "result";
    }

}

Unfortunately, an instance of MyServiceImpl does not get injected to myService variable in controller class.

What should I do to solve that problem?

With regards,

like image 961
user3231546 Avatar asked May 02 '26 23:05

user3231546


1 Answers

Your code is not compiled since 'do' is a keyword. But other than that, your code should be fine. I also try with SpringBoot with slight modification

Application

@SpringBootApplication
public class Application {

    /**
     * Run the Web Application using built-in Tomcat server.
     * This is used for testing only
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Controller

@RestController
public class HelloworldController {

    @Autowired
    private IHelloworldHelper helper;

    @RequestMapping("/hello")
    public Helloworld hello(String name) {
        return new Helloworld(1, helper.doAction(name));
    }
}

Helper

@Service
public class HelloworldHelper implements IHelloworldHelper {

    @Override
    public String doAction(String name) {
        return String.format("Hi %s!", name);
    }
}

public interface IHelloworldHelper {

    public String doAction(String name);
}

Model

public class Helloworld {

    private long id;
    private String content;

    public Helloworld(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}
like image 68
Anh Dũng Bùi Avatar answered May 05 '26 13:05

Anh Dũng Bùi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!