In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of Spring using annotations to develop methods, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will have something to gain after reading this article on the methods of Spring development using annotations, let's take a look at it.
To use annotation development after Spring4, you must ensure that the aop package is imported
Using annotations requires importing context constraints to add support for annotations
@ Component: if the component is placed on the class, it means that the class is managed by Spring and is called bean.
Import org.springframework.stereotype.Component;// is equivalent to @ Componentpublic class User {public String name = "xxx";}
@ Value
Import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;// is equivalent to @ Componentpublic class User {@ Value ("xxx") / / equivalent to public String name;}
Or
Import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;// is equivalent to @ Componentpublic class User {public String name; @ Value ("xxx") public void setName (String name) {this.name = name;}}
@ Component has several derivative annotations that we will layer according to the MVC three-tier architecture in Web development.
Dao [@ Repository]
Service [@ Service]
Controller [@ Controller]
These four annotation functions all represent registering a class with Spring to assemble Bean.
The scope of the annotation @ Scope
@ Scope is placed on the class. The default is singleton mode.
@ Scope (prototype) is a prototype pattern, creating a new object each time
Its effect is equivalent to
Add:
The code below @ Scope ("singleton") or @ Scope singleton mode outputs true.
@ Scope ("prototype") the following code outputs the result as false
Xml vs comments
Xml is more versatile and suitable for any occasion, simple and convenient maintenance.
Annotations are not used by their own class without talking about the relatively complex maintenance.
Best practice: xml is used to manage bean
Annotations are only responsible for completing the injection of attributes
In the process of using it, we need to pay attention to the following code
Examples of best practices
Import org.springframework.beans.factory.annotation.Value;public class User {@ Value ("XXX") public String name; public void setName (String name) {this.name = name;}} this is the end of the article on "methods for developing Spring using annotations". Thank you for reading! I believe you all have a certain understanding of "the method of Spring development using annotations". 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.