AccountDao
public interface AccountDao {
void saveAccount();
}
AccountDaoImpl
public class AccountDaoImpl implements AccountDao {
public void saveAccount() {
System.out.println("保存了账户");
}
}
AccountService
public interface AccountService {
void saveAccount();
}
AccountServiceImpl
public class AccountServiceImpl implements AccountService{
private AccountDao accountDao = new AccountDaoImpl();
public void saveAccount() {
accountDao.saveAccount();
}
}
bean.xml
上面有两个实体类,就配置两个bean,创建两个对象
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--把对象的创建交给bean来处理-->
<bean id="accountService" class="com.hh.service.AccountServiceImpl"/>
<bean id="accountDao" class="com.hh.dao.AccountDaoImpl"/>
</beans>
测试类:
public class Client {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("bean.xml");
AccountService service =
context.getBean("accountService", AccountService.class);
AccountDao accountDao =
context.getBean("accountDao", AccountDao.class);
System.out.println(service);
System.out.println(accountDao);
}
}