美文网首页
JAVA之Spring Bean

JAVA之Spring Bean

作者: 每天进步一点点变成更好的自己 | 来源:发表于2022-06-22 12:29 被阅读0次

1、Spring Ioc 控制反转【大工厂】

  • IoC 控制反转
    Spring IoC:Inversion of Control ,控制反转。通过IoC容器来管理所有java对象的实例化和初始化,控制对象与对象之间的依赖关系,即java Bean,类似于使用关键new创建对象。
    老流程:一个类调用其他类的属性或方法,通过new object()的方式将后者的对象创建出来,然后开始调用属性或方法。
    Spring中,JAVA对象创建的控制权掌握在IoC容器中,步骤:
    1、开发人员通过XML配置文件、注解、JAVA配置类等方式,对java对象进行定义,比如在XML配置文件中使用<bean>标签、在JAVA类中使用@component注解等。
    2、Spring启动,IoC容器会自动根据对象定义进行创建和管理,即Spring Bean。
    3、当我们使用某个Bean时,可以直接从IoC容器中获取,而不需要手动通过代码创建。
  • 依赖注入(DI)
    依赖注入:DI,Dependency Injection。在面向对象中,对象和对象的关系是依赖的关系。
  • IoC容器的两种实现
    IoC容器的底层就是一个Bean工厂。Spring框架提供了两种不同类型的IoC容器,即BeanFactory和ApplicationContext。

2、Spring Bean定义【产品】

由Spring IoC容器管理的对象成为Bean,根据Spring配置文件中的信息创建。
Spring配置文件支持两种格式:XML文件格式和Properties文件格式。

  • Properties
    配置文件主要是以key-value键值对的形式存在,只能赋值。
  • XML
    配置文件采用树形结构,结构清晰。

Beans.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-3.0.xsd">

    <bean id="helloWorld" class="net.biancheng.c.HelloWorld">
        <property name="message" value="Hello World!"/>
    </bean>
</beans>

3、Spring Bean属性注入

非常详细且写的很好的文章:http://c.biancheng.net/spring/attr-injection.html
Bean属性注入,就是将属性注入到Bean中的过程,既可以是普通属性,也可以是一个对象(Bean).
Spring主要通过2种方式进行Bean属性注入:

  • 构造函数注入
  • setter注入(设值注入)

Spring注入集合:

image.png

1、构造函数注入
使用构造函数注入,大致步骤如下:
1、在Bean中添加一个有参构造函数,构造函数内的每一个参数代表一个需要注入的属性
2、在Spring的XML配置文件中,通过<beans>及其展示<bean>对Bean进行定义
3、在<bean>元素内使用<constructor-arg>元素,对构造函数内的属性进行赋值,bean的构造函数内有多少参数,就需要使用多少个<constructor-arg>元素。

2、setter注入
使用setter方法,将属性值注入到Bean的属性中,大致步骤如下:
1、在Bean中提供一个默认的无参构造函数(在没有其他带参构造函数的情况下,可省略),并为所有需要注入的属性提供一个setXxx()方法
2、在Spring的XML配置文件中,使用<bean>及其子元素<bean>对Bean进行定义
3、在<bean>元素内使用<property>元素对各个属性进行赋值

#1、构造函数注入
<?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-3.0.xsd">

    <bean id="student" class="net.biancheng.c.Student">
        <constructor-arg name="id" value="2"></constructor-arg>
        <constructor-arg name="name" value="李四"></constructor-arg>
        <constructor-arg name="grade" ref="grade"></constructor-arg>
    </bean>

    <bean id="grade" class="net.biancheng.c.Grade">
        <constructor-arg name="gradeId" value="4"></constructor-arg>
        <constructor-arg name="gradeName" value="四年级"></constructor-arg>
    </bean>
</beans>

#2、setter注入(设值注入)
<?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-3.0.xsd">
    <bean id="student" class="net.biancheng.c.Student">
        <!--使用 property 元素完成属性注入
            name: 类中的属性名称,例如 id,name
            value: 向属性注入的值 例如 学生的 id 为 1,name 为张三
        -->
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="grade" ref="grade"></property>
    </bean>

    <bean id="grade" class="net.biancheng.c.Grade">
        <property name="gradeId" value="3"></property>
        <property name="gradeName" value="三年级"></property>
    </bean>
</beans>


#3、内部bean
<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-3.0.xsd">

    <bean id="employee" class="net.biancheng.c.Employee">
        <property name="empNo" value="001"></property>
        <property name="empName" value="小王"></property>
        <property name="dept">
            <!--内部 Bean-->
            <bean class="net.biancheng.c.Dept">
                <property name="deptNo" value="004"></property>
                <property name="deptName" value="技术部"></property>
            </bean>
        </property>
    </bean>

</beans>

相关文章

网友评论

      本文标题:JAVA之Spring Bean

      本文链接:https://www.haomeiwen.com/subject/nidukrtx.html