秋招实习spring全家桶重新学习

spring是一个轻量级的javaee开源框架。
IOC:控制反转*(依赖注入),不同于以往的new创建对象,将对象的创建交给spring
AOP:面向切面,在不修改源代码的前提下进行功能增强。
1.配置spring与idea
首先cv poem.xml然后maven reload一下加载即可
在这里插入图片描述
出现这个标志就成功了

创建xml

    public static void main(String[] args) {
        xml一定要放在resources下面
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        xml任意位置
        FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("Y:\\sb\\spring-learning\\src\\bean1.xml");
        //User.class 是告Bean的类别不然得到的是object,name对应xml id
        User user = context.getBean("user", User.class);
        user.add();
    }

在这里插入图片描述
IOC:解耦,将所有的对象调用应用创建等都交于spring管理。
技术:xml解析,工厂模式,反射
在这里插入图片描述add改路径改名字sever都得跟着改耦合度太高。
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
di是ioc的具体实现,注入属性

属性注入,本质是初始化时调用对应的set

<bean id="user" class="User">
    <constructor-arg name="name3" value="n3"></constructor-arg>
    <property name="name" value="n1"></property>
    <property name="name2" value="n2"></property>
</bean>

在这里插入图片描述
注入对象用ref
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述
在这里插入图片描述

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                     http://www.springframework.org/schema/context
                     http://www.springframework.org/schema/context/spring-context-3.0.xsd
                     http://www.springframework.org/schema/aop
                     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                     http://www.springframework.org/schema/tx
                     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
       default-lazy-init="true" default-autowire="byName">
    
<!--    开启注解扫描告诉spring哪些地方有注解-->
    <context:component-scan base-package="com.anno">
    </context:component-scan>

</beans>

在这里插入图片描述重点是根据类型
在这里插入图片描述
防止出现多个实现通过id可以确定

在这里插入图片描述@value是对普通类别的注入,value可以注入值

完全注解开发,设置一个config类

@Configurable
@ComponentScan(basePackages = {"com"})
public class myConfig {

}


        AnnotationConfigApplicationContext context = new 
        AnnotationConfigApplicationContext(myConfig.class);

在这里插入图片描述在这里插入图片描述
aop使用记录
1.开启aop扫描

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
或者
@EnableAspectJAutoProxy
2.增强代码编写
@Component
@Aspect
@Order(0)//多个方法增强优先级,数字越小优先级越大
public class MyProxy {
    @Before("execution(* com.anno.AnnoDao.aop(..))")
    public void before(){
        System.out.println("before");
    }
}

在这里插入图片描述
开启事务
在这里插入图片描述在这里插入图片描述在这里插入图片描述

maven:jar包管理工具
设置:
1.设置settings.xml
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述并且在idea里添加maven路径和settings.xml

@Responsebody:返回字符串而非页面跳转
@Restcontroller = Responsebody+@Controller
@controller代表页面跳转
自动扫描主程序所在包结果下的全部,不用显示配置scan
springboot一个相当于下面三个
在这里插入图片描述
在这里插入图片描述@config+bean等同于xml配置bean,注意@bean是在方法上组件名默认是方法名
获取组件
在这里插入图片描述
引入xml
在这里插入图片描述

配置文件绑定
在这里插入图片描述在这里插入图片描述

@Data lombok 自动生成getset构造器
在这里插入图片描述在这里插入图片描述
即可访问
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

因为表单只能写get和post,所以具体实现需要_method来实现,本质是添加了参数告诉springboot需要转换。这一步骤需要在yml里开启。可以通过一步步分析源码发现开启这个有个false所以需要开启。springboot大量的阅读配置文件加载来作为变量也就是用户与框架的沟通。

只有post才有请求体。
springboot底层就是map存储数据,然后把map存在list里针对各种类型注解等进行遍历匹配处理。(匹配参数解析器是否支持处理该参数)
自动装配本质是springboot创立了大量的约定,处理时便到特定的位置寻找处理。并与对应的特定配置文件建立了联系便于用户修改。
return map或者别的类型springboot都会遍历确定类型后进行对应的处理,唯一需要注意的就是返回类型
在这里插入图片描述
springboot做了大量的预配置,比如数据源是OnMissingBean时自动配置hikari
在这里插入图片描述在这里插入图片描述自己注入Bean后便会使用自己的,password等会到yml里关联查询。能用set的方法都可以在yml里写
引入starter的mean依赖相当于#include

controller调service,service调mapper。xml里的id对应方法名。沟通方式都是命名的统一规范,
由于mysql采样下划线分割,开启驼峰命名可以自动封装。
在这里插入图片描述
@profile(“name”)即可指定不同环境(生产测试)下的配置文件,注意命名规范,那么全称是application-{name}.properties(yml)
可以标注在类或者方法上
批量加载
在这里插入图片描述classpath:java+resources

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值