[Spring Framework] Write the first entry program

I. Overview:

1). Spring:

  The Spring framework is a layered Java SE/EE full-stack (one-stop) lightweight open source framework organized and developed by Rod Johnson. It is based on IoC (Inversion of Control, inversion of control) and AOP (Aspect Oriented Programming ). , Aspect-oriented programming) as the core, using basic JavaBean to complete the work that could only be done by EJB (Enterprise Java Beans) before, replacing the bloated and inefficient development mode of EJB.

 

2). Architecture of Spring Framework

The Spring framework adopts a layered architecture and includes 20 modules, which are roughly divided into Core Container, Data Access/Integration, Web, AOP, Aspects, Instrumentation, Messaging and Test .

 Corresponding Spring Help Document: Spring Framework 5 Chinese Documentation (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-worm 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

2. Core Container

The core container of the Spring framework is the foundation for other modules. It is mainly composed of Bean module , Core module , Context module , Context-support module and SpEL (Spring Expression Language) module .

(1) Bean module:

  Provides BeanFactory , which is a classic implementation of the factory pattern. Spring refers to managed objects as beans.

(2) Core core module:

 Provides the basic components of the Spring framework, including IoC and DI capabilities.

(3) Context module:

  Based on the modules of Core and Bean. It is the medium for accessing the definition and configuration of any object, where the ApplicationContext interface is the focal point of the context module.

(4) Context-support module:

  提供了对第三方库嵌入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

The result of the final test will be returned to the printUserInfo() method  in UserApp

Guess you like

Origin blog.csdn.net/m0_56233309/article/details/123712780