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

What are the core programming of Spring

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

Share

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

This article mainly explains "what are the core programming of Spring". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what the core programming of Spring is.

1) attribute setter injection

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. Geek time Brother Ma talks about the core programming idea of Spring

2) Construction method injection

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.

The following example demonstrates how the Spring container implements dependency injection through the case of property setter injection. The specific steps are as follows.

1. Create a PersonService interface

Create an interface called PersonService under the com.mengma.ioc package of the springDemo01 project, which contains an addPerson () method, as shown below.

Package com.mengma.ioc;public interface PersonService {public void addPerson ();} 2. Create an interface implementation class PersonServiceImpl

Create a class called PersonServiceImpl under the com.mengma.ioc package, which implements the PersonService interface, as shown below.

Package com.mengma.ioc;public class PersonServiceImpl implements PersonService {/ / defines the interface declaration that private PersonDao personDao;// provides a set () method for dependency injection public void setPersonDao (PersonDao personDao) {this.personDao = personDao;} / / method @ Overridepublic void addPerson () {personDao.add () that implements the PersonService interface; / / calls the add () method System.out.println in PersonDao ("addPerson () executes...");}}

In the above code, you first declare the personDao object and add a setter method to it for dependency injection, then implement the addPerson () method of the PersonDao interface, call the save () method in the method, and output a statement.

3. Add configuration information to applicationContext.xml

Add an element to the applicationContext.xml configuration file to instantiate the PersonServiceImpl class and inject an instance of personDao into personService. The actual code is as follows:

4. Write test methods

Create a method called test2 () in the FirstTest class, edited as follows:

@ Testpublic void test2 () {/ / define the path to the Spring configuration file String xmlPath = "applicationContext.xml"; / / initialize the Spring container and load the configuration file ApplicationContext applicationContext = new ClassPathXmlApplicationContext (xmlPath); / / get the personService instance PersonService personService = (PersonService) applicationContext.getBean ("personService") through the container; / / call personService's addPerson () method personService.addPerson () } Thank you for reading, the above is the content of "what is the core programming of Spring". After the study of this article, I believe you have a deeper understanding of what the core programming of Spring has, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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