Chapter One spring core concept

Source: Internet
Author: User
Tags getcolor

Spring role: Manage various business beans in the project (service class, DAO class, Action Class), instantiate class, attribute assignment

The Spring IOC (inversion of control) inversion, also known as Dependency injection (Dependency Injection[di]), is a design concept in object-oriented programming to reduce the coupling between program code.

IOC principle: Focus on principle

Instead of building a business instance in the business code using the Common keyword new, we are in the configuration file. The XML node tells the container how to name the object of the corresponding type built in memory.

DI Dependency Injection: focus on implementation

When you create an object instance, you inject a property value or other object instance for that object, focusing on the implementation.

The relationship: They are a description of different aspects of spring's core thinking.

The first case of spring [Create student object without the New keyword]

Specific steps:

① Creating student entity classes

② creating applicationcontext.xml configuration file under src

③ Test class

---Introducing the core jar package: [Beans, Core, Context, Expression]

---Create entity classes student can be implanted with corresponding property values

---Write the spring configuration file under src

Header file Find location: source code

Applicationcontext.xml:

Note: In the spring configuration file, use the <bean> element to define a bean instance (also known as a component). There are two common properties: One is ID, the name of the bean instance that is defined, and the other is class, which represents the type of the defined bean instance

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     xsi:schemalocation= "5/         http Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> 6          7         <bean id= "Stu" class= "cn.happy.entity.Student" > 8         <property name= "name" value= "hehe" ></ Property> 9         <property name= "age" value= "></property>10 one </bean>12        13"         </beans>

--Testing test

The context of spring is instantiated by Classpathxmlapplicationcontext and the bean instance is obtained by ID based on the ApplicationContext Getbean () method. ApplicationContext is an interface that is responsible for reading the spring configuration file, managing the loading of objects, generating, maintaining the dependency between bean objects and bean objects, and responsible for the bean's life cycle. Classpathxmlapplicationcontext is an implementation class for the ApplicationContext interface that reads the spring configuration file from the Classpath path.

1 public class Test01 {2 @Test3 public void Addtest () {4     applicationcontext ctx=new classpathxmlapplicationcontext (" Applicationcontext.xml "), 5     Student stus= (Student) Ctx.getbean (" Stu ");    6     System.out.println (stus); 7     8}

Output Result:

Iv. Dependency Injection [DI]

A printer is assembled from different papers and combinations of different types of cartridges.

Specific steps:

① layering Create ink and paper interfaces

② Implementation Class

③ assembly in the configuration file

--Create an ink interface and its implementation class under the ink package

Ink Interface:

Public interface Ink {public    String getColor ();}

Implementation class:

public class Colorink implements ink{public    String GetColor () {                return "color Cartridge";    }}
public class Blackink implements ink{public    String GetColor () {                return "Black Cartridge";    }}

--Create the paper interface and its implementation class under the paper package

Paper Interface:

Public interface Paper {public    String getcontent ();}

Implementation class:

public class A4paper implements paper{public    String getcontent () {                return "I am A4";    }}
public class B5paper implements paper{public    String getcontent () {        return "I am B5";    }}

--Create the print class under the printer package and populate the Ink,paper member variable, called the Domain property. Encapsulation, and the method of printing.

public class Print {    private ink ink;    Private Paper Paper;        public void print () {        System.out.println ("with" +ink.getcolor () + "print" +paper.getcontent () + "paper");    }    

--Configuration files and configuration of ink and paper as well as printers

--Test class. The strong turn is the printer class print

1 public class Springtest {2     @Test3 public     void Test1 () {4         applicationcontext ctx=new Classpathxmlapplicationcontext ("Applicationcontext.xml"); 5         print pri= (print) Ctx.getbean ("printer"); 6         Pri.print (); 7     }

Output Result:

V. Spring aop[aspect Oriented Programming] aspect-oriented programming

AOP principle:

Decompose complex requirements into different aspects and centralize common functions scattered in the system to address

The agent mechanism is used to assemble and run, and the code snippets are enhanced and new functions are added without changing the original program.

AOP Purpose:

Separation of facets from the system, independent of the business logic implementation

How do I use AOP?

Specific steps:

① adding spring AOP-related jar files to your project

② writing pre-enhancement and post-enhancement implementation logging capabilities

③ Write a spring file to enhance the processing of business methods

④ writing code to get business objects with enhanced processing

--Add the relevant jar file:

--Business class: and implement a specific interface, in which the log output function is not implemented

Isomeservice:

Someservice:

--The next step is to add logging to this method in an AOP way, writing an enhanced class

Methodbeforeadvice for pre-enhancement

Post-build enhancements via Afterreturningadvice

--spring the configuration file for component declarations.

Note: The header file requires the introduction of the corresponding AOP

--Next, implement the AOP configuration in the spring configuration file, first defining the pointcut. Execution is a pointcut indicator, which is a pointcut expression in parentheses, which allows you to configure the method to be cut in, and the pointcut expression supports fuzzy matching

--Test class

Mark Red Note points:

An issue with interfaces:

If a class implements an interface, the proxy used by default is the JDK dynamic agent

If a class does not implement any interfaces, the default is to use the Cglib dynamic proxy

In this case, if the class Someservice class is strong, the following error will be reported:

Implementation results:

Chapter One spring core concept

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.