报错,但是输出正常

来源:3-15 IoC在实际项目中的重要用途

白浊3147261

2022-09-28 09:59:49

相关代码:<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
     <bean id="bookDao" class="com.imooc.spring.ioc.bookshop.dao.BookDaoOracleImpl">

     </bean>
</beans>

相关代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="bookService" class="com.imooc.spring.ioc.bookshop.service.BookService">
        <property name="bookDao" ref="bookDao"/>
    </bean>
</beans>


相关截图:

https://img.mukewang.com/climg/6333aa3d09d917ce38402160.jpg

问题描述:

BookShopApplication中可以正常输出,就是不明白xml中为啥ref是红色的

写回答

1回答

好帮手慕小蓝

2022-09-29

同学你好,ref属性用于引用其他已经被声明过的bean。在同学提供的代码中,引用了命名为“bookDao”的bean,但是配置文件中是不可见的,所以IDEA会提示报错。但是如果同学通过其他方式或者在其他地方已经创建过这个bean,运行时是不会报错的。

祝学习愉快~

0

0 学习 · 9886 问题

查看课程