zoukankan      html  css  js  c++  java
  • spring的IOC和依赖注入

    一、pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.example</groupId>
        <artifactId>A02spring</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <!--https://mvnrepository.com/artifact/org.springframework/spring-context-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.2.8.RELEASE</version>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

    二、spring的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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--
        创建对象的三种方式
            创建具有无参构造函数的对象
            <bean id="student1" class="com.wuxi.benas.Student"></bean>
            由工厂类的方法创建的对象
            <bean id="createBeanFactory" class="com.wuxi.utils.CreateBeanFactory"></bean>
            <bean id="student2" factory-bean="createBeanFactory" factory-method="CreateStudent"></bean>
            由工厂类的静态方法创建对象
            <bean id="student3" class="com.wuxi.utils.CreateBeanFactory" factory-method="staticCreateStudent"></bean>
        -->
    
        <!--
        bean的作用范围
            scope:取值
                singleton:单例
                prototype:多例
                request:作用于web应用的请求范围
                session:作用于web应用的会话范围
                global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session
        <bean id="student4" class="com.wuxi.benas.Student" scope="prototype"></bean>
        -->
    
        <!--
        bean的生命周期
            单例:生命周期与容器一致
            多例:使用时创建,当没有引用时,由垃圾回收器销毁
        -->
    
        <!--
        依赖注入
            构造函数注入
        <bean id="student51" class="com.wuxi.benas.Student">
            <constructor-arg name="id" value="1"></constructor-arg>
            <constructor-arg name="name" value="孟美岐"></constructor-arg>
            <constructor-arg name="age" value="18"></constructor-arg>
            <constructor-arg name="birthday" ref="birthday51"></constructor-arg>
        </bean>
        <bean id="birthday51" class="java.util.Date"></bean>
            set方法注入
        <bean id="student52" class="com.wuxi.benas.Student">
            <property name="id" value="2"></property>
            <property name="name" value="黄婷婷"></property>
            <property name="age" value="19"></property>
            <property name="birthday" ref="birthday52"></property>
        </bean>
        <bean id="birthday52" class="java.util.Date"></bean>
            注入集合数据
        <bean id="usuallyTool" class="com.wuxi.benas.UsuallyTool">
            <property name="array">
                <array>
                    <value>孟美岐</value>
                    <value>黄婷婷</value>
                    <value>鞠婧祎</value>
                </array>
            </property>
            <property name="list">
                <list>
                    <value>孟美岐</value>
                    <value>黄婷婷</value>
                    <value>鞠婧祎</value>
                </list>
            </property>
            <property name="set">
                <set>
                    <value>孟美岐</value>
                    <value>黄婷婷</value>
                    <value>鞠婧祎</value>
                </set>
            </property>
            <property name="map">
                <map>
                    <entry key="姓名" value="孟美岐"></entry>
                    <entry key="年龄">
                        <value>18</value>
                    </entry>
                </map>
            </property>
            <property name="props">
                <props>
                    <prop key="姓名">孟美岐</prop>
                    <prop key="年龄">18</prop>
                </props>
            </property>
        </bean>
        -->
    </beans>

    三、使用

    /*
    1、获取核心容器的三个实现类
        * ClassPathXmlApplicationContext:读取类路径下的配置文件并创建容器
        * FileSystemXmlApplicationContext:读取系统路径下的配置文件并创建容器
        * AnnotationConfigApplicationContext:读取注解配置并创建容器
    2、两个核心容器
        * ApplicationContext:单例对象和多例对象都可以使用
        * BeanFactory:多例对象使用
    */
    public class MySpringTest {
        public static void main(String[] args) {
            ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
    /*
            // 创建对象的三种方式
            Student student1 = (Student) ac.getBean("student1");
            System.out.println(student1);
            Student student2 = (Student) ac.getBean("student2");
            System.out.println(student2);
            Student student3 = (Student) ac.getBean("student3");
            System.out.println(student3);
    
            // bean的作用范围
            Student student41 = (Student) ac.getBean("student4");
            Student student42 = (Student) ac.getBean("student4");
            System.out.println(student41 == student42);// false
    
            // 依赖注入
            //     构造函数注入
            Student student51 = (Student) ac.getBean("student51");
            System.out.println(student51);
            //     set方法注入
            Student student52 = (Student) ac.getBean("student52");
            System.out.println(student52);
            //     注入集合数据
            UsuallyTool usuallyTool = (UsuallyTool) ac.getBean("usuallyTool");
            System.out.println(usuallyTool);
    */
        }
    }

     四、对IOC的理解

      IOC(控制反转)就是将对象创建过程交由spring容器控制,目的是降低耦合。

  • 相关阅读:
    运算符和表达式详解
    超实用的Java web面试题
    80道最新java基础部分面试题(七)
    80道最新java基础部分面试题(六)
    80道最新java基础部分面试题(五)
    12道算法与编程面试题
    javaee和javase的区别
    2019年最新50道java基础部分面试题(四)
    2019年最新50道java基础部分面试题(三)
    2019年最新50道java基础部分面试题(二)
  • 原文地址:https://www.cnblogs.com/linding/p/13652104.html
Copyright © 2011-2022 走看看