提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
Spring学习笔记
前言
最近学习了Spring的相关知识,由于刚开始学,所以很多依赖注解记不住,下面记录了相关的知识点,以及用法。
一、常用依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!--spring操作数据库时导入-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!--junit依赖的包,使用@Test时需导入-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- aop织入时依赖的包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>`
二、常用配置约束
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
<!--spring文件中引入context文件头,上面也要有对应-->
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
<!--spring文件中引入aop文件头,上面也要有对应-->
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--指定要扫描的包,这个包下面的注解就会生效-->
<context:component-scan base-package="com.xx.xx"/>
<!--注解驱动-->
<context:annotation-config/>
<!--aop的注解驱动-->
<aop:aspectj-autoproxy/>
</beans>
三、常用注解及说明
@Autowired:自动装配,先类型后名字,名字通过@Qualifier(value=":")进行匹配
@Nullable:允许空值
@Resource:自动装配,先名字后类型
@Component:组件,放在类上,说明这个类被Spring管理了,xml里面无需再写bean,例如:@component(“user”),下面是User类的定义,则等价于<bean id=“user class=”""/>
@Configure:代表这是一个配置类
@Bean:通过方法注册一个bean,返回的类型就是就是Bean的类型,方法名就是Bean的id,获取bean用的类是AnnotationConfigApplicationContext(MyConfig.class),其中参数值与之前使用的xml不同,是反射。
@Import:在一个配置类中导入另一个配置类,例如:@Import(MyConfig2.class)
@ComponentScan:用来扫描包,例如:@ComponentScan(“com.zhoumin.pojo”)
在aop中常用的注解
@Aspect:声明这个类是一个切面,替代了xml中的声明
@Before:这个方法是前置执行
@After:这个方法是后置执行
@Around:这个方法是环绕执行
三、总结
浅显的学习了Spring,主要对于动态代理的那一块不太了解,因为涉及到反射机制,所以不是很清楚,等我啥时候弄明白了,再进行补充吧!上面的注解中要是存在错误请指出,谢谢!