Spring基于Java类配置Bean(四):通过@Configuration配置类引用xml配置信息

本文介绍了如何在Spring的@Configuration配置类中结合@ImportResource引入XML配置文件,允许在Java配置类中利用@AutoWired注解直接引用XML定义的Bean。内容包括XML配置文件的结构,@Configuration配置类的编写,UserService类的定义,以及相应的测试类和测试结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

总结:

在@Configuration配置类中,通过@ImportResource就可以引入xml配置文件,在配置类中可直接通过@AutoWired引用xml文件中定义的bean。

(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/beans
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- <context:component-scan base-package="com.study.spring.bean.javaClassBean"/> -->
    <bean id = "userDao"  class = "com.study.spring.bean.javaClassBean.UserDao"/>

</beans>

(2)@Configuration配置类

package com.study.spring.bean.javaClassBean;

import org.springf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值