欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring的ioc容器

程序员文章站 2022-05-23 10:05:18
...

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>com.ujiuye</groupId>
    <artifactId>Spring01</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <properties>
        <!--
            定义全局变量:变量名为project.spring.version
        -->
        <project.spring.version>5.0.0.RELEASE</project.spring.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${project.spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${project.spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${project.spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${project.spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${project.spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${project.spring.version}</version>
        </dependency>

    </dependencies>

    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
    </build>


</project>

applicationContext.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.xsd">

    <!--导入其他的spring配置文件
        resource:从classes路径开始寻找文件
        classpath:表示指定从classes路径开始寻找配置文件
    -->

    <import resource="classpath:com/ujiuye/spring/_01_hello/applicationContext_hello.xml"/>
    <import resource="classpath:com/ujiuye/spring/_02_configuration/applicationContext_configuration.xml"/>

    <bean id="student" class="com.ujiuye.spring._03_test.Student"></bean>

</beans>

_03_test:
Student,java:

package com.ujiuye.spring._03_test;

/**
 * Created by Administrator on 2019/9/2.
 */
public class Student {
    public Student(){
        System.out.println("Student被创建了...");
    }
}

_04_container:
App.java:

package com.ujiuye.spring._04_container;

import com.ujiuye.spring._03_test.Student;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * Created by Administrator on 2019/9/2.
 */
public class App {
    @Test
    public void testApplicationCintext(){
//        在创建容器对象的时候,就会实例化容器管理的对象
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("+====================================");
        Student student = context.getBean(Student.class);
        System.out.println(student);
    }

    @Test
    public void testBeanFactory(){
//        在从容器中获取对象的时候,容器才会实例化被获取的对象
        Resource resource=new ClassPathResource("applicationContext.xml");
        BeanFactory factory=new XmlBeanFactory(resource);
        System.out.println("+====================================");
        Student student = factory.getBean(Student.class);
        System.out.println(student);
    }
}