日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Spring MVC快速教程:依赖注入 Spring MVC Fast Tutorial: Dependency Injection

發(fā)布時(shí)間:2025/3/21 javascript 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring MVC快速教程:依赖注入 Spring MVC Fast Tutorial: Dependency Injection 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

What are we going to build?

Use singletons of CarManager and BrandManager instead of creating multiple instances.

What is dependency injection ?

Dependency injection (also called inversion of control) is basically giving an object what it needs instead of letting this object get it by itself.

CarManager singleton for CarListController

For the moment, CarListController gets its instance of CarManager by itself:

CarManager carManager = new CarManager();

Let's change that by making it an attribute:

package springmvc.web;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller;import springmvc.service.CarManager;public class CarListController implements Controller {private CarManager carManager;public ModelAndView handleRequest(HttpServletRequest arg0,HttpServletResponse arg1) throws Exception {ModelAndView modelAndView = new ModelAndView("carList");modelAndView.addObject("carList", this.carManager.getCarList());return modelAndView;}public CarManager getCarManager() {return carManager;}public void setCarManager(CarManager carManager) {this.carManager = carManager;} }

We now need to instantiate a CarManager somewhere else and pass it to this controller. It's done in '/WEB-INF/springmvc-servlet.xml':

<bean id="carManager" class="springmvc.service.CarManager"/><bean name="/list_cars.html" class="springmvc.web.CarListController"><property name="carManager" ref="carManager"/> </bean>
  • a bean 'carManager' is created
  • it's used to initialize 'carManager' of CarListController.

We build (ant), relaunch Tomcat:http://localhost:8180/springmvc/list_cars.html

CarManager singleton for CarNewController

Do it by yourself, it's exactly the same. In the configuration file, use the CarManager instance you've already declared for '/new_car.html'.

CarManager

Since we now use a single instance of CarManager for our whole application, we don't need its carList attribute to be static anymore:

package springmvc.service;import java.math.BigDecimal; import java.util.LinkedList; import java.util.List;import springmvc.model.Brand; import springmvc.model.Car;public class CarManager {private List<Car> carList;public CarManager() {Brand brand1 = new Brand();brand1.setId((long)1);brand1.setName("Mercedes");brand1.setCountry("Germany"); Brand brand2 = new Brand();brand2.setId((long)2);brand2.setName("Peugeot");brand2.setCountry("France"); Car car1 = new Car();car1.setId((long)1);car1.setBrand(brand1);car1.setModel("SL 500");car1.setPrice(new BigDecimal(40000));Car car2 = new Car();car2.setId((long)2);car2.setBrand(brand2);car2.setModel("607");car2.setPrice(new BigDecimal(35000));carList = new LinkedList<Car>();carList.add(car1);carList.add(car2); }public List<Car> getCarList() {return carList;} public Car createCar(Car c) {Car car = new Car();car.setId((long)carList.size() + 1);car.setBrand(c.getBrand());car.setModel(c.getModel());car.setPrice(c.getPrice());carList.add(car);return car;} }

BrandManager

Let's do the same for BrandManager:

  • define an instance of BrandManager in the configuration file
  • use it for CarNewController (add a private attribute, remove instantiations)
  • make its brandList attribute not static

Externalize initialization code

We can go even further: initialize carList (CarManager) and brandList (BrandManager) in Spring configuration file:

<bean id="carManager" class="springmvc.service.CarManager"><property name="carList"><list><ref bean="car1"/><ref bean="car2"/></list></property></bean> <bean id="brandManager" class="springmvc.service.BrandManager"><property name="brandList"><list><ref bean="brand1"/><ref bean="brand2"/></list></property></bean><bean id="brand1" class="springmvc.model.Brand"><property name="id" value="1"/><property name="name" value="Mercedes"/><property name="country" value="Germany"/></bean><bean id="brand2" class="springmvc.model.Brand"><property name="id" value="2"/><property name="name" value="Peugeot"/><property name="country" value="France"/></bean><bean id="car1" class="springmvc.model.Car"><property name="id" value="1"/><property name="brand" ref="brand1"/><property name="model" value="SL 500"/><property name="price" value="40000"/></bean><bean id="car2" class="springmvc.model.Car"><property name="id" value="2"/><property name="brand" ref="brand2"/><property name="model" value="607"/><property name="price" value="35000"/></bean>
  • we define brands
  • we define cars using the brands
  • we initialize CarManager and BrandManager lists
  • notice the difference between the 'value' attribute, where you put a primary type and 'ref', where you give a bean name.

To make it work, we just need to add a setter method for CarManager and BrandManager's lists and remove the now useless initialization code. In 'WEB-INF/src/springmvc/service/BrandManager.java':

package springmvc.service;import java.util.List;import springmvc.model.Brand;public class BrandManager {private List<Brand> brandList;public List<Brand> getBrandList() {return brandList;} public void setBrandList(List<Brand> brandList) {this.brandList = brandList;}public Brand getBrandById(Long id) {for (Brand brand : brandList) {if (brand.getId().equals(id))return brand;}return null;} }

In 'WEB-INF/src/springmvc/service/CarManager.java':

package springmvc.service;import java.util.List;import springmvc.model.Car;public class CarManager {private List<Car> carList;public List<Car> getCarList() {return carList;} public void setCarList(List<Car> carList) {this.carList = carList;}public Car createCar(Car c) {Car car = new Car();car.setId((long)carList.size() + 1);car.setBrand(c.getBrand());car.setModel(c.getModel());car.setPrice(c.getPrice());carList.add(car);return car;} }

Much cleaner, isn't it??

We rebuild (ant), relaunch Tomcat:http://localhost:8180/springmvc/list_cars.html

Everything should still work.. You can download the project?here.


from:?http://jeromejaglale.com/doc/java/spring/dependency_injection

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的Spring MVC快速教程:依赖注入 Spring MVC Fast Tutorial: Dependency Injection的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。