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 configure the IOC of Spring Framework in Java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to configure the IOC of the Spring framework in Java, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

IOC configuration of Spring

The most important feature of Spring is the inversion of IOC control, which is beneficial to IOC and we can reduce the coupling between objects.

IOC needs to be implemented through certain configurations. Configuration methods are as follows:

1) use xml file to configure

2) configure with annotations

To use the basic features of Spring, you must first import the dependencies of Spring:

Org.springframework

Spring-context

5.1.5.RELEASE

Spring Context: provides context information to the Spring framework. The Spring context includes enterprise services such as JNDI, EJB, email, internationalization, checksum and scheduling capabilities. It contains Spring Core components, which can realize the core functions of IOC.

Configure using a xml file

/ * *

* CPU API

, /

Public interface Cpu {

Void run ()

}

/ * *

* CPU of AMD

, /

Public class AMDCpu implements Cpu {

Public void run () {

System.out.println ("AMD's CPU is running.")

}

}

/ * *

* memory interface

, /

Public interface Memory {

Void read ()

Void write ()

}

/ * *

* memory of DDR8G

, /

Public class DDR8GMemory implements Memory {

Public void read () {

System.out.println ("use DDR8G's memory to read data.")

}

Public void write () {

System.out.println ("write data using the memory of DDR8G.")

}

}

Similar IntelCpu and DDR16Memory classes omit the code

/ * *

* computers

, /

Public class Computer {

Private Cpu cpu

Private Memory memory

Private String brand

... Omit get\ set

Public Computer () {

}

Public Computer (String brand, Cpu cpu, Memory memory) {

This.brand = brand

This.cpu = cpu

This.memory = memory

}

Public void start () {

System.out.println (brand+ "computer started")

Cpu.run ()

Memory.read ()

Memory.write ()

}

}

Under the resources directory of the maven project, add the configuration file:

ApplicationContext.xml

Configuration instructions:

Is the root tag, which represents the Java object container of Spring

The tag represents the creation of a Java object in the container, the property id represents the object name, and class is the type of the object.

A cpu object and a memory object are first created in the configuration file, and then a computer object is created. There are cpu properties of type Cpu and memory properties of type Memory and brand properties of type String in computer. Here, dependency injection is used to assign values to the properties.

Property refers to the properties of the object, name is the property name, and ref is the object reference, which refers to the previous cpu object.

The brand attribute injects a numeric value instead of an object reference, which is injected using value.

Spring context object

The Spring container can be thought of as a factory of JavaBean BeanFactory,BeanFactory is responsible for creating and saving the subclasses of each JavaBean,BeanFactory are:

1) ClassPathXMLApplicationContext

Based on XML profile context

2) AnnotationConfigApplicationContext

Context based on annotation configuration

3) FileSystemApplicationContext

File system-based context

The method of using ClassPathXMLApplicationContext:

Public class TestComputer {

@ Test

Public void testComputer () {

/ / create the application context object for the XML file

ClassPathXmlApplicationContext cxt =

New ClassPathXmlApplicationContext ("applicationContext.xml")

/ / get the Java object from the container by type

Computer computer = cxt.getBean (Computer.class)

/ / you can also get the object through the object name

/ / Computer computer = (Computer) cxt.getBean ("computer")

Computer.start ()

}

}

Configure using annotations

Spring's IOC can also be configured entirely through Java code and annotations without using configuration files, which makes the code more concise.

Common notes:

@ Component

When configured on top of a class, the Spring container automatically scans and adds objects with the annotated class

@ Autowired

When configured on a property or set method, the container automatically injects objects of the same type in the container into the property

@ Qualifier

Used to set identities for different components to distinguish between multiple objects of the same type

@ Value

Inject general types of values, such as @ Value (20), @ Value ("Zhang San")

@ Configuration

Added to the configuration class, which serves as the entry point for Spring startup

@ ComponentScan

Used in conjunction with @ Configuration to add to the configuration class to scan all @ Component annotated classes in the package

Add @ Component annotations to DDR8Memory classes and IntelCpu classes

Modify the Computer class:

@ Component

Public class Computer {

@ Value ("Apple computer")

Private String brand

@ Autowired

Private Cpu cpu

@ Autowired

Private Memory memory

....

}

@ Configuration

@ ComponentScan ("com.qianfeng.springioc.demo4")

Public class MyConfig {

Public static void main (String [] args) {

/ / create an annotation-based context object

AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext (MyConfig.class)

/ / get the Computer object

Computer computer = cxt.getBean (Computer.class)

Computer.start ()

}

}

Thank you for reading this article carefully. I hope the article "how to configure the IOC of Spring Framework in Java" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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