Spring学习笔记(五)基于注解的IOC案例
1.创建账户实体类
@Component
public class Account {
@Value("1")
private Integer id;
@Value("Tom")
private String name;
@Value("34567")
private Float money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
2.创建接口
public interface IAccountDao {
void saveAccount();
}
public interface IAccountService {
void saveAccount();
}
3.创建实现类
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
@Autowired
private Account account;
@Override
public void saveAccount() {
System.out.println("基于注解的spring");
System.out.println("保存的账户为:"+"Account{" +
"id=" + account.getId() +
", name='" + account.getName() + '\'' +
", money=" + account.getMoney() +
'}');
}
}
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
@Autowired
@Qualifier("accountDao")
private IAccountDao accountDao=null;
@Override
public void saveAccount() {
accountDao.saveAccount();
}
}
4.创建测试类
public class AccountServiceTest {
ApplicationContext applicationContext;
@Before
public void setUp(){
applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void testAccountService(){
IAccountService accountService = applicationContext.getBean("accountService", AccountServiceImpl.class);
accountService.saveAccount();
}
}
5.配置 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="cn.edu.wtu"></context:component-scan>
</beans>