用注解书写mvc模式
1.新建PersonDao.java
public interface PersonDao {
public void savePerson();
}
2.新建PersonService.java
public interface PersonService {
public void savePerson();
}
3.新建PersonDaoImpl.java
@Repository("personDao")
public class PersonDaoImp implements PersonDao{
@Override
public void savePerson() {
System.out.println("save person");
}
}
4.新建PersonServiceImpl.java
@Service("personService")
public class PersonServiceImpl implements PersonService{
@Resource(name="personDao")
private PersonDao personDao;
@Override
public void savePerson() {
this.personDao.savePerson();
}
}
5.新建PersonAction.java
@Controller("personAction")
public class PersonAction {
@Resource(name="personService")
private PersonService personService;
// private Long id;
public void savePerson(){
this.personService.savePerson();
}
}
注意:如果一个类中有基本类型,并且基本类型使用spring的形式赋值的,此时该类必须用xml来完成,不能用注解。
6.新建测试类
public class PersonTest extends SpringHelper{
static{
path = "spring/mvc/spring/annotation/applicationContext.xml";
}
@Test
public void test(){
PersonAction personAction = (PersonAction) context.getBean("personAction");
personAction.savePerson();
}
}
7.运行结果
save person