Spring基础知识及其入门一

Spring简介

  • Spring是一个开源框架
  • Spring为简化企业级应用开发而生。使用Spring可以使简单的JavaBean实现以前只有EJB才能实现的功能
  • Spring的Java EE/SE的一站式框架

Spring的优点

  • 方便解耦,简化开发
  • AOP编程和支持
  • 声明式事务的支持
  • 方便测序的测试
  • 方便集成各种优秀的框架
  • 降低Java EE的使用难度

Spring入门案例

  • IOC:Inverse of Control(反转控制)。指的就是将原本程序中手动创建UserService对象的控制权交给Spring管理。
  • DI:Dependency Injection(依赖注入)。指的就是Spring在创建这个对象的过程中,将这个对象依赖的属性给注入进去。

入门案例代码部分

  1. 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.xky</groupId>
    <artifactId>spring_ioc</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 备用仓库设置 -->
    <repositories>
        <repository>
            <id>aliyun</id>
            <name>aliyun</name>
            <url>https://maven.aliyun.com/repository/public</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

</project>

 

整个项目主要导入的jar如下所示

  • spring-core 4.2.4.RELSASE
  • spring-bean 4.2.4.RELSASE
  • spring-context 4.2.4.RELSASE
  • spring-expression 4.2.4.RELSASE

   2.所定义的UserService类代码如下

public class UserService {
    public void getTitle(){
        System.out.println("欢迎来到Spring的世界!");
    }
}

 

   3.实现UserService的类的代码实现

package com.spring.demotest;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserServiceImpl {

    /**
     * 传统方法实现UserService里面的方法
     */
    @Test
    public void demo1(){
        UserService userService = new UserService();
        userService.getTitle();
    }

    /**
     * 通过Spring IOC的方式调用UserService里面的方法
     */
    @Test
    public void demo2(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService)applicationContext.getBean("userService");
        userService.getTitle();
    }
}

 

      4.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">

    <!-- 配置UserService类 -->
    <bean id="userService" class="com.spring.demo.UserService"/>
</beans>

其中第三步中的demo2方法就是采用的一种Spring IOC的方式实现要实现的类,对要实现的类进行实例化。相比传统方法我们的Spring方式更加简单,更加明确。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值