Spring Study Notes 01 and spring Study Notes 01

Source: Internet
Author: User

Spring Study Notes 01 and spring Study Notes 01
1. Spring Introduction

Spring is a lightweight solution for enterprises, including: dependency injection-based core mechanism, declarative Transaction Management Based on AOP,

A collection of persistence layer technologies and excellent web mvc frameworks.

Composition of the Spring framework:

 

2. Spring preparation

1) first download the Spring compressed file from the official website and decompress it.

2) import the jar package to the eclipse project.

3. Use of Spring

Spring core container is a super large factory, and all objects are treated as Spring core container management objects, called beans.

First, define two classes: Pen and Person:

Public class Pen {public String write () {return "write with Pen ";}}
Public class Person {private Pen pen; public void setPen (Pen pen) {this. pen = pen;} public void usePen () {System. out. println ("I want to write"); System. out. println (pen. write ());}}

The write () method of Pen is used in the userPen () method in Person, that is, Person depends on Pen. Sring is used to process dependencies between these Bean objects and create objects.

Therefore, create the applicationContext. xml file under the src directory and configure the related bean:

 

<?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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"      xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">                <bean id="person" class="com.huan.example.Person">        <property name="pen" ref="pen"></property>    </bean>    <bean id="pen" class="com.huan.example.Pen"></bean></beans>

Spring calls the non-argument construction of the Class Based on the bean reflection mechanism in the configuration file to create an object, and puts the id as the key value into the Sring container, which is called the Bean in Spring.

The <property> sub-element in Person, Spring will execute the setter method in Person using reflection, that is, the setPen () method, and inject the Bean pointed to by ref as a parameter

In Person.

Create a new class for testing:

Public class TestPerson {public static void main (String [] args) {// load the configuration file and create the Spring container ApplicationContext ac = new ClassPathXmlApplicationContext ("applicationContext. xml "); // obtain the Bean object based on the id, without the need for new Person p = ac. getBean ("person", Person. class); // call the object Method p. usePen ();}}

The main implementation classes of the ApplicationContext interface are ClassPathXmlApplicationContext and FileSystemXmlApplicationContext. The former is

Search for the configuration file in the class loading path. The latter searches for the configuration file in the relative or absolute path of the file system.

Test result:

We can see from the above that the new call constructor is no longer used to create objects after the Spring framework is used. All java objects are created by the Spring container.

Related Article

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.