Java Learning note: Spring Framework

Source: Internet
Author: User
Tags aop

1. What is spring?

1.1 Spring is an open source framework

1.2 Spirng for simplifying enterprise application development, using spring allows simple JavaBean to implement functionality previously only available to EJBS

1.3 Spring is an IOC (DI) and an AOP container framework

2.Spring Getting Started example

2.1 Helloworld.java

 package com.oneline.spring.day01;  public class HelloWorld {     private  String name;          public void setName (String name) {        this. name=name;    }           public void Hello () {        System.out.println ("hello:" +name);}    }

2.2 Main.java

 packagecom.oneline.spring.day01;Importorg.springframework.context.ApplicationContext;Importorg.springframework.context.support.ClassPathXmlApplicationContext; public classMain { public Static voidmain (string[]args) {//1. Create a Spring IOC container objectApplicationContext ctx=NewClasspathxmlapplicationcontext ("applicationcontext.xml"); //2. Get an instance of the Bean from the IOC containerHelloWorld helloworld= (HelloWorld) Ctx.getbean ("HelloWorld"); //3. Call the Hello methodHelloworld.hello (); }    }

2.3 Applicationcontext.xml

<Beansxmlns= "http://www.springframework.org/schema/beans"Xmlns:context= "http://www.springframework.org/schema/context"xmlns:p= "http://www.springframework.org/schema/p"XMLNS:AOP= "http://www.springframework.org/schema/aop"Xmlns:tx= "http://www.springframework.org/schema/tx"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-4.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-4.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-4.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-4.0.xsd Http://www.springframework.org/schema/util http://www.springframework.org/schema/util/ Spring-util-4.0.xsd ">    <!--Configuration Bean -    <BeanID= "helloWorld"class= "com.oneline.spring.day01.HelloWorld">        < propertyname= "name"value= "Spring"></ property>    </Bean></Beans>

2.3.1 Class:bean is a full class name that creates a bean in the IOC container in a reflective manner, so a constructor with no arguments is required in the bean

2.3.2 Id: identifies the bean in the container

3.IOC & DI

Basic realization of 3.1 BEANFACTORY:IOC container

3.2 Applicationcontext: provides more advanced features of the class, is the sub-interface of the Beanfactory

3.3 Beanfactory is the Spring framework infrastructure for spring itself, ApplicationContext for developers using the spring framework, and is used directly in almost all applications ApplicationContext Not the underlying beanfactory.

3.4 Regardless of the way the configuration file is used, the same

4.DI Dependency Injection

4.1 Injection by the set method, the most common injection method

<!---    <   name = "name" value  = "Spring"></Property>

4.2 By constructing methods to inject

4.2.1 Car.java

 packagecom.oneline.spring.day01; public classCar {PrivateString name; PrivateInteger price;  publicCar (String name, Integer price) {Super();  this. Name =name;  this. Price =price ; } @Override publicString toString () {return"Car [name=" + name + ", price=" + price + "]"; }}
<!--to configure a Bean's properties by constructing a method -    <BeanID= "car"class= "com.oneline.spring.day01.Car">        <Constructor-argvalue= "Audi"Index= "0" ></Constructor-arg>        <Constructor-argvalue= " the"type= "java.lang.Integer"></Constructor-arg>    </Bean>    <!--If the injected attribute contains special characters, refer to the following notation -    <BeanID= "car"class= "com.oneline.spring.day01.Car">        <Constructor-argIndex= "0" >            <value><! [cdata[<ShangHai>]]></value>        </Constructor-arg>        <Constructor-argtype= "java.lang.Integer">            <value>250</value>        </Constructor-arg>    </Bean>

Ps:constructor-arg property Details of the tag, value: the attribute value of the injected property, index: the location of the injected property, type: the types of the injected property (which can be invoked with the specified constructor using these property values)

4.3 Cross-Reference configuration between classes

 packagecom.oneline.spring.day01; public classperson {PrivateString name; PrivateInteger age; PrivateCar car;  publicString getName () {returnname; }     public voidsetName (String Name) { this. Name =name; }     publicInteger getage () {returnage ; }     public voidsetage (Integer Age) { this. Age =age ; }     publicCar getcar () {returncar; }     public voidSetcar (car Car) { this. Car =car; } @Override publicString toString () {return"person [name=" + name + ", age=" + age + ", car=" + car + "]"; }    }
<BeanID= "person"class= "com.oneline.spring.day01.Person">        < propertyname= "name"value= "Tom"></ property>        < propertyname= "age"value= "+"></ property>        <!--You can use the Property's ref attribute to establish a reference relationship between beans -        < propertyname= "car"ref= "car2"></ property>    </Bean>

Java Learning note: Spring Framework

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.