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 write Spring program

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

Share

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

This article mainly explains "how to write Spring programs". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write Spring programs.

4.0.0

Javapub.rodert

FirstSpringProject

1.0-SNAPSHOT

Junit

Junit

4.13

Test

Org.springframework

Spring-context

5.2.7.RELEASE

Org.springframework

Spring-core

5.2.7.RELEASE

Org.springframework

Spring-beans

5.2.7.RELEASE

Create a package named javapub.rodert in the project's src directory, then create an interface named PersonDao in the package, and add an add () method package javapub.rodert to the interface

/ * *

* @ author wangshiyu rodert

* @ date 20:13 on 2020-7-2

* @ description

, /

Public interface PersonDao {

Public void add ()

}

Create an interface implementation class PersonDaoImpl

Create the PersonDao implementation class PersonDaoImpl under the javapub.rodert package

Package javapub.rodert

/ * *

* @ author wangshiyu rodert

* @ date 20:14 on 2020-7-2

* @ description

, /

Public class PersonDaoImpl implements PersonDao {

Public void add () {

System.out.println ("execution succeeded!!")

}

}

Create a Spring profile

Spring profile is the core of integrating Spring

Up to now, a Spring program has been built and tested.

Create a new test class

Package javapub.rodert;import org.junit.Test

Import org.springframework.context.ApplicationContext

Import org.springframework.context.support.ClassPathXmlApplicationContext

/ * *

* @ author wangshiyu rodert

* @ date 20:15 on 2020-7-2

* @ description

, /

Public class PersonDaoTest {

@ Test

Public void test1 () {

ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("ApplicationContext.xml")

PersonDao personDao = (PersonDao) applicationContext.getBean ("personDao")

PersonDao.add ()

}

}

Return the result:

Successful execution!

Use the JUnit test to run the test method, which runs successfully. When the program is executed, the creation of the object is not done through a new class, but through the Spring container management. This is how the Spring IoC (inversion of Control) container idea works.

Means that the IoC container uses the setter method to inject dependent instances. After the bean is instantiated by calling the no-parameter constructor or no-parameter static factory method, and then calling the setter method of the bean, the setter-based DI can be implemented.

Means that the IoC container uses constructors to inject dependent instances. Constructor-based DI is implemented by calling constructors with parameters, each of which represents a dependency.

At this point, I believe you have a deeper understanding of "how to write Spring programs". 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