SpringMVC笔记3--annotation

SpringMVC使用annotation可以减少配置文件的书写。要启动annotation的功能,首先需要在SpringMVC的配置文件中配置下面的标签。

<mvc:annotation-driven />
<context:component-scan base-package="com.my.webstore" />

annotation主要有以下这些:
@Controller 修饰一个Controller类,是表现层组件
@RequestMapping 绑定一个url到controller类或者controller中的函数。
@Repository 表示组件是一个资源类,这个资源类主要是一些CURD操作的函数,直接和数据库交互,为持久层组件
@Service 表明是一个Service组件,其中会依赖到一些Repository 组件,调用Repository的多个CURD操作。表示业务的逻辑,为业务层组件

范例:

@Controller,@RequestMapping

package com.my.webstore.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.my.webstore.domain.repository.ProductRepository;

@Controller
public class HomeController {
	
	@Autowired
	private ProductRepository repository;
	
	@RequestMapping("/products")
	public String welcome(Model model) {
		model.addAttribute("greeting", "Welcome	to	Web	Store!");
		model.addAttribute("tagline", "The	one	and	only	amazing	webstore");
		
		model.addAttribute("products", repository.getAllProducts());
		
		return "index";
	}
}

@Repository

package com.my.webstore.domain.repository.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.my.webstore.domain.Product;
import com.my.webstore.domain.repository.ProductRepository;

@Repository
public class ProductRepositoryImpl implements ProductRepository {
	private List<Product> list = new ArrayList<Product>();


	public ProductRepositoryImpl() {
		Product iphone = new Product("P1234", "iPhone	5s", new BigDecimal(500));
		iphone.setDescription("Apple	iPhone	5s	smartphone	with	4.00-inch	640x1136	display	and	8-megapixel	rear	camera");
		iphone.setCategory("Smart	Phone");
		iphone.setManufacturer("Apple");
		iphone.setUnitsInStock(1000);

		Product laptop_dell = new Product("P1235", "Dell	Inspiron",
				new BigDecimal(700));
		laptop_dell
				.setDescription("Dell	Inspiron	14-inch	Laptop	(Black)	with	3rd	Generation	Intel	Core	processors");
		laptop_dell.setCategory("Laptop");
		laptop_dell.setManufacturer("Dell");
		laptop_dell.setUnitsInStock(1000);

		Product tablet_Nexus = new Product("P1236", "Nexus	7", new BigDecimal(
				300));
		tablet_Nexus
				.setDescription("Google	Nexus	7	is	the	lightest	7	inch	tablet	With	a	quad-core	Qualcomm	Snapdragon™	S4	Pro	processor");
		tablet_Nexus.setCategory("Tablet");
		tablet_Nexus.setManufacturer("Google");
		tablet_Nexus.setUnitsInStock(1000);

		list.add(iphone);
		list.add(laptop_dell);
		list.add(tablet_Nexus);

	}
	
	public List<Product> getAllProducts() {
		// TODO Auto-generated method stub
		return list;
	}

	public Product getProductById(String productId) {
		// TODO Auto-generated method stub
		if (productId == null) {
			throw new IllegalArgumentException("productId can't be null");
		}
		Product tmpProduct = null;
		for (Product product : list) {
			if (product.getProductId().equals(productId)) {
				tmpProduct =  product;
				break;
			}
		}
		
		if (tmpProduct == null) {
			throw new IllegalArgumentException("product can not be found:"+productId);
		}
		
		return tmpProduct;
	}
}

@Service

package com.my.webstore.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.my.webstore.domain.Product;
import com.my.webstore.domain.repository.ProductRepository;

@Service
public class OrderServiceImpl implements OrderService {
	
	@Autowired
	private ProductRepository repository;

	public void processOrder(String productId, int count) {
		// ODO Auto-generated method stub
		Product product = repository.getProductById(productId);
		if (product.getUnitsInStock() < count) {
			throw new IllegalArgumentException("");
			
		}
		product.setUnitsInStock(product.getUnitsInStock()- count);
		
	}

}

原文地址:https://www.cnblogs.com/xiaozu/p/4639006.html