Spring学习

添加坐标

<packaging>jarpackaging>

    <dependencies>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>5.0.2.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.13version>
            <scope>testscope>
        dependency>
    dependencies>

配置文件代码


<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">
    
    <bean id="serviceTest" class="com.spring.helloService">bean>
beans>

测试类

package test;

import com.spring.helloService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @program: firstSpring
 * @description: 测试类
 * @author: Mr.Tan
 * @create: 2020-08-04 21:11
 **/
public class helloServiceTest {
    @Test
    public void test1() throws Exception {
        /**
         * 1.加载Spring的配置文件
         * 2.取出Bean容器中的实例
         * 3.调用bean方法
         */
        // 1.加载Spring的配置文件
        // ApplicationContext 在构建核心容器时,采用立即加载的方式
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        // 2.取出Bean容器中的实例
        helloService helloService = (helloService) context.getBean("serviceTest");
        // 3.调用bean方法
        helloService.hello();
    }
}

Spring学习_第1张图片

你可能感兴趣的:(JavaEE,spring)