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 use JavaConfig instead of xml to realize Spring configuration operation

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

Share

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

Editor to share with you how to use JavaConfig instead of xml to achieve Spring configuration operation, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

JavaConfig instead of xml implements Spring configuration of simple entity class package indi.stitch.pojo;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Componentpublic class User {private String name; public String getName () {return name;} @ Value ("Stitch") public void setName (String name) {this.name = name } @ Override public String toString () {return "User {" + "name='" + name +'\'+'}';}}

The @ Component annotation hands the entity class to the Spring container to be hosted as a SpringBean component

JavaConfig configuration class package indi.stitch.config;import indi.stitch.pojo.User;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan ("indi.stitch.pojo") public class BeanConfig {@ Bean public User getUser () {return new User ();}}

The @ Configuration annotation represents a configuration class that is Bean, which is equivalent to a xml configuration file, and is essentially a Component

The @ ComponentScan annotation limits the scanning scope of the configuration class

@ Bean represents the registered Bean, which is equivalent to the bean tag in the xml configuration file

Test class import indi.stitch.config.BeanConfig;import indi.stitch.pojo.User;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class MyTest {public static void main (String [] args) {ApplicationContext context = new AnnotationConfigApplicationContext (BeanConfig.class); User user = context.getBean ("getUser", User.class); System.out.println (user.getName ());}}

To implement the configuration using JavaConfig, you need to use the AnnotationConfigApplicationContext class to obtain the Spring configuration context by passing in the bytecode object of the configuration class. In this context, you can get the Bean object marked with the @ Bean tag, and the passed parameter is the method name of the method marked with the @ Bean tag.

Test result

Configuration of Spring with JavaConfig without xml case 1

Add @ Configuration annotation to the class name in the configuration class, and provide a method to return an instance of Bean, and this method adds the annotation @ Bean, so the object can be managed by Spring.

As follows:

User.java (Bean)

Public class User {private String name; public String getName () {return name;} @ Value ("Juyss") / / public void setName (String name) {this.name = name;} @ Override public String toString () {return "User {" + "name='" + name +'\'+'}';}}

SpringConfig.java (Java configuration class)

@ Configuration / / indicates that this class is an instance of the Spring configuration class public class SpringConfig {@ Bean / /, registered as a Bean managed by Spring public User getUser () {return new User ();}}

ConfTest.java (test class)

Public class ConfTest {@ Test public void Test () {ApplicationContext context = new AnnotationConfigApplicationContext (SpringConfig.class); User user = context.getBean ("getUser", User.class); / / the first parameter must be consistent with the method name System.out.println (user);}}

The result of running the test class is:

User {name='Juyss'}

Case two

Adding the annotation @ Configuration to the class name in the configuration class indicates the Spring configuration class, and adding the annotation @ ComponentScan ("ClassPath") indicates the scanned package path.

Then add the annotation @ Component to the Bean class name that needs to be managed by Spring

User.java (Bean)

@ Component / / indicates that this class is registered as the value of the Bean component public class User {private String name; public String getName () {return name;} @ Value ("Juyss") / / injected name attribute public void setName (String name) {this.name = name } @ Override public String toString () {return "User {" + "name='" + name +'\'+'}';}}

SpringConfig.java (Java configuration class)

@ Configuration / / indicates that the Spring configuration class @ ComponentScan ("com.juyss.pojo") / / scan all registered Beanpublic class SpringConfig {} ConfTest.java (test class) public class ConfTest {@ Test public void Test () {ApplicationContext context = new AnnotationConfigApplicationContext (SpringConfig.class); User user = context.getBean ("user", User.class); / / the first parameter is the class name lowercase System.out.println (user) }}

The result of running the test class is:

User {name='Juyss'}

The above is all the contents of the article "how to use JavaConfig instead of xml to implement Spring configuration operations". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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