spring常用注解的使用

2015-10-08 14:57:00    admin    2671    原创

我们在没有用注解写spring配置文件的时候,会在spring配置文件中定义Dao层的bean,

).@Autowired 与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上;
这样我们在service层中,写setDao方法,就可以直接通过接口调用Dao层,
用了注解写法后,在配置文件中不用再写Dao层的bean,
只需要在Dao实现类中加入
@Repository
public Class TestDaoImpl(){}
在service层定义
@Autowired
private TestDao testDao;
不需要再写setDao方法就可以通过接口调用Dao了
在Service层加入
@service
public Class TestService(){}
这样action中直接写
@Autowired
private TestService testService;
就可以调用service层
在Action层加入
@Controller
public Class TestAction(){}
就不需要在配置文件中定义action的bean
@requestMapping是spring-mvc的东西,
以前我们用struts2, 需要配置文件定义 action的名字,
在浏览器中写 XXX.do才能访问action的方法
使用了srping-mvc后,在action的方法中加入
@controller
public class TestAction(){
@requestMapping("XXX.do")
public String findAll(){
.................
}
}



豫ICP备15006410号
蝉知 蝉知4.5.2