Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Spring Framework

Java Spring Framework

Common Java Libraries Common Java Libraries


Spring is an open-source Java enterprise application development framework created by Rod Johnson in 2003. It provides a comprehensive programming and configuration model for building modern Java applications.

Core features of the Spring Framework:


Core Modules of the Spring Framework

1. Core Container

The core container is the foundation of the Spring Framework and includes:

2. Data Access/Integration

3. Web Module

4. Other Important Modules


Core Concepts of Spring

1. Inversion of Control (IoC) and Dependency Injection (DI)

IoC is the core concept of Spring; it transfers the creation of objects and the management of their dependencies from application code to the container.

Example

// Traditional way

public

class

UserService

{

   

private

UserRepository userRepository

=

new

UserRepositoryImpl

(

)

;

}

// Spring DI approach

public

class

UserService

{

   

private

UserRepository userRepository

;

   

   

// Inject via constructor

   

public

UserService

(

UserRepository userRepository

)

{

       

this

.

userRepository

=

userRepository

;

   

}

}

2. Aspect-Oriented Programming (AOP)

AOP allows cross-cutting concerns (such as logging, transactions, security, etc.) to be separated from business logic.

Example

@Aspect

@

Component

public

class

LoggingAspect

{

   

    @Before

(

"execution(* com.example.service..(..))"

)

   

public

void

logBefore

(

JoinPoint joinPoint

)

{

       

System

.

out

.

println

(

"Before method execution: "

joinPoint.

getSignature

(

)

.

getName

(

)

)

;

   

}

}

3. Spring Bean

Objects managed by the Spring container are called Beans. Beans can be defined via XML configuration or annotations.

Example

// Define a Bean using annotations

@

Component

public

class

MyService

{

   

// ...

}

// XML configuration approach

<

bean id

=

"myService"

class

=

"com.example.MyService"

/>


Introduction to Spring Boot

Spring Boot is an extension of the Spring Framework designed to simplify the initial setup and development process of Spring applications.

Main features of Spring Boot:

Creating a simple Spring Boot application:

Example

@SpringBootApplication

public

class

MyApp

{

   

public

static

void

main

(

String

[

]

args

)

{

        SpringApplication.

run

(

MyApp.

class

, args

)

;

   

}

}

@RestController

class

HelloController

{

    @GetMapping

(

"/hello"

)

   

public

String

hello

(

)

{

       

return

"Hello, Spring Boot!"

;

   

}

}


Advantages of the Spring Framework

  1. Modular Design: You can choose which modules to use as needed
  2. Simplified Development: Reduces boilerplate code through DI and AOP
  3. Test-Friendly: Testability was considered from the design stage
  4. Powerful Ecosystem: Rich third-party libraries and extensions
  5. Active community: Continuous updates and maintenance

Suggested Learning Path for Spring

  1. First master core Java knowledge
  2. Learn Spring Core and the DI/AOP concepts
  3. Practice Spring MVC to develop web applications
  4. Learn Spring Data for database operations
  5. Master Spring Boot to simplify the development workflow
  6. Explore Spring Cloud to build microservices

The Spring Framework is the foundation of modern Java development; mastering it will greatly improve your development efficiency and project quality.

Common Java Libraries Common Java Libraries