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 is the way to configure from xml to pure annotations

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

Share

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

The main content of this article is to explain "what is the way to configure xml to pure annotations". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the way from xml configuration to pure annotation"!

First, adopt pure XML configuration file

Before spring 2.5, using XML configuration files was relatively simple and intuitive, but there was a lot of configuration information.

For example:

Second, the way of XML+ annotations

After Spring 2.5, annotations have been added, mainly the SSM framework

It is mainly marked with @. The most important thing is @ component. Check the source code.

/ * * array in Target, scope used by ElementType, TYPE, class, interface, comment, enum FIELD, used to describe domain METHOD, method PARAMETER, parameter CONSTRUCTOR, constructor LOCAL_VARIABLE, local variables ANNOTATION_TYPE, PACKAGE, package name * / @ Target ({ElementType.TYPE}) / * * Retention declaration when SOURCE takes effect, only in the source file .java CLASS, retained in the compiled class file RUNTIME .java .class contains * * / @ Retention (RetentionPolicy.RUNTIME) / / annotation information of tag elements is contained in javadoc @ Documentedpublic @ interface Component {String value () default "" }

During the development process, the directory structure will create a new controller,service,dao three-tier structure, corresponding to the @ Controller,@Service,@Repository three annotations. In fact, click on the source code and find that the bottom of these three annotations are also the @ Component annotations.

1. Create a new project, create a new spring.xml under the resource directory, and add a package scan path

2. Create the class UserController under the path com.study.admin.controller, and add one of the above four annotations

3. If you add the test class UserTest, you can print the result, indicating that the class has been loaded into the IOC container

Public class UserTest {public static void main (String [] args) {ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext ("spring.xml"); / / you can get bean UserController bean = ioc.getBean (UserController.class); System.out.println (bean);} @ Test public void test1 () {ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext ("spring.xml") / / according to the name, the first letter is lowercase, you can also get bean UserController bean = (UserController) ioc.getBean ("userController"); System.out.println (bean);}}

4. Create service interface com.study.admin.service.BaseService and implementation class com.study.admin.service.impl.UserImpl, and dao layer data class com.study.admin.dao.UserDao.

In the controller layer, the business logic is implemented by calling service through @ Autowired, where the Autowired is matched first by type and then by name

4.1, if the injection is an interface, the implementation class will be found automatically, but if there are multiple implementation classes, an error will be reported here

Controller layer @ Controllerpublic class UserController {/ * 1. If BaseService has only one implementation class, it is normal * 2.1.If there are two implementation classes, the solution * 2.1. replace the following declared name with the corresponding class name (@ Autowired BaseService userService). ) * 2.2, the class injects service with the corresponding name @ Service ("baseService"), and * 2.3 add @ Qualifier ("userService") * 2.4 to one of the implementation classes with the difference between @ Primary * * Resource and Autowired * package in Autowired spring, priority type matching, and then name matching * Resource is the package of JDK Priority name matching, then type matching * / @ Autowired BaseService baseService Public void test () {baseService.getDate ();}} / / service layer / / Interface public interface BaseService {void getDate ();} / implementation class @ Servicepublic class RoleService implements BaseService {@ Override public void getDate () {System.out.println ("roleService get data");}} @ Servicepublic class UserService implements BaseService {@ Override public void getDate () {System.out.println ("userService get data") }} three, pure annotation mode, Javaconfig

Since spring 3.0, basically using spring boot to develop, no xml configuration file

1. Use @ configuration annotations instead of XML, @ ComponentScan instead of package scanning

2. The new bean is used to declare an external Bean. You need to new it and return it. The method name is the name stored in the IOC.

3. Use @ PropertySource ("db.properties") to map to the external configuration file, @ Value ("${mysql.name}") to get the value, ${} to get the value by name, and # {} to get the property value of the object

At this point, I believe you have a deeper understanding of "what is the way to configure xml to pure annotations". 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