Spring5笔记

Spring

常用依赖

org.springframework spring-webmvc 5.2.0.RELEASE --

注解

1.@Resource(常用)自动装配,通过类型,名字可通过加name属性精细 寻找
2.@Autowired自动装配,通过名字,类型,如果不能自动装配则需要@Qualifier(value=" ")
3.@component: 组件,放在类上,说明这个类被Spring管理了,就是bean。
等价于xml中的bean,创建一个域
4.@Value 注解给值,相当于property中给对象赋值
5.@Repository dao层 @Service service层 @Controller controller层 注入Spring 类似bean
6.@Scope 作用域 单例模式("singleton") 原型模式("prototype")

实现注解需要导入约束

xmlns:context="http://www.springframework.org/schema/context"

context:annotation-config/

--

实现aop需要导入约束

xmlns:aop="http://www.springframework.org/schema/aop"

--
beans.xml头部


context:annotation-config/

beans.xml注入问题

1.下标赋值、参数类型赋值、直接参数名赋值 三者不能混合使用(参数类型赋值类型不可重复)
2.起别名:,也可直接在bean中设置name属性,可同时取多个别名。
3.作用域:Scope 作用域 单例模式("singleton") 原型模式("prototype")
4.import:将多个beans.xml文件导入同一文件内。

可通过将xml文件联系起来。
无参构造

有参构造


依赖注入:

构造器注入(如上)
set注入:直接参数名赋值 数组array list list map map Set set
拓展注入:
两者需要导入xml约束
c命名:与p命名类似
p命名注入:在bean中直接赋值 p:username="王士贤"
autowire:通过type name 自动装配其他bean
指定要扫描的包,包下的注解生效
<context:component-scan base-package=""/>等价于@component
@component: 组件,放在类上,说明这个类被Spring管理了,就是bean。
等价于xml中的bean,创建一个域
@Value 注解给值,相当于property中给对象赋值
@Repository dao层 @Service service层 @Controller controller层 注入Spring 类似bean
context:annotation-config</context:annotation-config>
使用javaconfig配置 用new AnnotationConfigApplicationContext()链接配置类

img1.png
img2.png
img3.png
img.png
注解Aop
img_1.pngimg_2.png

Aop包导入

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.4</version>
</dependency>

--

Spring-Mybatis整合引入xml

mysql mysql-connector-java 8.0.28 org.mybatis mybatis 3.5.11 org.junit.jupiter junit-jupiter-engine 5.9.3 test log4j log4j 1.2.17 org.projectlombok lombok 1.18.30 provided org.springframework spring-webmvc 5.2.0.RELEASE org.springframework spring-jdbc 5.2.0.RELEASE javax.annotation javax.annotation-api 1.3.2 org.aspectj aspectjweaver 1.9.4 org.mybatis mybatis-spring 2.1.2 --
posted @ 2025-10-07 15:07  我是贤  阅读(4)  评论(0)    收藏  举报