@Autowired
来源:互联网 发布:mysql root权限 编辑:程序博客网 时间:2024/06/05 02:37
我们编写spring 框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean都建议定义成私有的域变量。并且要配套写上 get 和set方法。虽然可以通过eclipse等工具来自动生成。但是还是会引起程序阅读性上的不便。那么既然注解这么强大。是否可以也把他精简掉呢?
当 然可以。这个标签就是@Autowired
Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。
要实现我们要精简程序的目的。需要这样来处理:
* 在applicationContext.xml中加入:
* 修改在原来注入spirng容器中的bean的方法。
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
importorg.springframework.beans.factory.annotation.Autowired;
import com.firemax.test.hibernate.AlcorTCitys;
import com.firemax.test.hibernate.AlcorTCitysDAO;
import com.firemax.test.hibernate.AlcorTCountries;
import com.firemax.test.hibernate.AlcorTCountriesDAO;
import com.firemax.test.hibernate.AlcorTProvinces;
import com.firemax.test.hibernate.AlcorTProvincesDAO;
import com.firemax.test.hibernate.AlcotTDistrict;
import com.firemax.test.hibernate.AlcotTDistrictDAO;
public class CountryService {
}
* 在applicatonContext.xml中 把原来 引用的<porpery>标签也去掉。
修改成
当然,我们也可以在构造函数上使用@Auwowired 注解 。如果构造函数有两个入参,分别是 bean1 和bean2,@Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 CountryService (Bean1bean1 ,Bean2 bean2) 的入参来创建 CountryService Bean。
- @Autowired
- @Autowired
- Autowired
- @Autowired
- @Autowired
- @autowired
- @Autowired
- @Autowired
- @Autowired
- @AutoWired
- @Autowired
- @Autowired example
- @Autowired注解
- [spring]@Autowired
- @Autowired用法
- @Autowired认识
- Spring @Autowired
- @Autowired spring
- CKFinder 2.4.1清除主界面中的版权信息
- SURF、SIFT、Fast角点检测的代码(opencv)
- iOS开发系列--打造自己的“美图秀秀”
- java io的理解
- 养成常写博客的习惯
- @Autowired
- 一起来看看如何用JMX连接本地JVM上运行的Java程序
- 第十一周 阅读程序(2)
- 微信企业号开发之-如何获取secret 序列号
- python学习笔记(一)
- ubuntu下eclipse新建项目没有java project的解决办法
- 学会记录生活的每件小事
- 历史消息
- Apache apxs 扩展工具的安装