Overview of Spring Framework (1)

Spring is a layered JavaSE/EE full-stack (one-stop) lightweight open source framework

  • Layering:
    • The three-layer structure of EE provided by SUN: web layer, business layer, data access layer (persistence layer, integration layer)
    • Struts2 is a web layer based on MVC design pattern framework.
    • Hibernate is a persistent ORM framework.
  • One stop:
    • The Spring Framework has solutions for each of the three layers:
    • web层:Spring MVC.
    • Persistence Layer: JDBC Template
    • Business Layer: Spring's Bean Management

The core of Spring

  • IOC: (Inverse of Control) Inversion of Control
    : The creation of the object is handed over to Spring.
  • AOP: Aspect Oriented Programming is an extension of object-oriented functions. Instead of replacing object-oriented programming, it is used to solve some problems in OO.

IOC: Inversion of Control.


Advantages of Spring

  • Easy decoupling and simplified development
    • Spring is a big factory that can hand over all object creation and dependency maintenance to Spring management
  • AOP programming support
    • Spring provides aspect-oriented programming, which can easily implement functions such as permission interception and running monitoring of programs.
  • Declarative Transaction Support
    • Transaction management can be completed only through configuration without manual programming
  • Convenient program testing
    • Spring supports Junit4, and you can easily test Spring programs through annotations
  • Easy to integrate various excellent frameworks
    • Spring does not exclude various excellent open source frameworks, and it provides direct support for various excellent frameworks (such as: Struts, Hibernate, MyBatis, Quartz, etc.)
  • Reduce the difficulty of using JavaEE API
    • Spring provides encapsulation for some APIs (JDBC, JavaMail, remote calls, etc.) that are very difficult to use in JavaEE development, which greatly reduces the application difficulty of these APIs.

What is the difference between IOC and DI(*****)?

IOC: Inversion of Control: Spring manages the creation of objects.
DI: Dependency injection: In the process of creating objects in Spring, inject the properties of the object into the class

  • relationship between objects in object orientation;
    • Dependency:
      public class A{
      private B b;
      }
    • Inherited: is a
    • polymerization:
      • Gather:
      • combination:

Spring framework loads configuration files

ApplicationContext application context, which loads Spring framework configuration files

  • Load the classpath:
    new ClassPathXmlApplicationContext("applicationContext.xml"); : Load the configuration file below the classpath.
  • Load disk path:
    new FileSystemXmlApplicationContext("applicationContext.xml"); : Load the configuration file under the disk.

What is the difference between BeanFactory and ApplicationContext?

The ApplicationContext class inherits the BeanFactory
. When the BeanFactory uses this class, the getBean() method will load this class. When the
ApplicationContext class loads the configuration file, all classes are created. The
ApplicationContext provides an extension to the BeanFactory

  • internationalization
  • event delivery
  • Bean autowiring
  • Context implementation of various application layers
  • Early development uses BeanFactory.

MyEclipse Configuration XML Prompt

Window--->xml catalog--->add 找到schema的位置 ,将复制的路径 copy指定位置,选择schema location.

Spring introductory program

  • Download the Spring Development Kit

https://repo.spring.io/release/org/springframework/spring/

  • spring-framework-3.2.0.RELEASE-dist.zip ---Spring development package
    • docs : spring framework api and specification
    • libs : jar package developed by spring
    • schema : XML constraint document.
  • spring-framework-3.0.2.RELEASE-dependencies.zip --- Dependency package in Spring development

Start our first spring program

  1. Create a web project and import the corresponding jar package:
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
开发的日志记录的包:
com.springsource.org.apache.commons.logging-1.1.1.jar       --- 用于整合其他的日志的包(类似Hibernate中slf4j)
com.springsource.org.apache.log4j-1.2.15.jar

2. Create a Spring configuration file

在src下创建一个applicationContext.xml
引入XML的约束:
找到xsd-config.html.(*****)引入beans约束:
<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">

3. Configure the class in the configuration

<!--通过bean标签 设置类的信息,id为类取个标识 -->
    <bean id="userService" class="cn.spring.demo1.HelloServiceimpl">
        <!-- 使用<property>标签注入属性 values是普通值 ref是对象-->
        <property name="info" value="依赖注入" />
    </bean>

    完整的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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--通过bean标签 设置类的信息,id为类起个标识 -->
    <bean id="userService" class="cn.spring.demo1.HelloServiceimpl">
        <!-- 使用<property>标签注入属性 values是普通值 ref是对象-->
        <property name="info" value="依赖注入" />
    </bean>
</beans>
  1. HelloService.java
package cn.spring.demo1;

public interface HelloService {
    public void sayHello();
}

5.HelloServiceimpl..java implementation class

package cn.spring.demo1;

/**
 * @author NOP
 *
 */
public class HelloServiceimpl implements HelloService {
    private String info;

    public void setInfo(String info) {
        this.info = info;
    }

    public void sayHello() {
        // TODO Auto-generated method stub
        System.out.print("hello spring..."+info);
    }

}
  1. Write the test class SpringTest1.java
package cn.spring.demo1;

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

public class SpringTest1 {
    @Test
    // 传统方式
    public void demo1() {
        // 造成程序紧密耦合
        HelloService helloservice = new HelloServiceimpl();
        helloservice.sayHello();
    }

        //加载classpath:new ClassPathXmlApplicationContext("applicationContext.xml");
    @Test
    public void demo2() {
        //创建一个工厂类
        //加载classpath:new ClassPathXmlApplicationContext("applicationContext.xml");
        ApplicationContext applicationcontext = new ClassPathXmlApplicationContext(
                "applicationContext.xml");// 如果不写会去web-inf下去找applicationContext.xml

        HelloService helloservice = (HelloService)applicationcontext.getBean("userService");
        helloservice.sayHello();
    }

    //加载磁盘路径:  new FileSystemXmlApplicationContext("applicationContext.xml");
    @Test
    public void demo3( ) {
        //创建一个工厂类
        //加载磁盘路径:  new FileSystemXmlApplicationContext("applicationContext.xml");
        ApplicationContext applicationcontext = new FileSystemXmlApplicationContext(
                "applicationContext.xml");// 如果不写会去web-inf下去找applicationContext.xml

        HelloService helloservice = (HelloService)applicationcontext.getBean("userService");
        helloservice.sayHello();
    }

    //BeanFactory模式
    @Test
    public void demo4(){
        //ClassPathResource
        //BeanFactory beanFactory= new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); :加载classpath下面配置文件.
        BeanFactory beanFactory= new XmlBeanFactory(new FileSystemResource("applicationContext.xml"));//加载磁盘下的配置文件
        HelloService helloservice = (HelloService)beanFactory.getBean("userService");
        helloservice.sayHello();
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324826644&siteId=291194637