Spring入门—在Maven下配置Spring

1.什么是Spring?

Spring 是一个开源框架,它是为了解决企业应用开发的复杂性而创建的。Spring 使用基本的 JavaBean 来完成以前只可能由 EJB 完成的事情。然而,Spring 的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何 Java 应用都可以从 Spring 中受益。Spring中有两个重要的技术和思想:IOC和AOP

IOC:控制反转(Inversion of Control)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题。 控制反转还有一个名字叫做依赖注入(Dependency Injection)
AOP:为 Aspect Oriented Programming 的缩写,意为:面向切面编程,可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术

2.配置并编写第一个基于Spring的程序

2.1 在pom.xml中配置Spring的依赖:
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <!--spring基本框架核心工具类,其他的spring都有依赖这个-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.8.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <!--创建和管理bean的一个spring组件-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
2.2 编写pojo类

Student.java

package com.xzy.bean;

public class Student {
    private int age;
    private String name;
    private Teacher tea;

   //省略getter setter

Teacher.java

package com.xzy.bean;

public class Teacher {

    private String name;

   //省略getter setter
}

2.3 配置spring配置文件—ApplicatonContext.xml:

注意,这个配置文件在maven工程中一定要放在src/main/resource目录下(也就是一定呀放在classpath路径下),这样在程序运行的时候会自动的去找这个文件,必然会找不到而发生IO异常,具体如下图片所示:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.xzy"></context:component-scan>

    <!--配置bean并且注入依赖DI-->
    <bean id="teacher" class="com.xzy.bean.Teacher">
        <property name="name" value="刘老师"/>
    </bean>
    <bean id="student" class="com.xzy.bean.Student">
        <property name="age" value="23"/>
        <property name="name" value="李四"/>
        <property name="tea" ref="teacher"/>
    </bean>

</beans>
2.4 Dao层:
package com.xzy.dao;

/**
 * @Author: HuangXin
 * @Date: Created in 22:12 2019/7/30
 * @Description:
 */
public interface EmailDao {

    public void sent();

}
2.5 DaoImpl
package com.xzy.dao.Impl;

import com.xzy.dao.EmailDao;
import org.springframework.stereotype.Component;

/**
 * @Author: HuangXin
 * @Date: Created in 22:12 2019/7/30
 * @Description:
 */
@Component
public class EmailDaoImpl implements EmailDao {

    @Override
    public void sent() {
        System.out.println("发送email>>>>>>>>>>>>");
    }
}
2.6 service层:
package com.xzy.service;

import com.xzy.dao.EmailDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: HuangXin
 * @Date: Created in 22:15 2019/7/30
 * @Description:
 */
@Component
public class TestService {

    @Autowired
    private EmailDao ed;

    public void run(){
        ed.sent();
    }

}

3.测试:

package com.xzy;


import com.xzy.dao.EmailDao;
import com.xzy.dao.Impl.EmailDaoImpl;
import com.xzy.service.TestService;
import org.apache.log4j.Logger;
import org.junit.Test;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


/**
 * Unit test for simple App.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value={"/ApplicationContext.xml"})
public class AppTest {


    private static Logger log=Logger.getLogger(AppTest.class);

    //Autowired可以自动注入
    @Autowired
    private TestService ts;

    @Test
    public void test1(){

        //初始化spring容器
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");

        TestService ts=(TestService)context.getBean(TestService.class);
        ts.run();
    }

}

留言区

还能输入500个字符