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

A brief introduction to dependency injection in spring

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

Share

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

This article focuses on "A brief introduction to dependency injection in spring". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "A brief introduction to dependency injection in spring".

Dependency injection in spring

IOC function: reduce the coupling between programs (dependencies)

Dependency management: leave it to spring to maintain (objects of other classes are needed in the current class, which are provided by spring, and we just need to specify them in the configuration file)

Dependency maintenance: dependency injection

Dependency injection:

Data that can be injected:

Basic types and String

Other bean types (in configuration files or annotated configured bean)

Complex type / collection type

How to inject bean objects:

Use constructor

Use the set function

Use annotations

Use constructor

Constructor to enter: |

Tag used: constructor- arg

Where the tag appears: the inside of the bean tag

Attributes in tags

Type: used to specify the data type to be injected, which is also the type of one or some parameters in the constructor

Index: used to specify the data to be injected to assign values to the parameters at the specified index location in the constructor. The location of the index starts with e

Name: commonly used to assign values to parameters with the specified name in the constructor

= the above three are used to assign values to which parameter in the constructor

Value: used to provide data for basic types and String types

Ref: used to specify other bean type data. It refers to what has appeared in the Ioc core container of spring

Xmlns:xsi= "

Xsi:schemaLocation= "

Package com.ay.service

Public interface AccountService {

Public void saveAccount ()

}

Package com.ay.service.impl

Import com.ay.service.AccountService

Import java.util.Date

Public class AccountServiceImpl implements AccountService {

Private String name

Private Integer age

Private Date birthday

@ Override

Public void saveAccount () {

System.out.println ("method created successfully")

}

Public AccountServiceImpl (String name, Integer age, Date birthday) {

This.name = name

This.age = age

This.birthday = birthday

}

@ Override

Public String toString () {

Return "AccountServiceImpl {" +

"name='" + name +'\'+

", age=" + age +

", birthday=" + birthday +

'}'

}

}

Package com.ay.ui

Import com.ay.service.AccountService

Import org.springframework.context.ApplicationContext

Import org.springframework.context.support.ClassPathXmlApplicationContext

Public class Client {

Public static void main (String [] args) {

ApplicationContext ac = new ClassPathXmlApplicationContext ("bean.xml")

AccountService as = (AccountService) ac.getBean ("accountService")

As.saveAccount ()

System.out.println (as.toString ())

}

}

Summary:

Advantage: when getting a bean object, injecting data is a necessary operation, otherwise the object cannot be created successfully.

The downside: it changes the way bean objects are instantiated so that we have to provide this data if we don't need it when creating objects.

How much is the cost of Zhengzhou induced labor operation by using set function https://yiyuan.120ask.com/art/307587.html

Tag involved: property

Where it appears: inside the bean tag

Attributes of the tag

Name: used to specify the name of the set method called at inbound time

Value: used to provide data for basic types and String types

Ref: used to specify other bean type data. It refers to the bean object that has appeared in the Ioc core container of spring

Package com.ay.service

Public interface AccountService {

Public void saveAccount ()

}

Package com.ay.service.impl

Import com.ay.service.AccountService

Import java.util.Date

Public class AccountServiceImpl implements AccountService {

Private String name

Private Integer age

Private Date birthday

@ Override

Public void saveAccount () {

System.out.println ("method created successfully")

}

Public void setName (String name) {

This.name = name

}

Public void setAge (Integer age) {

This.age = age

}

Public void setBirthday (Date birthday) {

This.birthday = birthday

}

@ Override

Public String toString () {

Return "AccountServiceImpl {" +

"name='" + name +'\'+

", age=" + age +

", birthday=" + birthday +

'}'

}

}

Package com.ay.ui

Import com.ay.service.AccountService

Import org.springframework.context.ApplicationContext

Import org.springframework.context.support.ClassPathXmlApplicationContext

Public class Client {

Public static void main (String [] args) {

ApplicationContext ac = new ClassPathXmlApplicationContext ("bean.xml")

AccountService as = (AccountService) ac.getBean ("accountService")

As.saveAccount ()

System.out.println (as.toString ())

}

}

Xmlns:xsi=

Xsi:schemaLocation= ">

Summary:

Advantages: there are no clear restrictions when creating objects, and you can use the default constructor directly

The downside: if a member must have a value, it is possible that the set method did not execute.

At this point, I believe that you have a deeper understanding of "a brief introduction to dependency injection in spring". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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