什么是Bean管理
Bean管理指的是俩个操作
1)Spring创建对象
2)Spring注入属性
Bean管理操作有俩种方式
1)基于xml配置文件方式实现
2)基于注解方式实现
基于xml方式创建对象
<bean id="user" class="com.spring.User"></bean>
1)在Spring配置文件中,使用bean标签,标签里面添加对应属性,就可以实现对象创建。
2)在bean标签有很多属性,介绍常用的属性
*id属性:唯一标识
*class属性:类全路径
3)创建对象时候,默认也是执行无参构造方法完成对象创建
基于xml方式注入属性
1)DI:依赖注入,就是注入属性
? *第一种注入方式:使用set方法就行注入
? ①创建类,定义属性和对应的set方法
public class Book(){
    private String name;
    private String author;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    @Override
    public String toString() {
        return "Book{" +
                "name=‘" + name + ‘\‘‘ +
                ", author=‘" + author + ‘\‘‘ +
                ‘}‘;
    }
}
? ②在Spring配置文件配置对象创建,配置属性注入
<bean id="book" class="com.spring.Book">
	<property name = "name" value="zhangsan"/>
	<property name = "author" value="itcoder"/>
</bean>
? *第二种注入方式:使用有参构造进行注入
? ①创建类:定义属性,创建属性对应有参构造方法
public class Orders {
    private String order;
    private String address;
    public Orders(String order, String address) {
        this.order = order;
        this.address = address;
    }
    @Override
    public String toString() {
        return "Orders{" +
                "order=‘" + order + ‘\‘‘ +
                ", address=‘" + address + ‘\‘‘ +
                ‘}‘;
    }
}
? ②在Spring配置文件中进行配置
<bean id="orders" class="com.spring.springboot.mapper.Orders">
    <constructor-arg name="order" value="computer"></constructor-arg>
    <constructor-arg name="address" value="China"></constructor-arg>
</bean>
? ③测试:
@SpringBootTest
class ApplicationTests {
    @Test
    public void OrdersTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        Orders orders = context.getBean("orders", Orders.class);
        System.out.println(orders);
        orders.toString();
    }
}

p名称空间注入
1)使用p名称空间注入,可以简化基于xml配置方式
? *第一步:添加p名称空间在配置文件中
<?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">
? *第二步:进行属性注入,在bean‘标签里面进行操作
<bean id="orders" class="com.spring.springboot.mapper.Orders" p:order="food" p:address="China">
</bean>
字面量
1)null值
<property name="address">
	<null/>
</property>
2)属性值包含特殊符号
<!--属性值包含特殊符号:
	1.把<>进行转义
	2.把带特殊符号内容写到CDATA
-->
<property name="address">
	<value><![CDATA[<<南京>>]]></value>
</property>
注入属性-外部bean
1)创建两个类service和dao
public interface UserDao {
    void update();
}
2)在service调用dao里面的方法
public class UserService {
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public void add(){
        System.out.println("add...");
        userDao.update();
    }
}
3)在spring配置文件中进行配置
<bean id="userDao" class="com.spring.springboot.dao.impl.UserDaoImpl"></bean>
<bean id="userService" class="com.spring.springboot.service.UserService">
    <property name="userDao" ref="userDao"></property>
</bean>
注入属性-内部bean和级联赋值
1)一对多关系:部门和员工
一个部门有多个员工,一个员工属于一个部门
2)在实体类之间表示一对多关系
public class Dept {
    private String dname;
    public void setDname(String dname) {
        this.dname = dname;
    }
    @Override
    public String toString() {
        return "Dept{" +
                "dname=‘" + dname + ‘\‘‘ +
                ‘}‘;
    }
}
package com.spring.springboot.bean;
public class Empt {
    private String ename;
    private String gender;
    private Dept dept;
    public void setEname(String ename) {
        this.ename = ename;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public void setDept(Dept dept) {
        this.dept = dept;
    }
    public void add(){
        System.out.println(ename+"::"+gender+"::"+dept);
    }
}
3)在spring配置文件中进行配置
<bean id="empt" class="com.spring.springboot.bean.Empt">
    <property name="ename" value="lucy"></property>
    <property name="gender" value="女"></property>
    <property name="dept">
        <bean id="dept" class="com.spring.springboot.bean.Dept">
            <property name="dname" value="保安部"></property>
        </bean>
    </property>
</bean>
注入属性-级联赋值
1)第一种写法
<bean id="empt" class="com.spring.springboot.bean.Empt">
    <property name="ename" value="lucy"></property>
    <property name="gender" value="女"></property>
    <property name="dept" ref="dept"></property>
</bean>
<bean id="dept" class="com.spring.springboot.bean.Dept">
    <property name="dname" value="保安部"></property>
</bean>
2)第二种写法
? ①生成dept的getter方法
public class Empt {
    private String ename;
    private String gender;
    private Dept dept;
    public Dept getDept() {
        return dept;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public void setDept(Dept dept) {
        this.dept = dept;
    }
    public void add(){
        System.out.println(ename+"::"+gender+"::"+dept);
    }
}
? ②配置文件
<bean id="empt" class="com.spring.springboot.bean.Empt">
    <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.spring.springboot.bean.Dept">
    <property name="dname" value="保安部"></property>
</bean>
注入数组类型属性
注入List集合类型属性
注入Map集合类型属性
1)创建类、定义数组、List、map、set类型属性,生成对应set方法
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Stu {
    private String[] array;
    private List<String> list;
    private Map<String,String> map;
    private Set<String> sets;
    public void setArray(String[] array) {
        this.array = array;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public void setMap(Map<String, String> map) {
        this.map = map;
    }
    public void setSets(Set<String> sets) {
        this.sets = sets;
    }
    public void test(){
        System.out.println(Arrays.toString(array));
        System.out.println(list);
        System.out.println(map);
        System.out.println(sets);
    }
}
2)在spring配置文件中进行配置
<bean id="stu" class="com.spring.spring_boot.controller.Stu">
    <property name="array">
        <array>
            <value>Java</value>
            <value>Python</value>
        </array>
    </property>
    <property name="list">
        <list>
            <value>zhangsan</value>
            <value>fawaikuangtu</value>
        </list>
    </property>
    <property name="map">
        <map>
            <entry value="zhangsan" key="18"></entry>
            <entry value="lisi" key="19"></entry>
        </map>
    </property>
    <property name="sets">
        <set>
            <value>Hello</value>
            <value>World</value>
        </set>
    </property>
</bean>
在集合里面设置对象类型的值
public class Course {
    private String cname;
    public void setCname(String cname) {
        this.cname = cname;
    }
    @Override
    public String toString() {
        return "Course{" +
                "cname=‘" + cname + ‘\‘‘ +
                ‘}‘;
    }
}
private List<Course> courseList;
public void setCourseList(List<Course> courseList) {
    this.courseList = courseList;
}
<bean id="stu" class="com.spring.spring_boot.controller.Stu">
    <property name="array">
        <array>
            <value>Java</value>
            <value>Python</value>
        </array>
    </property>
    <property name="list">
        <list>
            <value>zhangsan</value>
            <value>fawaikuangtu</value>
        </list>
    </property>
    <property name="map">
        <map>
            <entry value="zhangsan" key="18"></entry>
            <entry value="lisi" key="19"></entry>
        </map>
    </property>
    <property name="sets">
        <set>
            <value>Hello</value>
            <value>World</value>
        </set>
    </property>
    <property name="courseList">
        <list>
            <ref bean="courseOne"></ref>
            <ref bean="courseTwo"></ref>
        </list>
    </property>
</bean>
<bean id="courseOne" class="com.spring.spring_boot.controller.Course">
    <property name="cname" value="Math"></property>
</bean>
<bean id="courseTwo" class="com.spring.spring_boot.controller.Course">
    <property name="cname" value="English"></property>
</bean>
把集合注入部分提取出来
1)在spring配置文件中引入名称空间util
<?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: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>Java</value>
    <value>c++</value>
    <value>Python</value>
</util:list>
<bean id="book" class="com.spring.spring_boot.controller.Book">
    <property name="name" ref="bookList"></property>
</bean>
Spring有两种类型bean,一种普通bean,另外一种工厂bean(FactoryBean)
普通bean:在配置文件中定义bean类型就是返回类型。
工厂bean:在配置文件定义bean类型可以和返回类型不一样。
第一步:创建类,让这个类作为工厂bean,实现接口FactoryBean
第二步:实现接口里面的方法,在实现的方法中定义返回的bean类型
import com.spring.spring_boot.controller.Course;
import org.springframework.beans.factory.FactoryBean;
public class MyBean implements FactoryBean<Course> {
    @Override
    public Course getObject() throws Exception {
        Course course = new Course();
        course.setCname("lucy");
        return course;
    }
    @Override
    public Class<?> getObjectType() {
        return null;
    }
    @Override
    public boolean isSingleton() {
        return false;
    }
}
<bean id="mybean" class="com.spring.spring_boot.factorybean.MyBean">
</bean>
@Test
public void MyBeanTest(){
    ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
    Course course = context.getBean("mybean", Course.class);
    course.show();
}
在Spring里面,设置创建bean实例是单实例还是多实例
在Spring里面,默认情况下是单实例对象
如何设置单实例还是多实例
1)在spring配置文件bean标签里面中就有一个属性(scope)用于设置单实例还是多实例
2)scope属性值
第一个值:singleton,表示是单实例对象
<bean id="book" class="com.spring.spring_boot.controller.Book" scope="singleton">
    <property name="name" ref="bookList"></property>
</bean>

第二个值:prototype,表示是多实例对象
<bean id="book" class="com.spring.spring_boot.controller.Book" scope="prototype">
    <property name="name" ref="bookList"></property>
</bean>

3)singleton和prototype区别
第一:singleton单实例,prototype多实例
第二:设置scope值是singleton时候,加载spring配置文件时候就会创建单实例对象
? 设置scope值是prototype时候,不是在加载spring配置文件时候创建对象,在调用getBean方法时候创建多实例对象
生命周期
1)从对象创建到对象销毁的过程
bean生命周期
1)通过构造器创建bean实例(无参构造)
2)为bean的属性设置值和对其他bean引用(调用set方法)
3)调用bean的初始化的方法(需要进行配置)
4)bean可有使用了(对象获取到了)
5)当容器关闭时候,调用bean的销毁方法(需要进行配置销毁的方法)
演示bean生命周期
bean的后置处理器:bean生命周期有七步
1)通过构造器创建bean实例(无参构造)
2)为bean的属性设置值和对其他bean引用(调用set方法)
3)把bean实例传递bean后置处理器的方法
4)调用bean的初始化的方法(需要进行配置)
5)把bean实例传递bean后置处理器的方法
6)bean可有使用了(对象获取到了)
7)当容器关闭时候,调用bean的销毁方法(需要进行配置销毁的方法)
演示添加后置处理器效果
package com.spring.spring_boot.bean;
public class Orders {
    public Orders() {
        System.out.println("第一步执行无参构造创建bean实例");
    }
    private String name;
    public void setName(String name) {
        this.name = name;
        System.out.println("第二步调用set方法设置属性值");
    }
    public void initMethod(){
        System.out.println("第三步执行初始化的方法");
    }
    public void lastMethod(){
        System.out.println("第五步执行销毁方法");
    }
}
package com.spring.spring_boot.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.Nullable;
public class MyBean 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="orders" class="com.spring.spring_boot.bean.Orders" init-method="initMethod" destroy-method="lastMethod">
    <property name="name" value="zhangsan"></property>
</bean>
<!--配置后置处理器-->
<bean id="myBean" class="com.spring.spring_boot.bean.MyBean"></bean>
@Test
public void Test01(){
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
    Orders orders = context.getBean("orders", Orders.class);
    System.out.println("第四步,获取创建bean实例化对象");
    context.close();
}

什么是自动装配
1)根据指定装配规则(属性名称或者属性类型),Spring自动将匹配的属性值注入
演示自动装配过程
autowire属性常用两个值
? byName根据属性名称注入,注入值bean的id值和类属性名称 一样。
? byType根据属性类型注入
1)根据属性名称自动注入
public class Dept {
    private String name;
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Dept{" +
                "name=‘" + name + ‘\‘‘ +
                ‘}‘;
    }
}
package com.spring.spring_boot.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 show(){
        System.out.println(dept);
    }
}
<bean id="emp" class="com.spring.spring_boot.autowire.Emp" autowire="byName"></bean>
<bean id="dept" class="com.spring.spring_boot.autowire.Dept"></bean>
@Test
public void Test01(){
   ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml");
   Emp emp = context.getBean("emp", Emp.class);
   emp.show();
}
2)根据属性类型自动注入
<bean id="emp" class="com.spring.spring_boot.autowire.Emp" autowire="byType"></bean>
<bean id="dept" class="com.spring.spring_boot.autowire.Dept"></bean>
直接配置数据库信息
1)配置连接池
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSourceFactory">
   <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
   <property name="url" value="jdbc:mysql://localhost:3306//test"></property>
   <property name="username" value="root"></property>
   <property name="password" value="root"></property>
</bean>
2)引入连接池依赖
<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.32</version>
</dependency>
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.1.10</version>
</dependency>
引入外部属性文件配置数据库连接池
1)创建外部属性文件:properties格式文件,写数据库信息
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/test
jdbc.username = root
jdbc.password = root
2)把外部properties属性文件引入到spring配置文件中
? *引入context命名空间
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.DruidDataSourceFactory">
   <property name="driverClassName" value="${jdbc.driver}}"></property>
   <property name="url" value="${jdbc.url}}"></property>
   <property name="username" value="root"></property>
   <property name="password" value="root"></property>
</bean>
原文:https://www.cnblogs.com/coderD/p/13828569.html