IOC:控制反转,把创建对象过程交给Spring进行管理Aop:面向切面,不修改源代码的情况下进行功能增强

package com.xxxx.lln;
/**
* 注释内容
*
* @author : li.linnan
* @create : 2022/8/15
*/
public class User {
public void sout(){
System.out.println("输出用户信息...");
}
}
配置文件
<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">
<bean id="user" class="com.xxxx.lln.User">bean>
beans>
package com.xxxx.lln.testdemo;
import com.xxxx.lln.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.junit.Test;
public class Test1 {
@Test
public void test(){
//加载spring配置文件,ClassPath表示是类路径,由于是在src下,所以直接写名字就行
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//获取配置创建的对象
//User user = context.getBean("user", User.class);
User user = (User) context.getBean("user");
System.out.println(user);
user.sout();
}
}
xml解析、工厂模式、反射

工厂模式----降低耦合度

IOC过程
(1)BeanFactory: IOC容器基本实现,是Spring内部使用的接口,不提供开发人员使用
(2)ApplicationContext: BeanFactory:接口的自己接口,提供更多更强大的功能,一般由开发人员进行使用。
Bean管理指的是两个操作:1、Spring创建对象;2、Spring注入属性
Bean管理操作有两种方式:
1、基于xml配置文件方式实现
2、基于注解方式实现
DI:依赖注入,就是注入属性
<bean id="user" class="com.xxxx.lln.User">bean>
(1)在 spring 配置文件中,使用 bean 标签,标签里面添加对应属性,就可以实现对象创建
(2)在 bean 标签有很多属性,介绍常用的属性 id 属性:唯一标识 class 属性:类全路径(包类路径)
(3)创建对象时候,默认也是执行无参数构造方法完成对象创建
(1)创建类、定义属性和对应的set方法
(2)在spring配置文件中配置对象创建,配置对象注入
<!--set方法注入属性-->
<bean id="book" class="com.xxxx.lln.Book">
<property name="name" value="圆圈正义"></property>
<property name="author" value="罗翔"></property>
</bean>
测试
@Test
public void test2(){
//加载spring配置文件,ClassPath表示是类路径,由于是在src下,所以直接写名字就行
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//获取配置创建的对象
Book book = context.getBean("book", Book.class);
System.out.println(book);
System.out.println(book.getName()+"---"+book.getAuthor());
}
(1)创建类、定义属性、创建属性对应有有参构造方法
//有参构造
public Book(String name, String author) {
this.name = name;
this.author = author;
}
(2)在spring配置文件中进行配置
<!--有参构造-->
<bean id="book" class="com.xxxx.lln.Book">
<constructor-arg name="name" value="一千零一夜"></constructor-arg>
<constructor-arg name="author" value="张良"></constructor-arg>
</bean>
使用p名称空间注入,可以简化基于xml配置方式
(1)添加p名称空间在配置文件中
xmlns:p="http://www.springframework.org/schema/p"
(2)进行属性注入,在bean标签里面进行操作
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--使用p名称空间注入-->
<bean id="book" class="com.xxxx.lln.Book" p:name="三国演义" p:author="罗贯中"></bean>
</beans>
(1)null值
<bean id="book" class="com.xxxx.lln.Book">
<property name="name">
<null/>
</property>
</bean>
(2)属性值包含特殊符号
<!-- 属性值包含特殊符号
1 把 < > 进行转义 < >
2 把带特殊符号内容写到 CDATA -->
<bean id="book" class="com.xxxx.lln.Book">
<!--<property name="name" value="<南京>"></property>-->
<property name="name">
<value><![CDATA[<<南京>>]]></value>
</property>
</bean>
- 创建两个类service类和dao类
- 在service调用dao里面的方法
- 在spring配置文件中进行配置
public class UserService {
// 创建 UserDao 类型属性,生成 set 方法
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add() {
System. out .println("service add...............");
userDao.update();
}
}
<!-- 1 service 和 dao 对象创建 -->
<bean id="userService" class="com.atguigu.spring5.service.UserService">
<!-- 注入 userDao 对象
name 属性:类里面属性名称
ref 属性:创建 userDao 对象 bean 标签 id 值
-->
<property name="userDao" ref="userDaoImpl"></property>
</bean>
<bean id="userDaoImpl" class="com.atguigu.spring5.dao.UserDaoImpl"></bean>
(1)一对多关系:部门和员工 一个部门有多个员工,一个员工属于一个部门 部门是一,员工是多
(2)在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示
// 部门类
public class Dept {
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
}
// 员工类
public class Emp {
private String ename;
private String gender;
// 员工属于某一个部门,使用对象形式表示
private Dept dept;
public void setDept(Dept dept) {
this.dept = dept;
}
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
}
(3)在spring配置文件中进行配置
<!-- 内部 bean -->
<bean id="emp" class="com.atguigu.spring5.bean.Emp">
<!-- 设置两个普通属性 -->
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<!-- 设置对象类型属性 -->
<property name="dept">
<bean id="dept" class="com.atguigu.spring5.bean.Dept">
<property name="dname" value="安保部"></property>
</bean>
</property>
</bean>
(1)第一种写法
<!-- 级联赋值 -->
<bean id="emp" class="com.atguigu.spring5.bean.Emp">
<!-- 设置两个普通属性 -->
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<!-- 级联赋值 -->
<property name="dept" ref="dept"></property>
</bean>
<bean id="dept" class="com.atguigu.spring5.bean.Dept">
<property name="dname" value="财务部"></property>
</bean>
(2)第二种写法

<!-- 级联赋值 -->
<bean id="emp" class="com.atguigu.spring5.bean.Emp">
<!-- 设置两个普通属性 -->
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<!-- 级联赋值 -->
<property name="dept" ref="dept"></property>
<property name="dept.dname" value="技术部"></property>
</bean>
<bean id="dept" class="com.atguigu.spring5.bean.Dept">
<property name="dname" value="财务部"></property>
</bean>
1、注入数组类型属性
2、注入List集合类型属性
3、注入Map集合类型属性
(1)创建类、定义数组、list、map、set类型属性,生成对应set方法
package com.xxxx.lln;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 注释内容
*
* @author : li.linnan
* @create : 2022/8/16
*/
public class Stu {
//1 数组类型属性
private String[] courses;
//2 list 集合类型属性
private List<String> list;
//3 map 集合类型属性
private Map<String,String> maps;
//4 set 集合类型属性
private Set<String> sets;
public void setSets(Set<String> sets) {
this.sets = sets;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
@Override
public String toString() {
return "Stu{" +
"courses=" + Arrays.toString(courses) +
", list=" + list +
", maps=" + maps +
", sets=" + sets +
'}';
}
}
(2)在spring配置文件进行配置
<bean id="stu" class="com.xxxx.lln.Stu">
<property name="courses">
<array>
<value>java 课程value>
<value>数据库课程value>
array>
property>
<property name="list">
<list>
<value>张三value>
<value>小三value>
list>
property>
<property name="maps">
<map>
<entry key="JAVA" value="java">entry>
<entry key="PHP" value="php">entry>
map>
property>
<property name="sets">
<set>
<value>MySQLvalue>
<value>Redisvalue>
set>
property>
bean>
(3)测试
@Test
public void test3(){
//加载spring配置文件,ClassPath表示是类路径,由于是在src下,所以直接写名字就行
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//获取配置创建的对象
Stu stu = context.getBean("stu", Stu.class);
System.out.println(stu);
stu.toString();
}
4、在集合里面设置对象类型值
<bean id="course1" class="com.xxxx.lln.Stu">
<property name="cname" value="Spring5 框架">property>
bean>
<bean id="course2" class="com.atguigu.spring5.collectiontype.Course">
<property name="cname" value="MyBatis 框架">property>
bean>
<property name="courseList">
<list>
<ref bean="course1">ref>
<ref bean="course2">ref>
list>
property>
5、把集合注入部分取出来
(1)在spring配置文件中引入名称空间
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www. springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
(2)使用util标签完成list集合注入提取
<util:list id="bookList">
<value>易筋经value>
<value>九阴真经value>
<value>九阳神功value>
util:list>
<bean id="book" class="com.atguigu.spring5.collectiontype.Book">
<property name="list" ref="bookList">property>
bean>
1、Spring 有两种类型 bean,一种普通 bean,另外一种工厂 bean(FactoryBean)
2、普通 bean:在配置文件中定义 bean 类型就是返回类型
3、工厂 bean:在配置文件定义 bean 类型可以和返回类型不一样
第一步 创建类,让这个类作为工厂 bean,实现接口 FactoryBean
第二步 实现接口里面的方法,在实现的方法中定义返回的 bean 类型
package com.xxxx.lln;
import org.springframework.beans.factory.FactoryBean;
/**
* 注释内容
*
* @author : li.linnan
* @create : 2022/8/16
*/
public class MyBean implements FactoryBean<Book> {
//定义返回Bean
@Override
public Book getObject() throws Exception {
Book book = new Book();
book.setName("abc");
return book;
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return FactoryBean.super.isSingleton();
}
}
<bean id="myBean" class="com.xxxx.lln.MyBean">bean>
@Test
public void test3(){
//加载spring配置文件,ClassPath表示是类路径,由于是在src下,所以直接写名字就行
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//获取配置创建的对象
Book book = context.getBean("myBean", Book.class);
System.out.println(book);
}
1、在 Spring 里面,设置创建 bean 实例是单实例还是多实例
2、在 Spring 里面,默认情况下,bean是单实例对象

3、如何设置单实例还是多实例
(1)在 spring 配置文件 bean 标签里面有属性(scope)用于设置单实例还是多实例
(2)scope 属性值 第一个值 默认值,singleton,表示是单实例对象 第二个值 prototype,表示是多实例对象

(3)singleton 和 prototype 区别
第一 singleton 单实例,prototype 多实例
第二 设置 scope 值是 singleton 时候,加载 spring 配置文件时候就会创建单实例对象
设置 scope 值是 prototype 时候,不是在加载 spring 配置文件时候创建 对象,在调用 getBean 方法时候创建多实例对象
1、生命周期
对象从创建到销毁的过程
2、bean生命周期
(1)通过构造器创建bean实例
(2)为bean的属性设置值和对其他bean引用(调用set方法)
(3)调用bean的初始化方法(需要进行配置初始化的方法)
(4)bean可以使用了(对象获取到了)
(5)当容器关闭时,调用bean的销毁方法(需要进行配置销毁的方法)
3、演示生命周期
package com.xxxx.lln;
public class Orders {
// 无参数构造
public Orders() {
System. out .println("第一步 执行无参数构造创建 bean 实例");
}
private String oname;
public void setOname(String oname) {
this.oname = oname;
System. out .println("第二步 调用 set 方法设置属性值");
}
// 创建执行的初始化的方法
public void initMethod() {
System. out .println("第三步 执行初始化的方法");
}
// 创建执行的销毁的方法
public void destroyMethod() {
System. out .println("第五步 执行销毁的方法");
}
}
<bean id="orders" class="com.xxxx.lln.Orders" init-method="initMethod" destroy-method="destroyMethod">
<property name="oname" value="手机">property>
bean>
@Test
public void test4(){
//加载spring配置文件,ClassPath表示是类路径,由于是在src下,所以直接写名字就行
//ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//获取配置创建的对象
Orders orders = context.getBean("orders", Orders.class);
System.out.println("第四步 获取创建bean实例对象");
System.out.println(orders);
//手动让bean实例销毁
context.close();
}

4、、bean 的后置处理器,bean 生命周期有七步
(1)通过构造器创建 bean 实例(无参数构造)
(2)为 bean 的属性设置值和对其他 bean 引用(调用 set 方法)
(3)把 bean 实例传递 bean 后置处理器的方法 postProcessBeforeInitialization
(4)调用 bean 的初始化的方法(需要进行配置初始化的方法)
(5)把 bean 实例传递 bean 后置处理器的方法 postProcessAfterInitialization
(6)bean 可以使用了(对象获取到了)
(7)当容器关闭时候,调用 bean 的销毁的方法(需要进行配置销毁的方法)
package com.xxxx.lln;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPost implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System. out .println("在初始化之前执行的方法");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System. out .println("在初始化之后执行的方法");
return bean;
}
}
<bean id="myBeanPost" class="com.xxxx.lln.MyBeanPost">bean>

1、根据指定装配规则(属性名称或者属性类型),spring自动将匹配的属性值进行注入
2、演示自动装配过程
package com.xxxx.lln.autowire;
public class Dept {
}
package com.xxxx.lln.autowire;
public class Emp {
private Dept dept;
public void setDept(Dept dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Emp{" +
"dept=" + dept +
'}';
}
public void test(){
System.out.println(dept);
}
}
<bean id="emp" class="com.xxxx.lln.autowire.Emp" autowire="byName">
bean>
<bean id="dept" class="com.xxxx.lln.autowire.Dept">bean>
@Test
public void test5(){
//加载spring配置文件,ClassPath表示是类路径,由于是在src下,所以直接写名字就行
//ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//获取配置创建的对象
Emp emp = context.getBean("emp", Emp.class);
System.out.println(emp);
emp.test();
}
<bean id="emp" class="com.xxxx.lln.autowire.Emp" autowire="byType">
bean>
<bean id="dept" class="com.xxxx.lln.autowire.Dept">bean>
1、直接配置数据库信息
(1)配置德鲁伊连接池
(2)引入德鲁伊连接池jar包

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://localhost:3306/userDb">property>
<property name="username" value="root">property>
<property name="password" value="root">property>
bean>
2、引入外部属性文件配置数据库连接池
(1)创建外部属性文件,properties 格式文件,写数据库信息
prop.driverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/userDb
prop.userName=root
prop.password=root
(2)把外部 properties 属性文件引入到 spring 配置文件中
引入 context 名称空间
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
在spring配置文件中使用标签引入外部属性文件
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClass}">property>
<property name="url" value="${prop.url}">property>
<property name="username" value="${prop.userName}">property>
<property name="password" value="${prop.password}">property>
bean>
1、什么是注解
(1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值, 属性名称=属性值…)
(2)使用注解,注解作用在类上面,方法上面,属性上面
(3)使用注解目的:简化 xml 配置
2、Spring 针对 Bean 管理中创建对象提供注解
(1)@Component
(2)@Service
(3)@Controller
(4)@Repository
上面四个注解功能是一样的,都可以用来创建 bean 实例
3、基于注解方式实现对象创建
第一步 引入依赖

第二步 开启组件扫描
<!--开启组件扫描:
1.如果扫描多个包,多个包用逗号隔开
2.扫描包上层目录-->
<context:component-scan base-package="com.xxxx.lln"></context:component-scan>
第三步 创建类,在类上面添加创建对象注解
// 在注解里面 value 属性值可以省略不写,
// 默认值是类名称,首字母小写
//UserService – userService
package com.xxxx.lln.service;
import org.springframework.stereotype.Service;
@Service(value = "userService")
public class UserService {
public void add(){
System.out.println("service add ......");
}
}
测试
@Test
public void testService(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userService",UserService.class);
System.out.println(userService);
userService.add();
}
4、开启组件扫描细节配置