(bean配置类的注解开发)学习Spring的第十三天

本文介绍了如何使用Spring框架的@Configuration注解替代XML配置文件,实现类作为配置类,并展示了@ComponentScan,@PropertySource和@Import等注解在配置中的应用。
摘要由CSDN通过智能技术生成

bean配置类的注解开发

问题提出

用类充当配置文件 applicationcontext.xml :

@Configuration注解标识此类为配置类,替代原有xml文件

 

 

 看原配置文件applicationcontext.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/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">

        <!--注解组件扫描: 扫描的指定基本包及其包下的类,识别使用@component注解-->
        <context:component-scan base-package="com.itheima"/>


        <context:property-placeholder location="classpath:jdbc.properties"/>


</beans>

这是bean配置类的文件SpringConfig(Java文件)

package com.itheima.config;

import com.itheima.beans.OtherBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;

@Configuration  //标注当前类是一个配置类(替代配置文件) +@Component
//<context:component-scan base-package="com.itheima"/> 配置组件扫描
@ComponentScan({"com.itheima"})
//<context:property-placeholder location="classpath:jdbc.properties"/> 配置properties加载
@PropertySource("classpath:jdbc.properties")
//<import resource=""></import>
@Import(OtherBean.class)
public class SpringConfig {

}

@ComponentScan({"com.itheima"})

组件扫描配置:扫描包
@PropertySource("classpath:jdbc.properties") 

加载外部资源properties
@Import(OtherBean.class)

导入其他配置类

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值