JavaEE学习之Spring框架(第一篇: 快速入门)

Spring IOC快速入门

1.下载Spring开发包

官方下载地址:https://repo.spring.io/release/org/springframework/spring/
压缩包下载完成后解压,目录结构如下:
在这里插入图片描述

2.导入Spring核心开发jar包到工程

  1. commons-logging
  2. spring-beans
  3. spring-context
  4. spring-core
  5. spring-expression
  6. log4j
pom.xml:
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.12</version>
    </dependency>
  </dependencies>

3.编写Spring核心配置文件

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管理 -->
    <bean id="userService" class="com.ciaotigre.spring.UserServiceImpl">
        <!-- 属性注入 -->
        <property name="name" value="ciaotigre"></property>
    </bean>
</beans>

4.程序中读取Spring配置文件,通过Spring框架获得Bean,完成相关操作

package com.ciaotigre.spring;

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.annotation.Bean;
//import org.springframework.context.support.ClassPathXmlApplicationContext;
//import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

public class SpringDemo {
    @Test
    /**
     * 传统方式
     */
    public void demo1(){
        UserService userService = new UserServiceImpl();
        userService.sayHello();
    }

    @Test
    /**
     * SpringIOC方式
     */
    public void demo2(){
        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
        UserService userService = (UserService)beanFactory.getBean("userService");
        userService.sayHello();
    }
}
发布了21 篇原创文章 · 获赞 0 · 访问量 539

猜你喜欢

转载自blog.csdn.net/CiaoTigre/article/details/104343415