【发布时间】:2015-03-16 06:47:50
【问题描述】:
我有一个非常简单的项目。
它有主类:
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);
}
}
控制器:
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";
}
}
还有一个服务(接口、实现):
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";
}
}
不幸的是,MyServiceImpl 的实例没有被注入到控制器类中的 myService 变量中。
我应该怎么做才能解决这个问题?
关于,
【问题讨论】:
-
您正在开发 spring mvc 应用程序。您需要正确初始化配置类。应用程序中的代码不足以使应用程序正常工作。
标签: java spring dependencies autowired code-injection