ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot框架期末考试大纲解说

2021-11-27 23:00:26  阅读:295  来源: 互联网

标签:springboot Spring bean 期末考试 注入 注解 解说 public name


题型

  • 选择题
  • 程序阅读题
  • 程序改错题
  • 程序填空题
  • 编程题

掌握常用坐标dependency的含义,如web,热部署等

<dependencies>
	<dependency>
        <groupId>项目组标识</groupId>
        <artifactId>项目标识</artifactId>
        <version>项目版本号</version>
        <scope>作用域</scope>
    </dependency>
</dependencies>

spring boot

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.M3</version>
</parent>

spring-boot-starter-web

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

热部署

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <version>2.3.12.RELEASE</version>
    <optional>true</optional>
</dependency>

所有讲授过的注解及其含义和用法;

启动器注解

@SpringBootApplication

这个注解是Spring Boot最核心的注解,用在 Spring Boot的主类上,标识这是一个 Spring Boot 应用,用来开启 Spring Boot 的各项能力。实际上这个注解是@Configuration,@EnableAutoConfiguration,@ComponentScan三个注解的组合。由于这些注解一般都是一起使用,所以Spring Boot提供了一个统一的注解@SpringBootApplication。
    
@ComponentScan

组件扫描。让spring Boot扫描到Configuration类并把它加入到程序上下文。
@ComponentScan注解默认就会装配标识了@Controller,@Service,@Repository,@Component注解的类到spring容器中。

组件注册注解

@Component :标准一个普通的spring Bean类。 
@Repository:标注一个DAO组件类。 
@Service:标注一个业务逻辑组件类。 
@Controller:标注一个控制器组件类。 

java显式装配注解

@Configuration

用于定义配置类,指出该类是 Bean 配置的信息源,相当于传统的xml配置文件,一般加在主类上。如果有些第三方库需要用到xml文件,建议仍然通过@Configuration类作为项目的配置主类——可以使用@ImportResource注解加载xml配置文件。

@Bean

相当于XML中的,放在方法的上面,而不是类,意思是产生一个bean,并交给spring管理。

例子

@Configuration  //代表这是一个配置类
@Import(MyConfig2.class)  //导入合并其他配置类,类似于配置文件中的 inculde 标签
public class MyConfig {
	/**
     *    通过方法注册一个bean,方法名= bean.id;返回值=bean.class
     */
   @Bean //通过方法注册一个bean
   public Dog dog(){
       return new Dog();
  }
}

mvc注解

@RestController

用于标注控制层组件(如struts中的action),表示这是个控制器bean,并且是将函数的返回值直 接填入HTTP响应体中,是REST风格的控制器;它是@Controller和@ResponseBody的合集。
    
@RequestMapping

RequestMapping是一个用来处理请求地址映射的注解;提供路由信息,负责URL到Controller中的具体函数的映射,可用于类或方法上。vaule=""等价于path="",可省略。
    
@ResponseBody

表示该方法的返回结果直接写入HTTP response body中

@RequestBody

是指方法参数被绑定到HTTP请求Body上,前端就不能用表单的方式提交了,需要用json的方式提交。
GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交。
在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。

@RequestParam

用在方法的参数前面
value = "" 前端参数名称
required = false 是否必须
defaultValue = "" 默认值
    
@PathVariable

通过 @PathVariable 可以将URL中占位符参数{xxx}绑定到处理器类的方法形参中@PathVariable("xxx") 

自动注入注解

@AutoWired

byType方式。把配置好的Bean拿来用,完成属性、方法的组装,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。

当加上(required=false)时,就算找不到bean也不报错。

@Qualifier

当有多个同一类型的Bean时,可以用@Qualifier("name")来指定。与@Autowired配合使用

@Resource(name="name",type="type")

没有括号内内容的话,默认byName。与@Autowired干类似的事。

仅了解的注解

@ConfigurationProperties

Spring Boot可使用注解的方式将自定义的properties文件映射到实体bean中,比如config.properties文件。

@Value("${app.name}")
注入简单值
 
@Import

通过导入的方式实现把实例加入springIOC容器中

@EnableAutoConfiguration

让 Spring Boot 根据应用所声明的依赖来对 Spring 框架进行自动配置,一般加在主类上。

@Profiles
    
Spring Profiles提供了一种隔离应用程序配置的方式,并让这些配置只能在特定的环境下生效。
任何@Component或@Configuration都能被@Profile标记,从而限制加载它的时机。

理解和应用IOC的原理和注入方式,AOP的原理和应用;

IoC(反转控制):

​ 一种设计思想,就是将原本在程序中手动创建对象的控制权,交由Spring框架来管理

DI(依赖注入):

​ 依赖注入,在Spring框架负责创建Bean对象时,动态的将依赖对象注入到Bean组件中。

属性注入的三种方式

构造器注入

无参构造
 <bean id="user" class="com.kuang.pojo.User"/>
有参构造
<!-- 第一种根据index参数下标设置 -->
<bean id="userT" class="com.kuang.pojo.UserT">
    <!-- index指构造方法 , 下标从0开始 -->
    <constructor-arg index="0" value="kuangshen2"/>
</bean>
<!-- 第二种根据参数名字设置 -->
<bean id="userT" class="com.kuang.pojo.UserT">
    <!-- name指参数名 -->
    <constructor-arg name="name" value="kuangshen2"/>
</bean>
<!-- 第三种根据参数类型设置 -->
<bean id="userT" class="com.kuang.pojo.UserT">
    <constructor-arg type="java.lang.String" value="kuangshen2"/>
</bean>

​ value:指定基本数据类型或String类型的数据

​ ref:指定其它bean类型的数据

Setter方法注入

配置

  • pojo
 public class Address {
 
     private String address;
 
     public String getAddress() {
         return address;
    }
 
     public void setAddress(String address) {
         this.address = address;
    }
 }
public class Student {
 
     private String name;
     private Address address;
     private String[] books;
     private List<String> hobbys;
     private Map<String,String> card;
     private Set<String> games;
     private String wife;
     private Properties info;
 
     public void setName(String name) {
         this.name = name;
    }
 
     public void setAddress(Address address) {
         this.address = address;
    }
 
     public void setBooks(String[] books) {
         this.books = books;
    }
 
     public void setHobbys(List<String> hobbys) {
         this.hobbys = hobbys;
    }
 
     public void setCard(Map<String, String> card) {
         this.card = card;
    }
 
     public void setGames(Set<String> games) {
         this.games = games;
    }
 
     public void setWife(String wife) {
         this.wife = wife;
    }
 
     public void setInfo(Properties info) {
         this.info = info;
    }
 
     public void show(){
         System.out.println("name="+ name
                 + ",address="+ address.getAddress()
                 + ",books="
        );
         for (String book:books){
             System.out.print("<<"+book+">>\t");
        }
         System.out.println("\n爱好:"+hobbys);
 
         System.out.println("card:"+card);
 
         System.out.println("games:"+games);
 
         System.out.println("wife:"+wife);
 
         System.out.println("info:"+info);
 
    }
 }
注入常量
 <bean id="student" class="com.kuang.pojo.Student">
     <property name="name" value="小明"/>
 </bean>
注入Bean(基础)

​   这里的值是一个引用,ref 

 <bean id="addr" class="com.kuang.pojo.Address">
     <property name="address" value="重庆"/>
 </bean>
 
 <bean id="student" class="com.kuang.pojo.Student">
     <property name="name" value="小明"/>
     <property name="address" ref="addr"/>
 </bean>
注入数组
 <bean id="student" class="com.kuang.pojo.Student">
     <property name="name" value="小明"/>
     <property name="address" ref="addr"/>
     <property name="books">
         <array>
             <value>西游记</value>
             <value>红楼梦</value>
             <value>水浒传</value>
         </array>
     </property>
 </bean>
注入List
<property name="hobbys">
     <list>
         <value>听歌</value>
         <value>看电影</value>
         <value>爬山</value>
     </list>
 </property>
注入Map
<property name="card">
     <map>
         <entry key="中国邮政" value="456456456465456"/>
         <entry key="建设" value="1456682255511"/>
     </map>
 </property>
注入Set
 <property name="games">
     <set>
         <value>LOL</value>
         <value>BOB</value>
         <value>COC</value>
     </set>
 </property>
注入Null
<property name="wife"><null/></property>
注入Properties
 <property name="info">
     <props>
         <prop key="学号">20190604</prop>
         <prop key="性别">男</prop>
         <prop key="姓名">小明</prop>
     </props>
 </property>

P命名和C命名注入

P命名空间(类Set注入,必须有Set方法)

<bean id="john-modern" class="com.example.Person" p:name="John Doe" p:spouse-ref="jane"/>
<!--等价于-->
<bean id="john-classic" class="com.example.Person">
    <property name="name" value="John Doe"/>
    <property name="spouse" ref="jane"/>
</bean>

C命名空间(类构造器注入,必须有有参构造)

<bean id="beanOne" class="x.y.ThingOne" 
        c:thingTwo-ref="beanTwo"  
        c:thingThree-ref="beanThree" 
        c:email="something@somewhere.com"/>
<!--推荐-->
<!--等价于-->
<bean id="beanOne" class="x.y.ThingOne">
        <constructor-arg name="thingTwo" ref="beanTwo"/>
        <constructor-arg name="thingThree" ref="beanThree"/>
        <constructor-arg name="email" value="something@somewhere.com"/>
</bean>
<bean id="beanOne" class="x.y.ThingOne" 
    c:_0-ref="beanTwo" 
    c:_1-ref="beanThree"
    c:_2="something@somewhere.com"/>
    <!--等价于-->
<bean id="beanOne" class="x.y.TingOne">
    <constructor-arg index="0" ref="beanTwo"/>
    <constructor-arg index="1" ref="beanThree"/>
    <constructor-arg index="2" value="something@somewhere.com"/>
</bean>

注解注入

@Component("user")
// 相当于配置文件中 <bean id="user" class="当前注解的类"/>
public class User {
    @Value("秦疆")
    // 相当于配置文件中 <property name="name" value="秦疆"/>
    public String name;
}
@Component("user")
public class User {
 
    public String name;
 
    @Value("秦疆")
    public void setName(String name) {
        this.name = name;
    }
}

AOP

面向切面的编程:把程序重复的代码抽取出来,在需要执行的时候,使用动态代理技术在不修改源代码的基础上对已有方法进行增强。

AOP作用:在程序运行期间,不修改源码对已有方法进行增强。

AOP优势:减少重复代码,提高开发效率,维护方便。

通知:安全,事务,日志等。分为前置通知(Before)、后置通知(AfterReturning)、异常通知(AfterThrowing)、最终通知(After)和环绕通知(Around)五种。

连接点(JoinPoint)是Spring允许你使用通知的地方,基本每个方法的前,后(两者都有也行),或抛出异常时都可以是连接点,Spring只支持方法连接点

切入点(Pointcut)上面说的连接点的基础上,来定义切入点

切面(Aspect)切面是通知和切入点的结合。

目标(target)表示目标对象(被代理对象)。被一个或者多个切面所通知的对象。

代理(proxy)表示代理对象。将通知应用到目标对象之后被动态创建的对象。可以简单地理解为,代理对象为目标对象的业务逻辑功能加上被切入的切面所形成的对象。

weaving 表示切入,也称为织入。将切面应用到目标对象从而创建一个新的代理对象的过程

spring.xml配置aop

		<!-- 配置service -->
    <bean id="customerService" class=“nuc.edu.cn.service.impl.CustomerServiceImpl”></bean>
         <!-- 基于xml的aop配置步骤,必须导入aop的jar包 -->

         <!-- 第一步:把通知类交给spring来管理 -->
    <bean id="customerAspect" class="nuc.edu.cn.advice.CustomerAspect"></bean>
 
         <!-- 第二步:导入aop名称空间 ,并且使用aop:config开始aop配置 -->
    <aop:config>
         <!-- 第三步:使用aop:aspect配置切面 ,id属性用于给切面提供一个唯一标识,ref用于引用通知bean的id -->
    	<aop:aspect id="customerAdvice" ref="customerAspect">
         <!-- 第四步: 配置通知的类型,指定增强的方法何时执行。method:用于指定增强的方法名称 -->
		<!-- 切入点表达式:关键字:execution(表达式). 表达式写法: 访问修饰符 返回值 包名.包名...类名.方法名(参数列表)pointcut="execution(* *..*.*(..))" -->
    		<aop:before method="before" pointcut="execution(public void nuc.edu.cn.service.impl.CustomerServiceImpl.saveCustomer())" />
         </aop:aspect>
	</aop:config>

掌握MVC应用方法,能够使用自动注入实现类托管;

xml自动注入

可以为bean节点添加增加属性 autowire="byName" 或 autowire="byType" ,会自动查找spring容器中的对应的bean注入,而省去了手动注入的麻烦。

注解自动注入

使用注解

@Autowired

  • 默认是根据类型自动装配。多匹配,按照byName方式进行装配。
  • @Qualifier则可以修改byName的值

@Resource

  • 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
  • 如果指定了name,则从Spring上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
  • 如果指定了type,则从Spring上下文中找到类型匹配的唯一bean进行装配,找不到或找到多个,都抛出异常
  • 如果既没指定name,也没指定type,则自动按照byName方式进行装配。如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配。

掌握mybatis的常用配置项,能够使用全局和映射文件方式,以及注解方式使用mybatis;

掌握前端基本控件及表单的使用方法,能够做到前后端交互;

标签:springboot,Spring,bean,期末考试,注入,注解,解说,public,name
来源: https://www.cnblogs.com/faetbwac/p/15613813.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有