Spring学习笔记
作者:shizhishi |
发布时间: |
分类:后端架构 |
声明:个人技术博客 • 非官方 • 仅供技术学习交流
* Spring官方网址:spring.io * 导入Jar包 * IoC:控制反转 :对象不是由自己创建,而是由Spring容器来创建 * 创建对象方式—XML方式applicationContext.xml—配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xml...
### 入门Spring
* Spring官方网址:spring.io
* 导入Jar包
* IoC:控制反转 :对象不是由自己创建,而是由Spring容器来创建
* 创建对象方式—XML方式applicationContext.xml—配置文件
<?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">
<!--配置对象
- name 别名,可以用“,”和“ ”隔开
- lazy-init="true" 懒加载
- scope="prototype" prototype 多例(默认是单例) 表示该 Bean 的作用域为多例。每次请求该 Bean 时,都会创建一个新的实例
-->
<bean id="user" class="pojo.User" name="dud,sos kok" lazy-init="true" scope="prototype"/>
<alias name="user" alias="person,people kin" /><!-- 别名( 可以配置多个别名 ) -->
</beans>
* 两种方式来创建IoC对象public class MyTest { @Test public void test(){ // 加载配置文件 创建IOC容器(相对路径) ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml", "applicationContext.xml"); // 获取配置创建的对象 User user = (User) context.getBean("user"); System.out.println(user.toString()); } @Test public void test2(){ // 加载配置文件 创建IOC容器(绝对路径) ApplicationContext context2 = new FileSystemXmlApplicationContext("D:\\JavaWeb\\2024_Spring\\spring\\hellospring\\src\\main\\
esources\\application.xml"); User user2 = context2.getBean("user",User.class); System.out.println(user2.toString()); }}
### DI依赖注入
* 依赖什么?注入什么?
依赖注入指在对象创建时设置初始值
* 依赖:bean对象创建依赖于容器
* 注入:指的是bean对象由容器来设置和装配(初始化)
* 第一种:构造器注入
* 第二种:Setter方式注入
--> 时间 空间 时间 空间 时间 空间
* 第三种—扩展注入标签内使用属性名前缀p:来定义依赖。 - 主要用于setter注入,不需要额外的标签。 -->
* 自动装配Autowire
* 注解/** * Autowired: 自动装配,通过类型匹配,如果匹配到多个,则通过属性名匹配 * required: 是否必须,默认为true,如果为false,则表示这个属性不是必须的,如果匹配不到,则不报错,直接为null * Qualifier: 指定bean的id,如果Autowired匹配不到,则通过Qualifier匹配 * */ @Autowired(required = false) @Qualifier("cat1") private Cat cat1; private Cat cat; /** * Resource: 自动装配 通过名称匹配,如果匹配到多个,则通过属性名匹配,是jdk自带的注解 * */ @Resource(name = "dog") private Dog dog;
### Java Annotation
自定义注解(包含三个不同的作用域)
/**
* @author shizhishi
*/ //定义一个注解
@Target(ElementType.TYPE)//类上
@interface Myannotation {
//注解的属性
String name();
}
/**
* @author shizhishi
*/
@Target(ElementType.METHOD)//方法上
@interface Myannotation1 {
//注解的属性
String name();
}
/**
* @author shizhishi
*/
@Target(ElementType.FIELD)//属性上
@interface Myannotation2 {
//注解的属性
String name();
}
@Myannotation(name = "zhangsan")
public class TextAnnotation {
@Myannotation2(name = "wangwu")
private String name;
@Myannotation1(name = "lisi")
void test() {
}
}
通过反射获取注解上的值
// 通过反射获取注解信息
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
// Class c = Class.forName("StudentDao");
//或
Class c = StudentDao.class;
Annotation[] annotations = c.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
//result: @Select("111111")
}
//获取类上注解的Value值
Select select = (Select) c.getAnnotation(Select.class);
System.out.println(select.value());
//result: 111111
//获取方法上的注解
Method methods = c.getDeclaredMethod("findAll");
Select select1 = methods.getAnnotation(Select.class);
System.out.println(select1.value());
}
### Spring Annotation
* 第一步:扫描包
* 基本的注解@Component 把普通pojo实例化到spring容器中,相当于配置文件中的@Controller 控制层(web),效果等同于@Component@Service 业务层,效果等同于@Component@Respository 持久层,效果等同于@Component
### 无配置文件开发:基于Java类
* 主要是对比applicationContext.xml配置文件/*** @author shizhishi* Configuration注解表示当前类是一个配置类,相当于spring的配置文件*/@Configuration@ComponentScans({ @ComponentScan(value = {"service","pojo"})})// 扫描包,相当于之前写的public class MyConfig { //最基本的配置,注册一个bean,就相当于我们之前写的一个bean标签// @Bean// public Student getStudent(){// return new Student();// }
* 不同类型的bean@Test public void test() { ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class); // 基本类型的bean// Student student = context.getBean("getStudent", Student.class); // 引用类型的bean Student student = context.getBean("student", Student.class); System.out.println(student.name); }
### AOP面向切面编程
* 切面和切点
切面:是通知和切点的集合,定义了
* 切点:缩小了切面的内容,可以指定在匹配的方法上执行哪些通知
* aop java自带接口
MethodBeforeAdvice :前置通知
* AfterReturningAdvice :后置通知
* DynamicIntroductionAdvice :环绕通知
* ThrowsAdvice :异常通知
* 导入Jar包 org.aspectj aspectjweaver 1.9.7
* 配置切面(启用jdk动态代理) - proxy-target-class 默认为false,表示使用jdk动态代理,设置为true表示使用cglib动态代理。 - 默认情况下,Spring AOP 是基于动态代理实现的,当目标对象实现了接口,Spring AOP 会使用 JDK 动态代理;当目标对象没有实现接口,Spring AOP 会使用 CGLIB 动态代理。 -->
* 配置切面(指定代理目标类)@Componentpublic class DiyPointCut { public void before(){ System.out.println("before"); } public void after(){ System.out.println("after"); } public void around(){ System.out.println("around"); }}
* 使用注解开发/*** @author shizhishi*/@Component@Aspectpublic class AnnotationPointCut { @Before("execution(* aop2.StudentServiceImpl.*(..))") public void before(){ System.out.println("AnnotationPointCut.before"); } @After("execution(* aop2.StudentServiceImpl.*(..))") public void after(){ System.out.println("AnnotationPointCut.after"); } @Around("execution(* aop2.StudentServiceImpl.*(..))") public Object around(ProceedingJoinPoint pjp){ System.out.println("AnnotationPointCut.around"); return null; }}