公司招人去哪个网站,南昌p2p网站专业建设,淘宝城购物中心,免费的wordpress模板下载虽然我们可以通过 Autowired 在 Bean 类中使用自动注入功能#xff0c;但是 Bean 还是在 applicatonContext.xml 文件中通过 bean 进行定义 —— 在前面的例子中#xff0c;我们还是在配置文件中定义 Bean#xff0c;通过 Autowired为 Bean 的成员变量、方法形参或构…虽然我们可以通过 Autowired 在 Bean 类中使用自动注入功能但是 Bean 还是在 applicatonContext.xml 文件中通过 bean 进行定义 —— 在前面的例子中我们还是在配置文件中定义 Bean通过 Autowired为 Bean 的成员变量、方法形参或构造函数形参提供自动注入的功能。 那么能不是也可以通过注解定义 Bean从 XML 配置文件中完全移除 Bean 定义的配置呢 答案是肯定的我们通过 Spring 2.5 提供的 Component 注释就可以达到这个目标了。 修改Bean的java类的代码如下在类名前面加上 Component注解 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 org.springframework.stereotype.Component; 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; Componentpublic 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(){ } //这里是业务逻辑的方法 。。。。。}然后我们修改配置文件applicatonContext.xml中启用自动注入的功能而放弃原来的bean方式的配置 ?xml version1.0 encodingUTF-8?beans xmlnshttp://www.springframework.org/schema/beans xmlns:contexthttp://www.springframework.org/schema/context xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:txhttp://www.springframework.org/schema/tx xsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd default-autowireautodetect bean identityManagerFactory classorg.springframework.orm.jpa.LocalEntityManagerFactoryBean property namepersistenceUnitName valuetesterPU / /bean bean idtransactionManager classorg.springframework.orm.jpa.JpaTransactionManager property nameentityManagerFactory refentityManagerFactory / /bean tx:annotation-driven transaction-managertransactionManager / bean idtransactionInterceptor classorg.springframework.transaction.interceptor.TransactionInterceptor !-- 事务拦截器bean需要依赖注入一个事务管理器 -- property nametransactionManager ref localtransactionManager / /property property nametransactionAttributes !-- 下面定义事务指service里面的方法传播属性 -- props prop keyinsert*PROPAGATION_REQUIRED/prop prop keyupdate*PROPAGATION_REQUIRED/prop prop keysave*PROPAGATION_REQUIRED/prop prop keyadd*PROPAGATION_REQUIRED/prop prop keyupdate*PROPAGATION_REQUIRED/prop prop keyremove*PROPAGATION_REQUIRED/prop prop keydelete*PROPAGATION_REQUIRED/prop prop keyget*PROPAGATION_REQUIRED,readOnly /prop prop keyfind*PROPAGATION_REQUIRED,readOnly /prop prop keyload*PROPAGATION_REQUIRED,readOnly /prop prop keychange*PROPAGATION_REQUIRED/prop prop keycount*PROPAGATION_REQUIRED/prop prop key*PROPAGATION_REQUIRED/prop /props /property /bean !-- 该 BeanPostProcessor 将自动对标注 Autowired 的 Bean 进行注入 -- !-- 这个Processor 已经被 context:annotation-config/ 所简化 bean classorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor/ -- !-- context:component-scan/ 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能同时还启用了注释驱动自动注入的功能即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor因此当使用 context:component-scan/ 后就可以将 context:annotation-config/ 移除了。 -- context:component-scan base-package com.firemax/ !-- 定义自动代理BeanNameAutoProxyCreator -- bean idbeanNameAutoProxyCreator classorg.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator !-- 指定对满足哪些bean name的bean自动生成业务代理 -- property namebeanNames list value*Service/value /list /property !-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器 -- property nameinterceptorNames list !-- 此处可增加其他新的Interceptor -- valuetransactionInterceptor/value /list /property /bean !-- bean idAlcorTCountriesDAO classcom.firemax.test.hibernate.AlcorTCountriesDAO property nameentityManagerFactory refentityManagerFactory / /bean bean idAlcorTProvincesDAO classcom.firemax.test.hibernate.AlcorTProvincesDAO property nameentityManagerFactory refentityManagerFactory / /bean bean idAlcotTDistrictDAO classcom.firemax.test.hibernate.AlcotTDistrictDAO property nameentityManagerFactory refentityManagerFactory / /bean bean idAlcorTCitysDAO classcom.firemax.test.hibernate.AlcorTCitysDAO property nameentityManagerFactory refentityManagerFactory / /bean bean idCountryService classcom.firemax.test.service.CountryService/ --/beans 新的applicaitonContext.xml 配置文件中蓝色的部分就是原来的bean的注入方式现在已经给屏蔽了。不需要再写。而红色部分就是使用了context:component-scan base-package com.firemax/ 让spirng自动搜索然后注入。 注意 这里注入的bean 的名称是按照类的名称把第一个字母改成小写来命名的。比如例子中的CountryService的bean的名称就是countryService.我们也可以通过Component(countryService) 这种方式来显示的定义一个bean的注入名称。但是在大多数情况下没有必要。 context:component-scan/ 的 base-package 属性指定了需要扫描的类包类包及其递归子包中所有的类都会被处理。 context:component-scan/ 还允许定义过滤器将基包下的某些类纳入或排除。Spring 支持以下 4 种类型的过滤方式通过下表说明 扫描过滤方式过滤器类型说明注释假如 com.firemax.test.SomeAnnotation 是一个注释类我们可以将使用该注释的类过滤出来。类名指定通过全限定类名进行过滤如您可以指定将 com.firemax.test.IncludeService纳入扫描而将 com.firemax.test.NotIncludeService 排除在外。正则表达式通过正则表达式定义过滤的类如下所示 com/.firemax/.test/.Default.*AspectJ 表达式通过 AspectJ 表达式定义过滤的类如下所示 com. firemax.test..*Service下面是一个简单的例子 context:component-scan base-packagecom.firemax context:include-filter typeregex expressioncom/.firemax/.test/.service/..*/ context:exclude-filter typeaspectj expressioncom.firemax.test.util..*//context:component-scan 默认情况下通过 Component 定义的 Bean 都是 singleton 的如果需要使用其它作用范围的 Bean可以通过 Scope 注释来达到目标如以下代码所示 通过 Scope 指定 Bean 的作用范围 package com.firemax.tester.service;import org.springframework.context.annotation.Scope;…Scope(prototype)Component(countryService)public class CountryService{ …} 这样当从 Spring 容器中获取 boss Bean 时每次返回的都是新的实例了。 在Spring2.5中引入了更多的典型化注解Repository ServiceControler是Component的细化。分别表示持久层服务层控制层。建议使用这个注解来取代Component 附上一个java Applicaiton的简单测试程序 /* * Created on 2008-9-28 * * 徐泽宇 roamer */package com.firemax.test.tester; import java.util.Iterator; import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext; import com.firemax.test.hibernate.AlcorTCitys;import com.firemax.test.hibernate.AlcorTCitysDAO;import com.firemax.test.hibernate.AlcorTCountries;import com.firemax.test.hibernate.AlcorTProvinces;import com.firemax.test.hibernate.AlcotTDistrict;import com.firemax.test.hibernate.AlcotTDistrictDAO;import com.firemax.test.service.CountryService; public class Tester { /** * param args */ public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub ApplicationContext ctx new FileSystemXmlApplicationContext(/WebContent/WEB-INF/classes/applicationContext*.xml); String[] beans ctx.getBeanDefinitionNames(); for (int i 0 ; i beans.length;i) { System.out.println(beans[i]); } CountryService countryService (CountryService)ctx.getBean(countryService); AlcorTCountries alcorTCountries countryService.getCountriesInfo(CN); System.out.println(alcorTCountries.getCountry()); System.out.println(开始调用子类); System.out.println(alcorTCountries.getAlcorTProvinceses().size()); IteratorAlcorTProvinces it alcorTCountries.getAlcorTProvinceses().iterator(); while (it.hasNext()){ AlcorTProvinces alcorTProvinces (AlcorTProvinces)it.next(); System.out.println(alcorTProvinces.getProvinceName()); } AlcotTDistrict alcotTDistrict new AlcotTDistrict(); alcotTDistrict.setDistrictCode(22); alcotTDistrict.setDistrictName(浦东); AlcorTCitys alcorTCitys countryService.getCityInfo(021); alcotTDistrict.setAlcorTCitys(alcorTCitys); countryService.saveProvinces(alcotTDistrict); }} 在所有的JPOPO中我们可以使用Entity 这个注解来注入 JOPO。这样我们在 Persistent.xml中就不需要在 定义哪些POJO的类了。 感谢 JPA 感谢Spring 终于不要频繁的去定义和修改这些Bean了 原文网址http://blog.csdn.net/remote_roamer/article/details/3008016 转载于:https://www.cnblogs.com/winkey4986/archive/2012/02/16/2355023.html