小编Lid*_*vic的帖子

Spring @Autowired是按名称还是按类型注入bean?

我正在读初春(威利出版社)的书.在第2章中有一个关于Java配置的例子@Autowired.它提供了这个@Configuration

@Configuration
public class Ch2BeanConfiguration {

    @Bean
    public AccountService accountService() {
        AccountServiceImpl bean = new AccountServiceImpl();
        return bean;
    }

    @Bean
    public AccountDao accountDao() {
        AccountDaoInMemoryImpl bean = new AccountDaoInMemoryImpl();
        //depedencies of accountDao bean will be injected here...
        return bean;
    }

    @Bean
    public AccountDao accountDaoJdbc() {
        AccountDaoJdbcImpl bean = new AccountDaoJdbcImpl();
        return bean;
    }
}
Run Code Online (Sandbox Code Playgroud)

和这个普通的bean类

public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
    ...
} …
Run Code Online (Sandbox Code Playgroud)

java configuration spring code-injection autowired

17
推荐指数
1
解决办法
1万
查看次数

标签 统计

autowired ×1

code-injection ×1

configuration ×1

java ×1

spring ×1