Spring注解详解目录一、注解说明 (2)1.使用简化配置 (2)2.使用Bean定义注解 (2)3.Spring支持以下4种类型的过滤方式: (2)二、注解介绍 (2)1.@Controller (3)2.@Service (3)3.@Autowired (4)4.@RequestMapping (4)5.@RequestParam (5)6.@ModelAttribute (6)7.@Cacheable和@CacheFlush (7)8.@Resource (7)9.@PostConstruct和@PreDestroy (8)10.@Repository (8)11.@Component(不推荐使用) (8)12.@Scope (8)13.@SessionAttributes (9)14.@InitBinder (9)15.@Required (9)16.@Qualifier (10)一、注解说明1.使用简化配置<context:annotationconfig/>将隐式地向Spring容器注册以下4个BeanPostProcessor:AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessorPersistenceAnnotationBeanPostProcessorRequiredAnnotationBeanPostProcessor2.使用Bean定义注解如果要使注解工作,则必须配置component-scan,实际上不需要再配置annotation-config。
base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。
还允许定义过滤器将基包下的某些类纳入或排除。
3.Spring支持以下4种类型的过滤方式:1)注解org.example.SomeAnnotation将所有使用SomeAnnotation注解的类过滤出来2)类名指定org.example.SomeClass过滤指定的类3)正则表达式com.kedacom.spring.annotation.web..*通过正则表达式过滤一些类4)AspectJ表达式org.example..*Service+通过AspectJ表达式过滤一些类二、注解介绍注解种类:1)@Controller2)@Service3)@Autowired4)@RequestMapping5)@RequestParam6)@ModelAttribute7)@Cacheable8)@CacheFlush9)@Resource10)@PostConstruct11)@PreDestroy12)@Repository13)@Component(不推荐使用)14)@Scope15)@SessionAttributes16)@InitBinder17)@Required18)@Qualifier1.@Controller例如@Controllerpublic class SoftCreateController extends SimpleBaseController{}或者@Controller("softCreateController")说明:@Controller负责注册一个bean到spring上下文中,bean的ID默认为类名称开头字母小写2.@Service例如@Servicepublic class SoftCreateServiceImpl implements ISoftCreateService{}或者@Service("softCreateServiceImpl")说明@Service负责注册一个bean到spring上下文中,bean的ID默认为类名称开头字母小写3.@Autowired例如@Autowiredprivate ISoftPMService softPMService;•或者@Autowired(required=false)private ISoftPMService softPMService=new SoftPMServiceImpl();说明@Autowired根据bean类型从spring上线文中进行查找,注册类型必须唯一,否则报异常。
与@Resource的区别在于,@Resource允许通过bean名称或bean类型两种方式进行查找@Autowired(required=false)表示,如果spring上下文中没有找到该类型的bean时,才会使用new SoftPMServiceImpl();@Autowired标注作用于Map类型时,如果Map的key为String类型,则Spring会将容器中所有类型符合Map的value对应的类型的Bean增加进来,用Bean的id或name作为Map的key。
@Autowired还有一个作用就是,如果将其标注在BeanFactory类型、ApplicationContext类型、ResourceLoader类型、ApplicationEventPublisher类型、MessageSource类型上,那么Spring会自动注入这些实现类的实例,不需要额外的操作。
4.@RequestMapping类@Controller@RequestMapping("/bbtForum.do")public class BbtForumController{@RequestMapping(params="method=listBoardTopic")public String listBoardTopic(int topicId,User user){}}方法@RequestMapping("/softpg/downSoftPg.do")@RequestMapping(value="/softpg/ajaxLoadSoftId.do",method=POST)@RequestMapping(value="/osu/product/detail.do",params={"modify=false"},meth od=POST)说明@RequestMapping可以声明到类或方法上参数绑定说明如果我们使用以下的URL请求:http://localhost/tt.do?method=listBoardTopic&topicId=1&userId=10&userName=t om topicId URL参数将绑定到topicId入参上,而userId和userName URL参数将绑定到user对象的userId和userName属性中。
和URL请求中不允许没有topicId参数不同,虽然User的userId属性的类型是基本数据类型,但如果URL中不存在userId 参数,Spring也不会报错,此时erId值为0。
如果User对象拥有一个dept.deptId的级联属性,那么它将和dept.deptId URL参数绑定。
5.@RequestParam参数绑定说明@RequestParam("id")http://localhost/tt.do?method=listBoardTopic&id=1&userId=10&userName=tom listBoardTopic(@RequestParam("id")int topicId,User user)中的topicId绑定到id这个URL 参数,那么可以通过对入参使用@RequestParam注解来达到目的@RequestParam(required=false):参数不是必须的,默认为true@RequestParam(value="id",required=false)请求处理方法入参的可选类型java基本数据类型和String默认情况下将按名称匹配的方式绑定到URL参数上,可以通过@RequestParam注解改变默认的绑定规则request/response/session既可以是Servlet API的也可以是Portlet API对应的对象,Spring会将它们绑定到Servlet和Portlet容器的相应对象上org.springframework.web.context.request.WebRequest内部包含了request对象java.util.Locale绑定到request对应的Locale对象上java.io.InputStream/java.io.Reader可以借此访问request的内容java.io.OutputStream/java.io.Writer可以借此操作response的内容任何标注了@RequestParam注解的入参被标注@RequestParam注解的入参将绑定到特定的request参数上。
java.util.Map/org.springframework.ui.ModelMap它绑定Spring MVC框架中每个请求所创建的潜在的模型对象,它们可以被Web视图对象访问(如JSP)命令/表单对象(注:一般称绑定使用HTTP GET发送的URL参数的对象为命令对象,而称绑定使用HTTP POST发送的URL参数的对象为表单对象)它们的属性将以名称匹配的规则绑定到URL参数上,同时完成类型的转换。
而类型转换的规则可以通过@InitBinder注解或通过HandlerAdapter的配置进行调整org.springframework.validation.Errors/org.springframework.validation.BindingResult 为属性列表中的命令/表单对象的校验结果,注意检验结果参数必须紧跟在命令/表单对象的后面org.springframework.web.bind.support.SessionStatus可以通过该类型status对象显式结束表单的处理,这相当于触发session清除其中的通过@SessionAttributes定义的属性请求处理方法返回值的可选类型•void此时逻辑视图名由请求处理方法对应的URL确定,如以下的方法:@RequestMapping("/welcome.do")public void welcomeHandler(){}对应的逻辑视图名为“welcome”•String此时逻辑视图名为返回的字符,如以下的方法:@RequestMapping(method=RequestMethod.GET)public String setupForm(@RequestParam("ownerId")int ownerId,ModelMap model){Owner owner=this.clinic.loadOwner(ownerId);model.addAttribute(owner);return"ownerForm";}对应的逻辑视图名为“ownerForm”•org.springframework.ui.ModelMap和返回类型为void一样,逻辑视图名取决于对应请求的URL,如下面的例子:@RequestMapping("/vets.do")public ModelMap vetsHandler(){return new ModelMap(this.clinic.getVets());}对应的逻辑视图名为“vets”,返回的ModelMap将被作为请求对应的模型对象,可以在JSP视图页面中访问到。