/** * Created by Jack on 2020.12.21. * @Autowired注解实现自动化注入 * 1、注解默认使用类型(class)查找bean,与属性字段名称没有关系 * 2、属性字段可以提供set方法,也可以不提供 * 3、注解可以声明在属性级别或set方法级别 * 4、如果想要通过指定名称查找bean对象,需要结合@Qualifier(value="")使用 */ public class AccountService { @Autowired private AccountDao accountDao; public void test(){ System.out.println("AccountService Test..."); accountDao.test(); } } public class AccountDao { public void test(){ System.out.println("AccountDao Test..."); } } <bean id="accountDao" class="com.xxxx.dao.AccountDao"></bean> <bean id="accountSercice" class="com.xxxx.service.AccountService"></bean> ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); AccountService accountService = (AccountService) ac.getBean("accountSercice"); accountService.test();