首页 > 其他分享 >3. SpringMVC-使用注解开发-beans

3. SpringMVC-使用注解开发-beans

时间:2023-10-06 14:13:21浏览次数:36  
标签:xml SpringMVC Component Controller bean beans 注解

万能开头:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config/> <!--启动注解-->
    <context:component-scan base-package="zhe.xin"/> <!--扫描这个包下的注解-->

</beans>
  1.  新加入了:用于扫描包
    <context:component-scan base-package="zhe.xin"/> <!--扫描这个包下的注解-->
  2. 添加maven的依赖包 :aop     这里因为我导入了MVC包,里面包含了aop包,若不是MVC项目需要导入此包

  3.  使用:
    1. 加入@Component就可以被扫描到,并且添加为bean了 
    2. @Value只能设置基本类型,要设置类需使用前面所学内容@Resource或者@AutoWired和@qualifier
    3. @Value等同于 : <property name="name" value="小哲"/>

       

 

总结&提示:

  1. 由@Component衍深出: (用于分包,效果与Component相同)  都是将类放到容器中
    1. @Repository (dao包)
    2. @Service  (service业务包)
    3. @Controller (Controller控制包)
  2. 这种写法仅仅适用于普通的bean,复制的bean还是用xml合适,因为xml更加清晰明了
  3. 补充:@nullable可以声明此字段可以为空
  4. @scope  声明作用域
  5. bean还是被xml管理的 , 注解只是用于注入

 

 

 

 2023-10-06  13:59:21

 

标签:xml,SpringMVC,Component,Controller,bean,beans,注解
From: https://www.cnblogs.com/zhexin/p/17744516.html

相关文章

  • @RequestBody注解
    用途 用于接收前端传递给后端的json字符串中的数据。(处理json格式的数据)@RequestBody用来接收前端传递给后端的json字符串中的数据,GET方式的请求一般通过URL中携带key-value参数,而@RequestBody接收的是请求体中的数据(json格式的数据,只有请求体中能保存json),所以使用@Reque......
  • SpringMVC 数据校验
    SpringMVC数据校验应用程序在执行业务逻辑前,必须通过数据校验保证接收的输入数据时正确合法的.在一般情况下,应用程序的开发是分层的,不同层的代码由不同开发人员负责。很多时候,同样的数据校验会出现在不同层中,这样会导致代码冗余,为了避免这样的情况,最好是将验证逻辑和相应的域......
  • SpringMVC 拦截器
    SpringMVC拦截器<!--配置拦截器,可定义多个拦截器--><mvc:interceptors><!--定义1个拦截器--><mvc:interceptor><!--用于指定拦截器匹配url--><mvc:mappingpath="/user/**"/><!--用于指定拦截器排除的url-->&......
  • SpringMVC 文件上传
    SpringMVC文件上传<!--支持文件上传--><beanid="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!--maxUploadSizePerFile:单个文件大小限制maxUploadSize:整个请求大小限制--><propert......
  • SpringMVC 异常处理
    SpringMVC异常处理异常处理类ExceptionHandlerpackagecom.tobie.globalexception;importorg.springframework.ui.ModelMap;importorg.springframework.web.bind.annotation.ControllerAdvice;importorg.springframework.web.bind.annotation.ExceptionHandler;import......
  • 自定义注解实现AOP
    自定义注解AOPpackagecom.log;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.annotation.Before;importorg.aspectj.lang.annotation.Pointcut;importorg.aspectj.lang.reflect.MethodSignature;importorg.s......
  • SpringMVC入门demo
    工作流程demo使用:配置文件配置DispatcherServlet<?xmlversion="1.0"encoding="UTF-8"?><!--更新版本为4.0--><web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/j......
  • SpringMVC 类型转换
    SpringMVC类型转换Spring引入通用的数据类型转换系统,其定义了SPI接口和相应的运行时执行类型转换的API,提供无状态、强类型其可以在任意类型之间进行类型转换,可以用于任何需要的地方(如SpEL、数据绑定等)内置类型转换器<!--开启类型转换服务--><mvc:annotation-drivenconver......
  • spring注解开发---beans注入
    万能xml开头:<!--导入p,c命名空间context注解--><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"......
  • beans头
    MVC全部导入的xml头<!--导入p,c命名空间context注解--><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p&qu......