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

时间:2022-09-02 09:35:29

The problem is, I have written simple application in spring which gets data from database using jdbctemplate and prints it on site. I want it to be made using dependency injection through constructor or setters(I would like to see both approaches). I was trying to figure it on my own, but I have little experience with Spring and so on I can't make it working. Can anyone provide me with solution, how should it be done using code below?

问题是,我在spring编写了一个简单的应用程序,它使用jdbctemplate从数据库中获取数据并在现场打印。我想通过构造函数或setter使用依赖注入来创建它(我希望看到这两种方法)。我试图自己解决这个问题,但是我对Spring的经验很少,所以我无法让它工作。任何人都可以为我提供解决方案,如何使用下面的代码完成?

    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:

@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 个解决方案

#1


0  

You can make constructor based dependency injections by following this code snippet:

您可以通过以下代码片段进行基于构造函数的依赖注入:

JdbcTemplate jdbcTemplate;

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

Or you could do setter based dependency injection as:

或者你可以做基于setter的依赖注入:

JdbcTemplate jdbcTemplate;

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

#2


0  

First of All you should add @component annotation to your Customer class so this class will be managed by spring. then to inject the customer property in your controller you have 3 choices:

首先,您应该将@component注释添加到Customer类,以便此类将由spring管理。然后在您的控制器中注入客户属性,您有3个选择:

1) @Autowired Customer customer;

1)@Autowired客户客户;

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

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

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

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

#1


0  

You can make constructor based dependency injections by following this code snippet:

您可以通过以下代码片段进行基于构造函数的依赖注入:

JdbcTemplate jdbcTemplate;

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

Or you could do setter based dependency injection as:

或者你可以做基于setter的依赖注入:

JdbcTemplate jdbcTemplate;

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

#2


0  

First of All you should add @component annotation to your Customer class so this class will be managed by spring. then to inject the customer property in your controller you have 3 choices:

首先,您应该将@component注释添加到Customer类,以便此类将由spring管理。然后在您的控制器中注入客户属性,您有3个选择:

1) @Autowired Customer customer;

1)@Autowired客户客户;

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

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

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

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