idea--关联Spring

1,508 阅读1分钟
  1. cmd + ;打开Project Structure
  2. 选择Facets菜单

目前没有任何关联

  1. 添加新的框架关联

选择spring

  1. 选择模块

点击OK
显示没有定义应用上下文,这是因为该项目不是基于Spring的。该项目的结果如下:
项目依赖中并没有Spring。

换一个spring项目

第一个框是自动选择的,和SpringBoot工程特定的目录标志有关,会自动选择这些特殊的文件

第二个需要手动选择添加非特殊的文件

IDEA自动关联,出现Spring标志,点击Spring标志,指向定义位置。

  1. Could not autowire. No beans of … type found 关联Spring后,在使用@AutowiredIDEA报错Could not autowire.No beans of '...'type found.
    但是项目可以正常运行,这个是IDEA的问题:spring auto scan配置,在编辑情况下,无法找不到对应的bean,于是提示找不到对应bean的错误。常见于mybatis的mapper,如下
<!-- mapper scanner configurer -->
<bean id="mapperScannerConfig" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.adu.spring_test.mybatis.dao" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

解决方案:

  1. 降低Autowired检测的级别,将Severity的级别由之前的error改成warning或其它可以忽略的级别.
  2. 去除关联Spring

个人觉得:降低Autowired检测的级别较好,因为关联Spring确实可以带来编码的便利,而且自动引入错误较少出现,也很容易发现改正。