spring 中 @Autowired 是什么意思啊,基于注解的定义就不用说了,我只想知道这个@Autowired 的作用。

另基于注解的命令还有其他那些啊,我只知道@controller,@requestMapping,@service等,哪位高手能帮我详细说明一下啊,感谢

我们在没有用注解写spring配置文件的时候,会在spring配置文件中定义Dao层的bean,
这样我们在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(){
.................
}
}
这样,就不需要struts了,直接在浏览器中输入XXX.do就可以访问findAll方法了
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-03-31

action中直接写

@Autowired

private TestService testService;

就可以调用service层。

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

这样我们在service层中,写setDao方法,就可以直接通过接口调用Dao层,

用了注解写法后,在配置文件中不用再写Dao层的bean,

只需要在Dao实现类中加入

@Repository

public Class TestDaoImpl(){}

在service层定义

@Autowired

private TestDao testDao;

不需要再写setDao方法就可以通过接口调用Dao了。

本回答被网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网