java开发中spring注解的使用方法?

如题所述

注解方式配置(简化XML配置文件)
组件自动扫描功能
首先需要在applicationContext.xml中添加<context:component-scan/>
a.扫描Bean组件的注解,替代xml中的<bean>元素的定义。
@Service 用于Service业务组件
@Control 用于Action控制组件
@Respository 用于DAO数据访问组件
@Component 用于其他组件
Bean组件扫描到容器后,默认名字为类名(首字母小写)
如果需要自定义名称可以使用@Service("id名")
b.依赖注入的注解标记
@Resource 按名称@Resource(name="id名")
@AutoWired 按名称
@Autowired
@Qualifier("id名")
c.其他注解
@Scope 等价于<bean scope="">
@PostConstruct 等价于<bean init-method="">
@PreDestroy 等价于<bean destroy-method="">
温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-01-18

注解使用方法如图所示:

第2个回答  2016-01-09
首先你要导入相应的spring的jar包,然后在类或者方法前面加@加上你的注解名称。如
@Controller
public class ArchiveAction extends BaseAction {
。。。。。。

}