【Spring框架】编写第一个入门程序

一. 概述:

1). Spring:

  Spring框架是由Rod Johnson组织和开发的一个分层的Java SE/EE full-stack(一站式)轻量级开源框架,它以IoC(Inversion of Control,控制反转)AOP(Aspect Oriented Programming,面向切面编程)为内核,使用基本的JavaBean来完成以前只可能由EJB(Enterprise Java Beans)完成的工作,取代了EJB的臃肿,低效的开发模式。

2). Spring框架的体系结构

Spring框架采用分层架构包括20个模块,这些模块大体分为Core Container、Data Access/Integration、Web、AOP、Aspects、Instrumentation、Messaging和Test

 对应Spring帮助文档:Spring Framework 5 中文文档(Spring 5 Reference)

GroupId ArtifactId Description(描述)
org.springframework spring-aop Proxy-based AOP support
org.springframework spring-aspects AspectJ based aspects
org.springframework spring-beans Beans support, including Groovy
org.springframework spring-context Application context runtime, including scheduling and remoting abstractions
org.springframework spring-context-support Support classes for integrating common third-party libraries into a Spring application context
org.springframework spring-core Core utilities, used by many other Spring modules
org.springframework spring-expression Spring Expression Language (SpEL)
org.springframework spring-instrument Instrumentation agent for JVM bootstrapping
org.springframework spring-instrument-tomcat Instrumentation agent for Tomcat
org.springframework spring-jdbc JDBC support package, including DataSource setup and JDBC access support
org.springframework spring-jms JMS support package, including helper classes to send and receive JMS messages
org.springframework spring-messaging Support for messaging architectures and protocols
org.springframework spring-orm Object/Relational Mapping, including JPA and Hibernate support
org.springframework spring-oxm Object/XML Mapping
org.springframework spring-test Support for unit testing and integration testing Spring components
org.springframework spring-tx Transaction infrastructure, including DAO support and JCA integration
org.springframework spring-web Web support packages, including client and web remoting
org.springframework spring-webmvc REST Web Services and model-view-controller implementation for web applications
org.springframework spring-websocket WebSocket and SockJS implementations, including STOMP support

二.  Core Container(核心容器)

Spring框架的核心容器是其他模块建立的基础,它主要由Bean模块Core模块Context模块Context-support模块SpEL(Spring Expression Language)模块组成,具体介绍:

(1)Bean模块:

  提供了BeanFactory,是工厂模式的经典实现。Spring将管理对象称为Bean。

(2)Core核心模块:

  提供了Spring框架的基本组成部分,包括IoCDI功能。

(3)Context模块:

  建立在Core和Bean的模块基础之上。它是访问定义和配置任何对象的媒介,其中ApplicationContext接口是上下文模块的焦点。

(4)Context-support模块:

  提供了对第三方库嵌入Spring应用的集成支持,如缓存(EhCache、Guava、JChache)、邮件服务(CommonJ、Quartz)和模板引擎(FreeMarker、JasperReports、速率)。

(5)SpEL模块:

  它是Spring3.0版本后新增的模块,提供了Spring Expression Language支持,是运行时查询和操作对象图的强大表达式语言。

三.  Spring框架的入门程序

1). 创建一个新项目

  在pom.xml文件中加入依赖,在https://mvnrepository.com/中寻找版本

 

 然后我们配置applicationContext.xml文件,我们可以在IDEA给指定名字的文件加入指定内容

<?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">

</beans>

2). 创建文件目录

3).  编写程序

User.java 

package test;

public class User {
    private int id;
    private String userName;
    private String passWord;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", passWord='" + passWord + '\'' +
                '}';
    }
}

 

UserApp.java

package test;

public class UserApp {
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void printUserInfo(){
        System.out.println(user.toString());
    }
}

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

    <bean id="userbean" class="test.User">
        <property name="id" value="1"/>
        <property name="userName" value="张三"/>
        <property name="passWord" value="232919"/>
    </bean>

    <bean id="UserApp_user" class="test.UserApp">
        <property name="user" ref="userbean"/>
    </bean>

</beans>

 UserTest.java

      在测试代码前,我们了解ApplicationContext的概念,它是BeanFactory的子接口,也被称为应用上下文(翻译过来介绍这个意思),是另一种常用的核心容器。它由org.springframework.ApplicationContext.xml接口定义,不仅包含了BeanFactory的所有功能,还添加了对国际化、资源访问、事件传播等方面的支持。

 1)通过ClassPathXmlApplicationContext创建ApplicationContext实例

 2)创建完后就可以获取其中的Bean的id或name来获取指定的Bean

最后测试的结果就会返回UserApp中的printUserInfo()方法 

猜你喜欢

转载自blog.csdn.net/m0_56233309/article/details/123712780