SSH learning-Spring basics and sshspring Basics

Source: Internet
Author: User

SSH learning-Spring basics and sshspring Basics
1. What is the Spring framework?

Spring is a J2EE application framework and a lightweight container framework for IOC and AOP. It is a lightweight container for managing the lifecycle of javaBean. It can be used independently or with the Struts framework, hibernate framework.

2. Advantages and disadvantages of the Spring framework:

  Advantages:

Lightweight container framework is not invasive

Using IoC containers makes it easier to combine the direct relationship between objects, interface-oriented programming, and reduced coupling.

Aop makes it easier to Expand functions and follows the ocp development principles.

By default, a single instance is used to create an object. You do not need to use the single instance mode for processing.

  Defects:Business functions depend on the unique features of spring and the spring environment.

3. six modules of Spring:

Control reversal IOC Container = BeanFactory + bean. xml

In spring, beanfacocois the core interface of IOC containers. BeanFacotory is used to instantiate, locate, configure objects in applications, and establish dependencies between these objects. Xmlbeanfacofactory implements the BeanFactory interface, and obtains xml configuration file data to form dependencies between application objects and objects.

  DI dependency injection (processing object dependencies! For example, after creating an Action, you need to process the Action dependent Service)

There are three injection methods in spring: set injection, interface injection, and constructor injection.

  Aop aspect object program for aspect-Oriented Programming!

AOP is vertical programming. For example, both business 1 and business 2 require a common operation. Instead of adding the same code to each business, it is better to write the code again, let the two businesses use this code together.

 

In spring, there are two ways to implement Aspect-Oriented implementations. One is dynamic proxy and the other is CGLIB. Dynamic proxy must provide interfaces, while CGLIB implementation has inheritance.

 

4. Spring development steps:

1. Download the Spring source code and introduce the Jar file: (After 3.0, only spring functions are available in the spring source code. If spring depends on other packages, download them separately)

Commons-logging-1.1.3.jar [need to download separately, spring dependent log package]
Spring-beans-3.2.5.RELEASE.jar "3.2 source code: Spring bean node support]
Spring-context-3.2.5.RELEASE.jar [3.2 source code: context support]
Spring-core-3.2.5.RELEASE.jar "3.2 source code: core functions]
Spring-expression-3.2.5.RELEASE.jar "3.2 source code: Spring expression]

2. Configure Application. xml/bean. xml (one of the names can be used)

Configure all the objects to be created in the Spring container,Corresponding namespaces and constraints can be copied in the source code!

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <beans xmlns = "http://www.springframework.org/schema/beans" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xmlns: p = "http://www.springframework.org/schema/p"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans5 http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 7 <! -- Create a User object --> 8 <bean id = "user" class = "cn. itcast. B _ioc.User"> </bean> 9 </beans>

3. Create an IOC container and create an object through the IOC container!

1. Create an object using a constructor without or without parameters;

2. Create an object using the factory class;

5. Explain how Spring creates beans:
1  <bean id="user"  class="cn.itcast.b_ioc_bean.User" scope="singleton" lazy-init="true" init-method="init" destroy-method="destroy"></bean>

5. 1. Use scope to specify whether the bean is a singleton or multiple examples:

Scope = "singleton. Create beans for all singleton instances at startup.

  Scope = "prototype"Multiple instances are not created at startup. [A new bean is created every time an instance is obtained from the container]

When creating Dao/Service, you can specify singleton or not. The default is singleton! You must specify this parameter when creating an Action!

5.2. Delayed Initialization
Lazy-init = "false" no delay in Initialization
Lazy-init = "true" indicates that no bean for the Singleton is created during container initialization;
Create an instance when obtaining the object from the container for the first time. If you obtain the object again, obtain the object of the last created Singleton.
5.3. Initialization and destruction methods
Init-method = ""
Initialization method. After the object is created, the initialization method is executed. if the object is a Singleton, the initialization method is executed once. If the object is multiple examples, the initialization method is executed multiple times.
Destroy-method = ""
Run this command when you call the destroy method of the ClassPathXmlApplicationContext object!

6. Bean lifecycle:

. Create an IOC container object first
. The object is created every time the bean is obtained from the container
. After the object is created, execute the initialization method (after each object is created)
. Object created
. Destroy the container and run the destroy method.

 

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.