java学习知识积累-初识spring框架的”依赖注入”
控制层 注入式@Autowired 控制层代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.springapp.mvc; import com.springapp.core.FinArticleService; import com.springapp.model.News; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import java.util.List; @Controller @RequestMapping("/") public class HelloController { @Autowired private FinArticleService finArticleService; @RequestMapping(value = "index.html", method = RequestMethod.GET) public String printWelcome(ModelMap model) { List<News> newses = finArticleService.getArticleByPage(); model.addAttribute("newses", newses); return "index/index"; } } |
注入业务service文件 “FinArticleService…
Read more