@Autowired 注解的工作方式

// 定义主从数据库服务器,主数据库负责(Write op),从数据库负责(read op�?
@Autowired
protected SqlMapClientTemplate masterSqlMapClientTemplate;

@Autowired
protected SqlMapClientTemplate slaveSqlMapClientTemplate;
我一直迷惑于项目没有使用读写分离,相应的jdbc也只配了masterSqlMapClientTemplate的数据源,<!-- 根据sqlMapClient创建一个SqlMapClientTemplate的模版类实例sqlMapClientTemplate -->
<bean id="masterSqlMapClientTemplate"
class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="masterSqlMapClient" />
</bean>
怎么slaveSqlMapClientTemplate也会起作用呢?
原来@Autowired 的工作方式是按照类型进行匹配的,不是按照后面的那个值,比如SqlMapClientTemplate masterSqlMapClientTemplate;它会在xml查找类型为SqlMapClientTemplate的bean,而不是masterSqlMapClientTemplate,这跟我之前学习的spring稍微有些不同的地方,是因为这个注解很特殊,一般的spring通过xml配置好bean的Id,然后在程序中需要的地方写上注解,它会根据注解的地方拿到这个id的bean,然后装配上对象,而@Autowired这个注解却不是通过id,而是通过类型装配

猜你喜欢

转载自vitoer.iteye.com/blog/2326496