Spring
IoC容器–存放对象
控制反转(IoC)+依赖注入
【配置IoC】
编写第一个IoC
【自己随便写个类】
【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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- <bean id="my-first-object" class="com.lhk.ioc.Person"></bean>-->
<!-- 把 com.lhk.ioc.Person 类交给 Spring 了
由 Spring 帮我们生产 Person 对象出来(Person 来调用我们的构造方法 new 对象)
由于咱没写任何构造方法,默认就去调用无参构造方法
并且将生产好的对象,以 'my-first-object' 为名进行售卖 -->
<bean id="my-first-object" class="com.lhk.ioc.Person">
<constructor-arg value="1998" /> <!--使用有参构造方法-->
</bean>
</beans>
【自己编写一个类加入Spring中】
【在主方法中使用Spring提供的方法调用】
public class Main {
public static void main(String[] args) {
// 定义一个 Spring 容器(IoC 容器)
// 实际上,由于 Spring 支持 XML、注解等方法,实现类挺多
// 文件系统 XML 应用上下文
// FileSystemXmlApplicationContext 是 ApplicationContext 接口的实现类
// ApplicationContext 代表就是刚才说的 IoC 容器的抽象
// 所以,通过读取 D:\课程\2022-08-21-2022火箭班-Spring\ioc\src\main\resources\spring.xml 中的内容,来创建 IoC 容器
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("H:\\Java\\work\\ioc_test\\src\\main\\resources\\Spring.xml");
Object bean = context.getBean("my-first-object");
System.out.println(bean.getClass().getCanonicalName());//获取真实得对象类型
System.out.println(bean.toString());
}
}
产生依赖关系
以构造方法注入
【ref = “spring中依赖的对象‘】
【value = 】–是对有参构造的参数赋值
【main方法调用】
public class Main {
public static void main(String[] args) {
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("H:\\Java\\work\\ioc_test\\src\\main\\resources\\Spring.xml");
System.out.println(context.getBean("zoo"));
}
}
以set方法注入
【XML】
【name是monkey,会去monkey属性(property)的设置。按照Java的规范,调用monkey-hill的setter方法。所以调用setMonkey,使用ref=“monkey1” 】
<bean id="monkey1" class="com.lhk.ioc.Monkey">
<constructor-arg value="1234"/>
</bean>
<bean id="monkey-hill" class="com.lhk.ioc.MonkeyHill">
<property name="monkey1" ref="monkey1"/>
</bean>
以静态工厂方法售卖对象
工厂方法–专门创建对象
<bean id="city-name" class="com.lhk.ioc.CityFactory" factory-method="create"/>
【使用Spring调用CityFactory中的create方法,会得到一个City对象,名字是city-name】
【运行结果】
通过实例工厂方法构造对象
<bean id="my-first-object" class="com.lhk.ioc.Person"/>
<bean id="monkey1" class="com.lhk.ioc.Monkey">
<constructor-arg value="1234"/>
</bean>
<bean id="zoo" class="com.lhk.ioc.Zoo" >
<constructor-arg ref="monkey1"/>
</bean>
<bean id="monkey-hill" class="com.lhk.ioc.MonkeyHill">
<property name="monkey" ref="monkey1"/>
</bean>
<bean id="city-name" class="com.lhk.ioc.CityFactory" factory-method="create"/>
<bean id="factory" class="com.lhk.ioc.UniverseFactory">
<constructor-arg ref="my-first-object" />
<constructor-arg ref="city-name"/>
<constructor-arg ref="monkey-hill"/>
<constructor-arg ref="monkey1"/>
<constructor-arg ref="zoo"/>
<!--要一一对其-->
</bean>
<bean id="universe" factory-bean="factory" factory-method="createUniverse"/>
AOP支持
SpringMVC
【一般来说将 过程对象 交给Spring管理,Spring管理的对象默认是单例】
注解
类的注册
【注册时,它们没有区别】
实例工厂的注册
Bean的使用
Bean修饰工厂方法
配置文件(configuration)的使用
【影响软件执行逻辑的策略数据,称为配置文件】
【IDEA-Spring配置】
#关闭<Spring>打印
spring.main.banner-mode = off
#关闭日志打印
spring.main.log-startup-info = off
#字符化图片
spring.banner.image.location = classpath:img.png
代码中读取配置文件
【yml】
日志的使用
package com.lhk.ssm_test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SsmTestApplication {
private final static Logger log= LoggerFactory.getLogger(SsmTestApplication.class);
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SsmTestApplication.class, args);
log.info("我是一个日志!");
}
}
日志的级别
spring:
main:
banner-mode: off
log-startup-info: false
logging:
file:
name:E:\QQ下载文件\my-first.log #日志存放地址
level:
# 让 com.peixinchen.log.demo.DemoApplication 下的日志输出界别变成 debug
com.lhk.log.demo.DemoApplication: error
# 也可以通过包名,指定这个包下所有的类的日志输出级别
com.peixinchen: debug
# 影响所有类
root: debug
【grep console插件 日志打印更好看点】
LomBok使用
【目的:减少代码的编写】
Spring数据库访问(Mybatis)
对象代理
Mybatis原理
【构建项目】
【原理仍是对象代理】
方法的返回值类型
使用XML方式
【对象名与数据库表中列名不同,需要 结果映射】
【注解可以做,很麻烦,所以采取XML】
Spring AOP原理
AOP:面向切面编程
OOP:面向对象编程
如何实现切入?
【编织】
【使用别人使用AOP创建好的场景,比如数据库中的事务】
【两条sql通过引入显示事务,告诉MySQL这是一个事务】
@transactional 将一个方法变成事务