Spring笔记(一)

Spring

1、文档注释

应用场景注解注解说明
处理请求 @Controller 处理 Http 请求
处理请求 @RestController @Controller 的衍生注解
路由请求 @RequestMapping 路由请求 可以设置各种操作方法
路由请求 @GetMapping GET 方法的路由
路由请求 @PostMapping POST 方法的路由
路由请求 @PutMapping PUT 方法的路由
路由请求 @DeleteMapping DELETE 方法的路由
请求参数 @PathVariable 处理请求 url 路径中的参数 /user/{id}
请求参数 @RequestParam 处理问号后面的参数
请求参数 @RequestBody 请求参数以json格式提交
返回参数 @ResponseBody 返回 json 格式
  • 依赖:spring-webmvc

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.2.9.RELEASE</version>
</dependency>

快捷键

Ctrl+Alt+V:生成变量名

psvm:main函数

sout:输出

Alt + Enter:强制类型转换

Ctrl + /: 注释

Ctrl + SHift + /:块注释

踩坑

<bean id="hello" class="com.promefire.Hello">
      <property name="str" value="Spring"/>
  </bean>

name 后面的值 要与Hello类中变量相同

 

id = 变量名 class = new的对象

Hello hello = new Hello()

property 给对象中的属性设置一个值

一、IOC理论

对于新增或修改业务,不需要修改代码。只需要修改配置文件,

二、实例化容器

bean骨架

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

   <bean id="..." class="..."> (1) (2)
       <!-- collaborators and configuration for this bean go here -->
   </bean>

   <bean id="..." class="...">
       <!-- collaborators and configuration for this bean go here -->
   </bean>

   <!-- more bean definitions go here -->

</beans>

三、IOC创建对象方式

1、默认使用无参构造创建

2、使用有参构造

1、下标赋值

<bean id="user" class="com.promefire.injection.User">
       <!-- collaborators and configuration for this bean go here -->
       <constructor-arg index="0" value="智杰"/>
   </bean>

2、类型赋值

<bean id = "user" class = "com.promefire.injection.User">
       <constructor-arg type="java.lang.String" value=" (Type赋值)智杰"/>
   </bean>

不建议使用,可能会有多个相同类型的变量

3、参数名

<bean id = "user" class = "com.promefire.injection.User">
       <constructor-arg name="name" value="promefire"/>
   </bean>

配置文件加载的时候,容器中管理的对象已经被初始化

不能忘记写有参构造函数,否则会报错

posted @ 2020-10-11 22:38  Promefire  阅读(84)  评论(0)    收藏  举报