Overview of the Spring Framework (1)

Source: Internet
Author: User
Tags aop

Spring is a layered javase/ee full-stack (one-stop) lightweight open-source framework
    • Layered:
      • The three-tier architecture of the EE provided by Sun: Web tier, business layer, data access layer (persistence layer, integration layer)
      • STRUTS2 is the Web layer based on the MVC design pattern framework.
      • Hibernate is a long-lasting ORM framework.
    • One-stop:
      • The spring framework has a solution for each layer on layer three:
      • Web tier: Spring MVC.
      • Persistence layer: JDBC Template
      • Business layer: Spring's bean management
The core of Spring
    • IOC: (Inverse of control inversion controls)
      Control reversal: The creation of the object is done by spring.
    • Aop:aspect oriented programming is an object-oriented feature extension. Instead of replacing object-oriented, it is used to solve some problems in OO.

IOC: Control inversion.

Spring Benefits
    • Easy decoupling for simplified development
      • Spring is a large factory that can maintain all object creation and dependency relationships and give spring management
    • Support for AOP programming
      • Spring provides aspect-oriented programming, which can easily implement the functions of permission interception, operation monitoring, etc.
    • Support for declarative Transactions
      • The management of transactions can be done only through configuration, without the need for manual programming
    • Easy testing of programs
      • Spring support for JUNIT4, which can be easily tested with annotations for spring programs
    • Easy integration of a variety of excellent frameworks
      • Spring does not exclude a variety of excellent open source frameworks that provide direct support for a variety of excellent frameworks (e.g. Struts, Hibernate, MyBatis, quartz, etc.)
    • Reduce the difficulty of using Java EE APIs
      • Spring provides encapsulation for some of the most difficult APIs (JDBC, JavaMail, remote calls, and so on) in Java EE Development, making these API applications much less difficult to apply
IOC and DI (* * * * *) difference?

IOC: Control reversal: The creation of objects is managed by spring.
DI: Dependency Injection: In the process of creating objects in spring, the properties that are dependent on the object are injected into the class

    • The relationship between objects in object oriented;
      • Depend on:
        public class a{
        Private b b;
        }
      • Inheritance: is a
      • Polymerization:
        • Gathered:
        • Combination:
Spring Framework Load configuration file

ApplicationContext application context, loading Spring Framework configuration file

    • Load classpath:
      New Classpathxmlapplicationcontext ("Applicationcontext.xml"); : Load classpath configuration file below.
    • To load a disk path:
      New Filesystemxmlapplicationcontext ("Applicationcontext.xml"); : Load the configuration file under disk.
What is the difference between beanfactory and ApplicationContext?

The ApplicationContext class inherits the Beanfactory
Beanfactory when using this class, the Getbean () method loads the class.
When the ApplicationContext class loads the configuration file, all the classes are created
ApplicationContext provides an extension to Beanfactory

    • International Treatment of
    • Event delivery
    • Bean Automatic Assembly
    • Context implementation for a variety of different application tiers
    • Early development uses beanfactory.
MyEclipse Configuring XML Hints
Window--->xml catalog--->add 找到schema的位置 ,将复制的路径 copy指定位置,选择schema location.
Spring's Getting Started program
    • Download the Spring development package

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

    • Spring-framework-3.2.0.release-dist.zip---Spring development package
      • Docs:spring Framework APIs and specifications
      • libs:spring Development of JAR packages
      • Schema:xml the constraint document.
    • Spring-framework-3.0.2.release-dependencies.zip---A dependency package in spring development
Start our first spring program.
    1. Create a Web project to introduce the appropriate jar package:
spring-beans-3.2.0.RELEASE.jarspring-context-3.2.0.RELEASE.jarspring-core-3.2.0.RELEASE.jarspring-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 Classes

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. Writing 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//traditional public void demo1 () {//Cause program tightly coupled helloservice HelloService = new Hello        Serviceimpl ();    Helloservice.sayhello ();    }//Load Classpath:new classpathxmlapplicationcontext ("Applicationcontext.xml"); @Test public void Demo2 () {//Create a factory class//Load Classpath:new classpathxmlapplicationcontext ("Applicationcontex        T.xml "); ApplicationContext ApplicationContext = new Classpathxmlapplicationcontext ("Applicationcontext.xml");//If not write will go to web-inf down to find applicationcontext.xml helloservice HelloService = (helloservice) applicationcontext.getbean ("UserS        Ervice ");    Helloservice.sayhello ();    }//Load disk path: New Filesystemxmlapplicationcontext ("Applicationcontext.xml"); @Test public void Demo3 () {//Create a factory class//Load Disk path: New Filesystemxmlapplicationcontext ("ApplicationContext        . xml "); ApplicationContext ApplicationContext = new Filesystemxmlapplicationcontext ("Applicationcontext.xml");//AS If not write will go to web-inf down to find applicationcontext.xml helloservice HelloService = (helloservice) applicationcontext.getbean ("UserSe        Rvice ");    Helloservice.sayhello (); }//beanfactory mode @Test public void Demo4 () {//classpathresource//beanfactory beanfactory= new Xml Beanfactory (New Classpathresource ("Applicationcontext.xml"));        : Load classpath configuration file below. Beanfactory beanfactory= New Xmlbeanfactory (New Filesystemresource ("Applicationcontext.xml"));//load configuration file under Disk HelLoservice HelloService = (helloservice) beanfactory.getbean ("UserService");    Helloservice.sayhello (); }}

Overview of the Spring Framework (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.