开发者社区 问答 正文

spring autowired byName我没有按名字,为什么也给我配上了呢?


配置文件顶部是这样的。
然后配置了

<bean id="jdbcTemplate"   class="org.springframework.jdbc.core.JdbcTemplate">
   <property name="dataSource">
        <ref bean="dataSource" />
   </property>
</bean>

我的代码

@Autowired

private JdbcTemplate jt;

为什么这样也可以装配上啊?我百度,说byName,名字对不上的话会报错。我的既没报错,而且还装配上了。。。。

展开
收起
a123456678 2016-03-17 15:06:12 3253 分享 版权
1 条回答
写回答
取消 提交回答
  • 你在使用ref="xxx"的时候,这个default-autowire就是有用的了,default-autowire是在bean文件中的,和注解根本就不是一个概念的东西。Spring先处理bean文件中的autowire属性,然后使用BeanPostProcessor来处理@Autowired、@Resource等注解,就是你使用了定义的。你用Spring Tool Suite打开Bean文件,在Bean Graph中就会看到context:annotation-config开头的所扫描到的一系列bean

    2019-07-17 19:05:28
    赞同 展开评论