Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build the basic environment of Spring

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to build the basic environment of Spring". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is Spring?

Spring is an open source framework that handles the coupling between the business logic layer and other layers. Therefore, Spring runs the idea of interface-oriented development throughout the application of the whole system, and Spring is a lightweight framework, so at the beginning of its birth, it became popular in the Java development market and was widely recognized and responded.

Basic concepts of Spring

Dependency injection (DI:Dependency Injection), also known as IoC:Inversion of Control, transfers control of a component object from the code itself to an external container. The Spring container is also an IoC container, which is used to manage all Java Bean, mainly through BeanFactory to generate and manage Bean.

Spring framework building 1. Create a Java Project

The Spring framework supports both Java Project and Dynamic Web Project, as follows:

two。 Import the Jar package required by the Spring framework

A total of 6 packages are needed to build the Spring framework, as shown below:

1 / / log package 2 commons-logging-1.1.1.jar3 / / spring core package 4 spring-aop-4.0.6.RELEASE.jar5 spring-beans-4.0.6.RELEASE.jar6 spring-context-4.0.6.RELEASE.jar7 spring-core-4.0.6.RELEASE.jar8 spring-expression-4.0.6.RELEASE.jar3. Configure Spring Profil

For Java Project, applicationContext.xml must be placed in the src directory, as follows:

12 6 7 8 9 10 11 12 13

For the Student class in the configuration file, the definition is as follows:

1 package com.hex.first; 23 / * * 4 * define a student category 5 * @ author Administrator 6 * 7 * / 8 public class Student {9 10 / * 11 * Student ID12 * / 13 private int id;14 15 / * 16 * Student name 17 * / 18 private String name;19 20 / * 21 * Age 22 * / 23 private int age 24 25 public int getId () {26 return id;27} 28 public void setId (int id) {29 this.id = id;30} 31 public String getName () {32 return name;33} 34 public void setName (String name) {35 this.name = name;36} 37 public int getAge () {38 return age 39} 40 public void setAge (int age) {41 this.age = age;42} 43 44 @ Override45 public String toString () {46 / / TODO Auto-generated method stub47 return "ID=" + id+ ", Name=" + name+ ", Age=" + age;48} 49}

View Code

4. Declare object

For the general declaration of objects and the use of Spring to obtain objects are as follows:

1 / / regular New object method declares an object 2 Student student=new Student (); 3 student.setId (1); 4 student.setName ("hex"); 5 student.setAge (20); 6 System.out.println (student); 7 / / injected via Spring, Spring context object 8 ApplicationContext context=new ClassPathXmlApplicationContext ("applicationContext.xml"); 9 / get bean object 10 Student student01= (Student) context.getBean ("student") whose id is student from Spring's IOC container; 11 System.out.println (student01)

View Code

5. Test result

As follows:

What are the differences between Spring and traditional methods?

All the traditional methods are hard-coded, and once there is a change, the code needs to be modified, while Spring uses injection to put changeable things in the configuration file to facilitate modification.

1. First of all, suppose a scenario in which students have a need to learn a course.

With regard to the course interface (ICourse), the definition is as follows:

1 / * * 2 * define a course interface 3 * @ author Administrator 4 * 5 * / 6 public interface ICourse {7 8 / * * 9 * learn 10 * / 11 void learn (); 12}

View Code

There are two implementation classes: JavaCourse and HtmlCourse, with the following code:

1 package com.hex.first; 2 3 / * * 4 * learn Java course 5 * @ author Administrator 6 * 7 * / 8 public class JavaCourse implements ICourse {9 10 @ Override11 public void learn () {12 System.out.println ("study Java course.") ; 13 14} 15 16}

View Code

And

1 package com.hex.first; 2 3 / * * 4 * learn Html course 5 * @ author Administrator 6 * 7 * / 8 public class HtmlCourse implements ICourse {9 10 @ Override11 public void learn () {12 System.out.println ("study Html course.") ; 13 14} 15 16}

View Code

If a student wants to take these two courses, there are three ways to implement them: 1. The first: the primitive polymorphic method

Add two methods to the Student class to learn Java and Html, as follows:

1 / * * 2 * learn Java 3 * / 4 public void learnJava () {5 ICourse course=new JavaCourse (); 6 course.learn (); 7} 8 9 / * * 10 * Learning Html11 * / 12 public void learnHtml () {13 ICourse course=new HtmlCourse (); 14 course.learn (); 15}

View Code

The calling code is as follows:

1 / / 1, the most primitive polymorphic method 2 System.out.println ("the first, the most primitive polymorphic method:"); 3 student.learnJava (); 4 student.learnHtml ()

View Code

Note: this program leads to a strong coupling relationship between students and courses, if you need to add a course to learn the Python language, you need to increase the implementation of the interface class (PythonCourse), modify the Student class, increase the learnPython method, change a lot, or some students only learn one of the courses, while other students learn two courses, it will be more troublesome to adopt this program.

two。 The second is to adopt a simple factory method to extract the creation of the course and be managed by the factory.

Add a learn method to Student:

1 / * * 2 * learn to distinguish 3 * @ param name4 * / 5 public void learn (String name) {6 ICourse course=CourseFactory.getCourse (name); 7 course.learn (); 8} by parameters

View Code

Add factory class (CourseFactory)

1 package com.hex.first; 2 3 / * * 4 * course Factory 5 * @ author Administrator 6 * 7 * / 8 public class CourseFactory {9 10 / * * 11 * get the course object 12 * @ param name13 * @ return14 * / 15 public static ICourse getCourse (String name) {16 if (name.equals ("java")) {17 return new JavaCourse () 18} else {19 return new HtmlCourse (); 20} 21} 22}

View Code

Then when the client calls, all it needs to do is pass the string. As follows:

1 / / 2, simple factory method 2 System.out.println ("2, simple factory method:"); 3 student.learn ("java"); 4 student.learn ("html")

View Code

Note: although this scheme strips out the creation of objects and is managed by the factory, the type of courses is still hard-coded and is not perfect.

3. The third is to realize the dynamic injection of objects by Spring.

Add learning methods to the student class, and the parameter is ICourse interface:

1 / * * 2 * delivery interface 3 * @ param course4 * / 5 public void learn (ICourse course) {6 course.learn (); 7} 8

View Code

In the Spring configuration file, configure the objects to be injected:

1 2 3

Get the object through Spring to realize dynamic control:

1 / / 3, 2 System.out.println through SpringIOC ("3, through SpringIOC:"); 3 ICourse course= (ICourse) context.getBean ("java"); 4 student.learn (course); 5 ICourse course2= (ICourse) context.getBean ("html"); 6 student.learn (course2)

View Code

All of the above three ways can achieve the requirements, and which scheme can be adopted can be determined according to the actual situation.

This is the end of "how to build the basic Environment of Spring". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report