Spring 2.5 JPA hibernate 使用方法的点滴整理(三):通过 @Autowired的使用来消除 set ,get方法.
<script></script> 通过
(一),
(二)的介绍。我们已经做了初步的简化程序。
但是在我们编写spring 框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量。并且要配套写上 get 和 set方法。虽然 可以通过eclipse等工具来自动生成。但是还是会引起程序阅读性上的不便。那么既然注解这么强大。是否可以也把他精简掉呢?
当然可以。这个标签就是@Autowired
Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。
要实现我们要精简程序的目的。需要这样来处理:
-
在applicationContext.xml中加入:
-
<!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->
-
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
- 修改在原来注入spirng容器中的bean的方法。在域变量上加上标签@Autowired,并且去掉 相应的get 和set方法
-
-
-
-
-
-
package com.firemax.test.service;
-
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;
-
import org.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 {
-
private static Log logger = LogFactory.getLog(CountryService.class);
-
@Autowired
-
private AlcorTCountriesDAO alcorTCountriesDAO;
-
@Autowired
-
private AlcorTProvincesDAO alcorTProvincesDAO;
-
@Autowired
-
private AlcorTCitysDAO alcorTCitysDAO;
-
@Autowired
-
private AlcotTDistrictDAO alcotTDistrictDAO;
-
-
public CountryService(){
-
-
}
-
-
-
-
-
- public void updateCountry(AlcorTCountries alcorTCountries ) throws Exception{
-
this.alcorTCountriesDAO.update(alcorTCountries);
-
}
-
-
....
-
-
-
-
}
- 在applicatonContext.xml中 把原来 引用的<porpery >标签也去掉。
-
<bean id="CountryService" class="com.firemax.test.service.CountryService">
-
<property name="alcorTCountriesDAO" ref="AlcorTCountriesDAO" />
-
<property name="alcorTProvincesDAO" ref="AlcorTProvincesDAO" />
-
<property name="alcorTCitysDAO" ref="AlcorTCitysDAO" />
-
<property name="alcotTDistrictDAO" ref="AlcotTDistrictDAO" />
- </bean>
修改成
-
<bean id="CountryService" class="com.firemax.test.service.CountryService">
-
- </bean>
当然,我们也可以在构造函数上使用@Auwowired 注解 。如果构造函数有两个入参,分别是 bean1 和 bean2,@Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 CountryService (Bean1 bean1 ,Bean2 bean2) 的入参来创建 CountryService Bean。
在运行一下你的业务程序。如果没有错误。恭喜你。这个步骤我们又完成了。
分享到:
相关推荐
基于 spring2.5 hibernate3.3 jpa 的annotation注解来减少配置文件的大小 通过jersey实现restful形式请求
Struts2+Spring2.5+Hibernate3(JPA)+ExtJS3基本后台
一、整合开发时Hibernate、Spring需要的JAR文件。 hibernate核心安装包下的(下载路径:http://www.hibernate.org/,点击“Hibernate Core”右边的“Downloads”): hibernate3.jar lib\bycode\cglib\hibernate-...
Struts1.3 spring2.5 JPA 所需jia包
Spring2.5整合JPA(Hibernate实现)所需的JAR包
这是jsf+spring2.5+jpa(hibernate)的jar包,很多人为了jsj环境而配置半天,在此提供jar包共享。注:除了ajax4jsf和tomahawk-1.1.3.jar,因为csdn只让我上传20mb,大家自己可以下一下自己试试。
Struts1.3+spring2.5+JPA(hibernate) 实现的一个小示例,比较适合初学者和整合配置查询。
Java Web高级编程 涵盖WebSockets、Spring Framework、JPA Hibernate和Spring Security, 有目录,很清晰
spring注解完整版+spring data jpa官方文档中文翻译+JPA2.0官方文档 文档内容齐全 值得参考学习
Spring2.5整合JPA实例 博文链接:https://zmx.iteye.com/blog/556452
这是整合SpringMVC+Spring+SpringDataJPA+Hibernate简单的实现登录的功能,用的是mysql数据库,这是一个web Project 如果你用的是JavaEE6那么你要注意bean-validator.jar和weld-osgi-bundle.jar与slf4j的jar包冲突。...
Maven整合Spring+SpringMVC+Hibernate+SpringDataJPA
5-Spring-Boot(五):spring data jpa的使用.docx5-Spring-Boot(五):spring data jpa的使用.docx5-Spring-Boot(五):spring data jpa的使用.docx5-Spring-Boot(五):spring data jpa的使用.docx5-Spring-Boot(五):...
Spring 2.0的'spring-jdo.jar', 'spring-jpa.jar', 'spring-hibernate3.jar', 'spring-toplink.jar' 和 'spring-ibatis.jar' 被合并到Spring 2.5大粒度的'spring-orm.jar'中。 Spring 2.5的 'spring-test.jar' 取代...
oa系统_ssh2_Struts2+spring2.5+JPA) oa系统_ssh2_Struts2+spring2.5+JPA) oa系统_ssh2_Struts2+spring2.5+JPA) oa系统_ssh2_Struts2+spring2.5+JPA)
Spring Data JPA API。 Spring Data JPA 开发文档。 官网 Spring Data JPA API。
这是Spring2.5集成JPA的有关事务的例子,挺实用的哦!
基础框架,可满足开发需要
使用注解进行开发: struts2注解(去掉了struts.xml)配置文件 spring注解(去掉了在XML文件中bean的配置) jpa注解(去掉了*.hbm.xml)文件的配置 该项目完全可以运行,包含了所有的JAR包,数据库默认使用ORACLE,MYSQL...
Struts 2 + Spring 2 + JPA + hibernate + AJAX +实例, 包含依赖jar包。