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 implement dependency injection DI by spring

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

Share

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

This article mainly introduces spring how to achieve dependency injection DI related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe you will have something to gain after reading this spring how to achieve dependency injection DI article, let's take a look at it.

Spring dependency injection DI1, create a maven project mvn archetype:generate-DarchetypeCatalog=internal2, modify pom.xml

Introduce the required dependencies, first spring-context, then spring-test, and finally junit.

UTF-8 4.3.7.RELEASE junit junit 4.12 test org.springframework spring-context ${springframework.version} org.springframework spring-test ${springframework.version} org.apache.maven.plugins maven-compiler-plugin 1.8 1.8 utf-8 Maven-assembly-plugin 3.0.0 com.xueyoucto.xueyou.App jar-with-dependencies Make-assembly package single 3 、 Add classes Person and Bodypackage com.xueyou.demo Import org.springframework.stereotype.Component;@Componentpublic class Person {public String getName () {return name;} public void setName (String name) {this.name = name;} private String name;} package org.xueyou.demo;import org.springframework.stereotype.Component;@Componentpublic class Body {public int getId () {return id;} public void setId (int id) {this.id = id } private int id;} 4. In the configuration class App, add ComponentScan

It is important to note that the scanned package needs to be specified here.

Package com.xueyou.demo;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;/** * Hello world! * / @ Configuration@ComponentScan (basePackages = {"org.xueyou.demo", "com.xueyou.demo"}) public class App {public static void main (String [] args) {System.out.println ("Hello World!");}} 5, create a new test class package com.xueyou.demo;import org.junit.Assert Import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.xueyou.demo.Body;@RunWith (SpringJUnit4ClassRunner.class) @ ContextConfiguration (classes = App.class) public class Springtest @ Autowired private Body body; @ Autowired private Person person; @ Test public void testBodyIsNull () {Assert.assertNotNull (body) } @ Test public void testPersonIsNull () {Assert.assertNotNull (person);}} 6. Run the test class

The results are as follows:

7. We can see from the running results

The Person class and the study class have been injected into spring, and spring can use them.

The spring-test dependency cannot use the problem org.springframework spring-test 4.3.7.RELEASE test

Remove

This is the end of test's article on "how spring implements dependency injection DI". Thank you for reading! I believe you all have a certain understanding of "how to achieve dependency injection DI in spring". If you want to learn more, you are welcome to follow the industry information channel.

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