首页 > 其他分享 >Spring的@Configiration和@ComponentScan注解代替xml配置文件

Spring的@Configiration和@ComponentScan注解代替xml配置文件

时间:2022-10-23 00:01:33浏览次数:58  
标签:xml 配置文件 Spring ComponentScan 注解 Configuration

1.使用xml配置文件配置Spring容器:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.oxygen.dao.impl"/>

</beans>

2.Spring的纯注解开发模式用以下两个注解代替xml配置文件.

@Configuration
@ComponentScan

 

3.为了使用这两个注解,我们需要写一个java类,称之为java 配置类

package com.oxygen.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.oxygen.dao")
public class SpringConfig {
}

 

@Configuration注解的作用是告诉Spring这个类是配置类,它的作用是代替xml配置文件

@ComponentScan注解的作用是代替xml配置文件当中的component-scan.

 <context:component-scan base-package="com.oxygen.dao.impl"/>

@ComponentScan注解值用一次,如果要设置多个扫描的包,则用数组格式,用逗号隔开.

注意,外面用大括号,中间用逗号隔开。

@ComponentScan({"com.oxygen.dao","com.oxygen.service"})

标签:xml,配置文件,Spring,ComponentScan,注解,Configuration
From: https://www.cnblogs.com/majestyking/p/16817656.html

相关文章

  • 微服务组件--注册中心Spring Cloud Eureka分析
    Eureka核心功能点【1】服务注册(register):EurekaClient会通过发送REST请求的方式向EurekaServer注册自己的服务,提供自身的元数据,比如ip地址、端口、运行状况指标的url、......
  • SpringBoot 创建项目连接mysql数据库
    Spring 创建项目1.创建一个springboot项目2.点击File---- New---- project项目名称可以随便填写...3. Springboot版本尽量不要最新版,怕你们驾驭不了......
  • SpringBoot的 META-INF/spring.factories有什么用
    1,spring.factories文件中的内容,将来都会转化Properties对象。spring.factories内容示例org.springframework.data.repository.core.support.RepositoryFactorySupport=org.s......
  • PG入门(2)postgreSQL重要配置文件及参数设置
    pg_hba.conf作用哪些主机可以连接数据库实例哪个数据库用户可以使用它允许这个用户使用哪些数据库客户端使用什么连接方式和认证方式postgresql.conflisten_addresses='*'......
  • 2流高手速成记(之四):SpringBoot整合redis及mongodb
    最近很忙,好不容易才抽出了时间,咱们接上回上次我们主要讲了如何通过SpringBoot快速集成mybatis/mybatis-plus,以实现业务交互中的数据持久化,而这一切都是基于关系型数据库(SQ......
  • SpringCloudConfig简单使用
    动态修改配置文件信息,避免每次改动都需要发版的问题SpringCloudConfig是一个分布式的配置管理方案,包含了server端和client两个部分,同时我们需要创建个git仓库,这个仓库就......
  • Spring基础使用一
    Spring基础使用一概念:Spring是一个开源框架,它是由RodJohnson创建的,同时是为了解决企业应用开发的复杂性而创建的,Spring可以使用基本的JavaBean来完成以前只可能由EJB完成......
  • C# HTTP POST AND GET json or xml
    usingSystem.Net;usingSystem.Net.Cache;usingSystem.IO;stringHttpPost(stringstrUrl,stringstrPostData){s......
  • SpringBoot 后台管理系统竟然出了详细教程!
    其实项目网上有很多了,但是教程比较详细的没多少,今天分享的项目从安装部署到代码具体功能都有很详细都说明。eladmin是一款基于SpringBoot2.1.0、Jpa、SpringSecurit......
  • SpringBoot微服务打包Docker镜像
    1.构建sprintboot项目2.打包应用3.编写dockerfileidea下载docker插件(可以高亮)4.构建镜像先把之前镜像都移除dockerrm-f$(dockerps-qa)在Linuxhome目......