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 develop Sping annotations

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

Share

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

This article focuses on "how to develop Sping annotations". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to develop Sping annotations.

Basic annotations

@ Configuration

What it does: the tag represents a configuration class on the class (equivalent to a configuration class)

@ Bean

Function: put a bean in the container that is equivalent to the bean tag in the xml file

@ Configuration

Public class DemoConfig {

@ Bean

Public Person person () {

Person person = new Person ()

Person.setPerson ("china")

Return person

}

}

@ scope

Function: specify scope

Divided into single case, multiple cases, etc.

@ ComponentScan ()

Function: packet scanning

@ Configuration

@ ComponentScan ("com.zyh.pratice")

Public class DemoConfig {

@ Bean

Public Person person () {

Person person = new Person ()

Person.setPerson ("china")

Return person

}

}

All comments under the specified report can be scanned to

Filter

ExcludeFilters: does not contain

Usage

@ Configuration

@ ComponentScan (value = "com.zyh.pratice", excludeFilters = {

@ ComponentScan.Filter (type = FilterType.ASSIGNABLE_TYPE,value = IncludeBean.class)

})

Public class DemoConfig {

IncludeFilters: includ

Usage

@ Configuration

@ ComponentScan (value = "com.zyh.pratice", includeFilters = {

@ ComponentScan.Filter (type = FilterType.ASSIGNABLE_TYPE,value = IncludeBean.class)

}, useDefaultFilters = false)

Pay attention to setting useDefaultFilters to false when using includeFilters

FilterType

There are several types to choose from

@ Conditional Wuxi Gynecology Hospital http://www.bhnnk120.com/

Function: conditional annotation. Pass in an implementation class of Condition, and load bean only when the condition is met.

@ Conditional (ConditionDemo.class)

Public Bill bill () {

Return new Bill ()

}

Condition

Action position

On the method annotated by bean: register the bean when the condition is met

Class: register this class when the condition is met

Import

Purpose: import the specified class

@ Configuration

@ ComponentScan

@ Import (ImportDemo.class)

Public class DemoConfig2 {

}

So that ImportDemo will be registered with the container

ImportSelector

After implementing the ImportSelector interface, you can return multiple classes and register them in the container

ImportBeanDefinitionRegistrar

Public class BeanDefinitionDemo implements ImportBeanDefinitionRegistrar {

Public void registerBeanDefinitions (AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {

BeanDefinition beanDefinition = new RootBeanDefinition (BeanDemo.class)

BeanDefinitionRegistry.registerBeanDefinition ("beanDemo", beanDefinition)

At this point, I believe you have a deeper understanding of "how to develop Sping 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