Spring-simple example of control reversal and spring reversal

Source: Internet
Author: User

Spring-simple example of control reversal and spring reversal

The spring framework is an open-source lightweight container framework based on the core IOC and AOP technologies. It mainly solves the complex operation implementation of enterprises.

How can I explain IOC and AOP? In watching spring videos, the two terminologies must be understood.

IOC: inverse of Control: Control inversion. It means the relationship between programs, without code control, but completely controlled by containers. In the running stage, containers directly inject their relationships to components based on the configuration information. This is also the meaning of dependency injection. Dependency injection and control inversion are actually a concept. Only difference is emphasized. Dependency injection emphasizes that the injection of the link is completed by the container at runtime, while control inversion emphasizes that the link is controlled by the container. In fact, the essence is the same.

Use the code to demonstrate how the inversion is implemented.

1. Create a common java project.

2. Introduce the corresponding jar package. Spring. jar, log4j-1.2.14.jar (for logging), commons-logging.jar

3. Provides the log4j. properties configuration file. (You do not need to add a log jar package)

4. Provides the configuration file ApplicationContext. xml.

5. Start writing code.

  1. Package ioc. iocsample;
  2. /**
  3. * School
  4. * @ Author lhy
  5. *
  6. */
  7. Public class School {
  8. Private String name;
  9. Public School (String name)
  10. {
  11. This. name = name;
  12. }
  13. Public void printInfo ()
  14. {
  15. System. out. println ("the school name is:" + name );
  16. }
  17. }
  1. Package ioc. iocsample;
  2. /**
  3. * Student
  4. * @ Author lhy
  5. *
  6. */
  7. Public class Student {
  8. Public int id;
  9. Public String name;
  10. Private School;
  11. Public int getId (){
  12. Return id;
  13. }
  14. Public void setId (int id ){
  15. This. id = id;
  16. }
  17. Public String getName (){
  18. Return name;
  19. }
  20. Public void setName (String name ){
  21. This. name = name;
  22. }
  23. Public School getSchool (){
  24. Return school;
  25. }
  26. Public void setSchool (School school ){
  27. This. school = school;
  28. }
  29. }

 
Configuration file:

  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: aop = "http://www.springframework.org/schema/aop"
  5. Xmlns: tx = "http://www.springframework.org/schema/tx"
  6. Xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  7. Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  8. Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">
  9. <Bean id = "school" class = "ioc. iocsample. School">
  10. <Constructor-arg index = "0">
  11. <Value> Langfang Normal University </value>
  12. </Constructor-arg>
  13. </Bean>
  14. <Bean id = "student" class = "ioc. iocsample. Student">
  15. <Property name = "id" value = "001"/>
  16. <Property name = "name" value = "Zhang San"/>
  17. <Property name = "school" ref = "school"/>
  18. </Bean>
  19. </Beans>


The client performs a test. The Student has a reference in the School. The name of the School where the Student is enrolled is as follows:

  1. Package ioc. iocsample;
  2. Import org. springframework. beans. factory. BeanFactory;
  3. Import org. springframework. context. support. ClassPathXmlApplicationContext;
  4. Public class Client {
  5. Public static void main (String [] args ){
  6. // TODO Auto-generated method stub
  7. BeanFactory factory = new ClassPathXmlApplicationContext ("applicationContext. xml ");
  8. Student student = (Student) factory. getBean ("student ");
  9. Student. getSchool (). printInfo ();
  10. }
  11. }


The student class and school class are not instantiated in the program. They are directly created by the beanFactory in the container, which hides the creation details. At the same time, the program does not need to care about the dependency between the student class and the school class, but the container is responsible for it, the container injects the attribute values and dependencies into the javabean of the Student and School classes (in fact, the School and Student are a javaBean. JavaBean is a java class encapsulated according to certain principles .)

There are two types of dependency injection: one is value-based injection (using the getter and setter methods), and the other is using the constructor injection.

Check that the ID and name in student in the program use get and set to assign values: the configuration file is as follows:

<Property name = "id" value = "001"/>
<Property name = "name" value = "Zhang San"/>

In addition, if the school attribute in Student is of the School type, it is configured as follows in the container:

<Property name = "school" ref = "school"/>

The name in the School in the program is assigned by the constructor, and is configured as follows in the container:

<Constructor-arg index = "0">
<Value> Langfang Normal University </value>
</Constructor-arg>

If there is a parameter in the constructor, the index value starts from 0. If there are multiple parameters, the index value increases sequentially.

If the constructor is a class, the bean label is used.

<Constructor-arg index = "0">

<Bean class = "specific classes">

</Constructor-arg>

There are three methods for dependency injection DI (dependence Injection) in spring: Interface Injection and get set injection) constructor Injection)

Differences between the three injection methods:

1. Interface injection: components must rely on the implementation of specific interfaces. The loaded interface implementation and the specific objects of interface implementation are completed by containers. In this way, the interface must be dependent on the container. Such components are invasive and reduce reusability. For example, Context. lookup (ServletContext. getXXX) commonly used in J2EE development is a manifestation of interface injection. (This injection method is not commonly used)

2. getter/setter injection: it is clear about what needs to be injected. Comply with java design rules. It is more suitable for java developers and is more natural and convenient to use.

 

3. constructor injection: the dependent components have been injected when the class is loaded. However, if there are many parameters, it is inconvenient to use.

 

However, the latter two injection methods are commonly used by spring, while the first interface injection method is not commonly used.

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.