开始需要建立一个工程
导入需要的的包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
创建applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="studentEntity" class="com.v1.entity.StudentEntity">
<property name="id" value="112"/>
<property name="name" value="战一柔"/>
</bean>
</beans>
1.xml形式一个bean是如何加载到spring容器中的
package com.v1.test;
import com.v1.entity.StudentEntity;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class V1TestSpring {
private static ClassPathXmlApplicationContext applicationContext;
public static void main(String[] args) {
// 读取加载xml文件 生成bean
applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentEntity studentEntity = applicationContext.getBean("studentEntity",StudentEntity.class);
System.out.println(studentEntity.toString());
}
/**
* spring中的bean id 重复会怎么样 A spring中的bean Id 是不能重复的
* A 报错 B 覆盖
*
*
*/
}
运行
通过xml配置的信息来把实例注入spring容器中
以注解的形式注入实例到spring容器中
1.通过java文件配置bean的实例化信息
@Configuration
@ComponentScan(value = "com.v2")
public class MySpringConfig {
/**
* 等同于xml文件中的
* <bean id="studentEntity" class="com.v2.entity.StudentEntity">
* <property name="id" value="122"/>
* <property name="name" value="江楚辞"/>
* </bean>
*
*/
@Bean
public StudentEntity studentEntity(){
return new StudentEntity("江楚辞",122);
}
}
@ComponentScan 指定扫描地址
@Configuration 注解是用来标注类为配置类,等同于xml文件 在启动的时候 spring的AnnotationConfigApplicationContext扫描到
作用是:初始化spring容器,注入bean
public class V2TestSpring {
private static AnnotationConfigApplicationContext annotationConfigApplicationContext;
public static void main(String[] args) {
//注解方式加载spring 通过使用 Configuration Bean 注解 不依赖xml文件 把studentEntity的实例注入到sprinig容器中
annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MySpringConfig.class);
StudentService studentService = annotationConfigApplicationContext.getBean("studentService",StudentService.class);
//如何获取当前spring容器中注入了那些对象实例
String[] beanDefinitionNames = annotationConfigApplicationContext.getBeanDefinitionNames();
for(String beanName : beanDefinitionNames){
System.out.println("spring容器内对象:"+beanName);
}
System.out.println(studentService.toString());
/**
* 为什么springboot在启动的时候 需要假如
*/
}
}
在spring中默认是启动的时候加载bean还是调用的时候才会加载bean -- 在实例对象的构造方法中打印一段信息
public StudentEntity(String name, Integer id) {
System.out.println("====>构造StudentEntity实例化");
this.name = name;
this.id = id;
}
在默认的情况下在启动的时候就把对象信息加载到spring容器,
加入注解@Lazy
@Lazy 注解在默认的配置
参数为true 的时候是在获取对象的时候才会创建对象实例
查看false运行情况
看以上运行的结果
默认情况不加入@Lazy注解 | 在spring启动时加载实例对象 |
加入@Lazy注解(默认为true) | 在获取实例对象的时候注入到spring容器中 |
@Lazy(value=false) | 在spring启动时加载实例对象 |
k'l
题目。
1.Configuration注解的作用
指定类为配置类,会被启动类 AnnotationConfigApplicationContext扫描的 主要用来 初始化bean 加载bean
2.@Lazy注解的作用
控制spring启动的加载bean的顺序
3.AnnotationConfigApplicationContext的作用
4.springIOC 和 springAOP 是什么 作用是什么
springIOC 是一种思想 :对项目中的实例进行统一管理 ,控制了对象创建的模式(单例,原型。。。。)
降低模块与模块之间的耦合度
SpringAOP 面向切面编程 AOP主要的原理:动态代理
代理的原是基于反射
jdk动态代理 :通过反射生成代理类,在mybatis中的mapper接口映射到sql语句就是jdk动态代理来实现的
cglib动态代理: 是一种基于 ASM字节码技术为目标类生成代理类
5.spring中的bean id重复了会报错?
会
6.spring的作用域有哪些
mybatis 题目
1.mybatis一级缓存存在那些问题
2.如何禁止一级缓存
3.一级缓存和二级缓存的区别
4.mybatis二级缓存底层是如何实现的