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

相关文章

MybatisPlus中几种条件构造器运用方式

《MybatisPlus中几种条件构造器运用方式》QueryWrapper是Mybatis-Plus提供的一个用于构建SQL查询条件的工具类,提供了各种方法如eq、ne、gt、ge、lt、le、lik... 目录版本介绍QueryWrapperLambdaQueryWrapperUpdateWrapperL

SpringBoot简单整合ElasticSearch实践

《SpringBoot简单整合ElasticSearch实践》Elasticsearch支持结构化和非结构化数据检索,通过索引创建和倒排索引文档,提高搜索效率,它基于Lucene封装,分为索引库、类型... 目录一:ElasticSearch支持对结构化和非结构化的数据进行检索二:ES的核心概念Index:

Python数据验证神器Pydantic库的使用和实践中的避坑指南

《Python数据验证神器Pydantic库的使用和实践中的避坑指南》Pydantic是一个用于数据验证和设置的库,可以显著简化API接口开发,文章通过一个实际案例,展示了Pydantic如何在生产环... 目录1️⃣ 崩溃时刻:当你的API接口又双叒崩了!2️⃣ 神兵天降:3行代码解决验证难题3️⃣ 深度

Linux内核定时器使用及说明

《Linux内核定时器使用及说明》文章详细介绍了Linux内核定时器的特性、核心数据结构、时间相关转换函数以及操作API,通过示例展示了如何编写和使用定时器,包括按键消抖的应用... 目录1.linux内核定时器特征2.Linux内核定时器核心数据结构3.Linux内核时间相关转换函数4.Linux内核定时

Java方法重载与重写之同名方法的双面魔法(最新整理)

《Java方法重载与重写之同名方法的双面魔法(最新整理)》文章介绍了Java中的方法重载Overloading和方法重写Overriding的区别联系,方法重载是指在同一个类中,允许存在多个方法名相同... 目录Java方法重载与重写:同名方法的双面魔法方法重载(Overloading):同门师兄弟的不同绝

idea设置快捷键风格方式

《idea设置快捷键风格方式》在IntelliJIDEA中设置快捷键风格,打开IDEA,进入设置页面,选择Keymap,从Keymaps下拉列表中选择或复制想要的快捷键风格,点击Apply和OK即可使... 目录idea设www.chinasem.cn置快捷键风格按照以下步骤进行总结idea设置快捷键pyth

Linux镜像文件制作方式

《Linux镜像文件制作方式》本文介绍了Linux镜像文件制作的过程,包括确定磁盘空间布局、制作空白镜像文件、分区与格式化、复制引导分区和其他分区... 目录1.确定磁盘空间布局2.制作空白镜像文件3.分区与格式化1) 分区2) 格式化4.复制引导分区5.复制其它分区1) 挂载2) 复制bootfs分区3)

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

Java数组动态扩容的实现示例

《Java数组动态扩容的实现示例》本文主要介绍了Java数组动态扩容的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1 问题2 方法3 结语1 问题实现动态的给数组添加元素效果,实现对数组扩容,原始数组使用静态分配