代码之家  ›  专栏  ›  技术社区  ›  elec

如何进行基于构造函数或setter的依赖注入?

  •  1
  • elec  · 技术社区  · 7 年前

        public class Customer {
        private long id;
        private String firstName, lastName;
    
    
        public Customer(long id, String firstName, String lastName) {
            this.id = id;
            this.firstName = firstName;
            this.lastName = lastName;
        }
    
        @Override
        public String toString() {
            return String.format(
                    "Customer[id=%d, firstName='%s', lastName='%s']",
                    id, firstName, lastName);
        }
    }
    

    控制器:

    @Controller
    public class HelloController {
    
        @Autowired
        JdbcTemplate jdbcTemplate;
    
    
        Customer customer;
    
    
        @RequestMapping("/hello")
        public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {
            model.addAttribute("name", name);
            return "hello";
        }
    
        @RequestMapping("/getMock")
        public String getMock(Model model) {
    
            JdbcTemplate mock = Mockito.mock(JdbcTemplate.class);
    
            List fakeList = new ArrayList<>();
            fakeList.add(new Customer(1l, "sth", "sth2"));
    
    
    
            Mockito.when(mock.query(any(String.class), any(RowMapper.class))).thenReturn(fakeList);
    
            List<Customer> mockResult = mock.query(
                    "SELECT id, first_name, last_name FROM customers",
                    (rs, rowNum) -> new Customer(rs.getLong("id"), rs.getString("first_name"), rs.getString("last_name"))
            );
    
            String result = null;
            for(Customer customer : mockResult) result += (customer.toString() + "<br>");
    
            model.addAttribute("mockString", result);
            return "hello";
        }
    
        @RequestMapping("/getDatabase")
        public String getDatabase(Model model) {
            List<Customer> list = jdbcTemplate.query(
                    "SELECT id, first_name, last_name FROM customers",
                    (rs, rowNum) -> new Customer(rs.getLong("id"), rs.getString("first_name"), rs.getString("last_name"))
            );
    
            String result = null;
            for (Customer customer : list) result += (customer.toString() + "<br>");
            model.addAttribute("databaseString", result);
            return "hello";
        }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Hazim    7 年前

    您可以按照以下代码段进行基于构造函数的依赖项注入:

    JdbcTemplate jdbcTemplate;
    
    @Autowired
    public HelloController(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate=jdbcTemplate;
    }
    

    或者您可以执行基于setter的依赖项注入,如下所示:

    JdbcTemplate jdbcTemplate;
    
    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate=jdbcTemplate;
    }
    
        2
  •  1
  •   becem wniss    7 年前

    首先,您应该添加 @component 注释,以便该类由spring管理。然后,要将客户属性注入控制器中,您需要

    @Autowired Customer customer;

    2) @Autowired public void setCustomer(Customer customer) { this.customer = customer; }

    3) @Autowired public HelloController(Customer customer) { this.customer = customer; }