Spring -> IOCxml使用注解的方式创建类对象(@Component,@Service,@Repository,@Controller)

本文主要是介绍Spring -> IOCxml使用注解的方式创建类对象(@Component,@Service,@Repository,@Controller),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.XML

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--需要使用到context的命名标签名称:组件扫描描述:扫描类路径以查找自动注册为springbean的带注释组件。默认情况下,将检测到Spring提供的;@Component@Repository@Service@Controller@RestController@ControllerAdvice@Configuration原型。base-package格式(可以写一个总包的路径,也可以使用逗号隔开使用多个路径)--><context:component-scan base-package="test10month.test1012"/><!--附加:方式一:功能描述:只扫描xxx(这里是component)注解,不使用默认的都扫描include:包括   filter:过滤--><context:component-scan base-package="test10month.test1012" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/></context:component-scan><!--方式二:功能描述:除了xxx(这里是Service)不扫描以外,其他的都扫描exclude:排除   filter:过滤--><context:component-scan base-package="test10month.test1012"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/></context:component-scan>
</beans>

2.类

/*** 四个注解都可以使用,作用相同,都是给类创建对象* (value = "annotationSpring"),可以不写,默认为类的名字,首字母小写* value也可以不写,直接写"xxx",实现接口的时候最好写出具体名称*/
@Component
@Service
@Repository
@Controller(value = "annotationSpring")
class AnnotationSpring {public void test() {System.out.println("注解测试");}
}

3.测试类

package test10month.test1012;import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;/*** 功能描述:* @version 1.0* @className AnnotationSpringTest* @author: 罗德* @create: 2020-10-12 19:17*/
public class AnnotationSpringTest {public static void main(String[] args) {var context = new ClassPathXmlApplicationContext("test10month/test1012/AnnotationSpring.xml");var annotationSpring = context.getBean("annotationSpring", AnnotationSpring.class);annotationSpring.test();/*** 注解测试*/}
}

这篇关于Spring -> IOCxml使用注解的方式创建类对象(@Component,@Service,@Repository,@Controller)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/194847

相关文章

golang 对象池sync.Pool的实现

《golang对象池sync.Pool的实现》:本文主要介绍golang对象池sync.Pool的实现,用于缓存和复用临时对象,以减少内存分配和垃圾回收的压力,下面就来介绍一下,感兴趣的可以了解... 目录sync.Pool的用法原理sync.Pool 的使用示例sync.Pool 的使用场景注意sync.

Swagger2与Springdoc集成与使用详解

《Swagger2与Springdoc集成与使用详解》:本文主要介绍Swagger2与Springdoc集成与使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录1. 依赖配置2. 基础配置2.1 启用 Springdoc2.2 自定义 OpenAPI 信息3.

java对接第三方接口的三种实现方式

《java对接第三方接口的三种实现方式》:本文主要介绍java对接第三方接口的三种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录HttpURLConnection调用方法CloseableHttpClient调用RestTemplate调用总结在日常工作

Golang interface{}的具体使用

《Golanginterface{}的具体使用》interface{}是Go中可以表示任意类型的空接口,本文主要介绍了Golanginterface{}的具体使用,具有一定的参考价值,感兴趣的可以了... 目录一、什么是 interface{}?定义形China编程式:二、interface{} 有什么特别的?✅

使用Python实现调用API获取图片存储到本地的方法

《使用Python实现调用API获取图片存储到本地的方法》开发一个自动化工具,用于从JSON数据源中提取图像ID,通过调用指定API获取未经压缩的原始图像文件,并确保下载结果与Postman等工具直接... 目录使用python实现调用API获取图片存储到本地1、项目概述2、核心功能3、环境准备4、代码实现

windows和Linux安装Jmeter与简单使用方式

《windows和Linux安装Jmeter与简单使用方式》:本文主要介绍windows和Linux安装Jmeter与简单使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Windows和linux安装Jmeter与简单使用一、下载安装包二、JDK安装1.windows设

Spring 缓存在项目中的使用详解

《Spring缓存在项目中的使用详解》Spring缓存机制,Cache接口为缓存的组件规范定义,包扩缓存的各种操作(添加缓存、删除缓存、修改缓存等),本文给大家介绍Spring缓存在项目中的使用... 目录1.Spring 缓存机制介绍2.Spring 缓存用到的概念Ⅰ.两个接口Ⅱ.三个注解(方法层次)Ⅲ.

Spring Boot 整合 Redis 实现数据缓存案例详解

《SpringBoot整合Redis实现数据缓存案例详解》Springboot缓存,默认使用的是ConcurrentMap的方式来实现的,然而我们在项目中并不会这么使用,本文介绍SpringB... 目录1.添加 Maven 依赖2.配置Redis属性3.创建 redisCacheManager4.使用Sp

Spring Cache注解@Cacheable的九个属性详解

《SpringCache注解@Cacheable的九个属性详解》在@Cacheable注解的使用中,共有9个属性供我们来使用,这9个属性分别是:value、cacheNames、key、key... 目录1.value/cacheNames 属性2.key属性3.keyGeneratjavascriptor

PyTorch中cdist和sum函数使用示例详解

《PyTorch中cdist和sum函数使用示例详解》torch.cdist是PyTorch中用于计算**两个张量之间的成对距离(pairwisedistance)**的函数,常用于点云处理、图神经网... 目录基本语法输出示例1. 简单的 2D 欧几里得距离2. 批量形式(3D Tensor)3. 使用不