Spring使用-IoC容器

Spring的使用

1. Spring Core

IoC Container(重点)
AOP(重点)
Events
Resources
i18n
Validation
Data Binding
Type Conversion
SpEL

1.1 IoC Container-负责实例化、配置和组装 bean

容器通过读取配置元数据获取有关要实例化、配置和组装的对象的说明。

IoC容器

Spring IoC 容器使用一种配置元数据形式

Spring 容器中使用其他形式的元数据的信息:

基于注释的配置:Spring 2.5 引入了对基于注释的配置元数据的支持。
基于 Java 的配置:从 Spring 3.0 开始,Spring JavaConfig 项目提供的许多功能成为核心 Spring 框架的一部分。因此,您可以使用 Java 而不是 XML 文件在应用程序类外部定义 bean。

1.1.1 IoC容器配置

<?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="..." class="...">  
        <!-- collaborators and configuration for this bean go here -->
    </bean>
    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>
    <!-- more bean definitions go here -->
</beans>

1.1.2实例化IOC容器

提供给构造函数的位置路径或路径是资源字符串,用于让容器从各种外部资源(如本地文件系统、Java 等)加载配置元数据。

ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

1.1.3创建对象

  1. 使用无参构造创建对象
public class Student {
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
<?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">

        <!--使用Spring来创建对象,在Spring这些类都成为Bean
            类型 变量名 = new 类型
            此处的id等价于变量名,class等价于 new 类型
            中间的property相当于给属性赋值-->
        <bean id="student" class="cn.bleds.pojo.Student">
            <!--创建一个hello对象,属性str的值为Spring-->
            <property name="name" value="ka"/>
            <property name="age" value="18"/> 
        </bean>
    </beans>

<property name="age" value="18"/>
这样注入是使用空参构造方法创建实例,然后使用set注入的方式来给变量赋值(必须要有set方法,是)。

<?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="student" class="cn.bleds.pojo.Student">
            <!--创建一个hello对象,属性str的值为Spring-->
             <constructor-arg name="name" value="amos"/>
             <constructor-arg name="age" value="15"/>
        </bean>
    </beans>

<constructor-arg name="name" value="amos"/>
这样的注入方式是使用有参构造函数创建实例,并赋值。

这样的注入好有其他两个方式
<constructor-arg type="java.lang.String" value="ka"/> -根据参数的类型来注入
<constructor-arg index="0" value="ka"/> -根据参数的索引来注入

1.1.4 依赖注入

<bean id="moreComplexObject" class="example.ComplexObject">
    <!-- results in a setAdminEmails(java.util.Properties) call -->
    <property name="adminEmails">
        <props>
            <prop key="administrator">administrator@example.org</prop>
            <prop key="support">support@example.org</prop>
            <prop key="development">development@example.org</prop>
        </props>
    </property>
    <!-- results in a setSomeList(java.util.List) call -->
    <property name="someList">
        <list>
            <value>a list element followed by a reference</value>
            <ref bean="myDataSource" />
        </list>
    </property>
    <!-- results in a setSomeMap(java.util.Map) call -->
    <property name="someMap">
        <map>
            <entry key="an entry" value="just some string"/>
            <entry key ="a ref" value-ref="myDataSource"/>
        </map>
    </property>
    <!-- results in a setSomeSet(java.util.Set) call -->
    <property name="someSet">
        <set>
            <value>just some string</value>
            <ref bean="myDataSource" />
        </set>
    </property>
</bean>

下面的列表显示了一个示例:null

<bean class="ExampleBean">
    <property name="email">
        <null/>
    </property>
</bean>
posted @ 2021-01-30 23:33  x_amos  阅读(31)  评论(0编辑  收藏  举报