2011-03-29 65 views
4

我已经继承了使用XML文件定义和连接Bean的Spring 3应用程序。我知道,自Spring 2以来,这些大多数都可以用注释替代。我想让Spring:将Spring应用程序从XML迁移到注释

  • 通过扫描特定的包类与任何注解是用来表示一个Spring bean
  • 尝试中,我已经打上一个bean自动装配按姓名和现场检测豆相关注释

我需要采取哪些步骤才能实现此目的?

回答

5

该手册有你需要的所有信息:http://static.springsource.org/spring/docs/3.1.0.M1/spring-framework-reference/html/beans.html#beans-annotation-config

的TL; DR的版本是,你需要添加<context:annotation-config/>到您的Spring配置,然后标注您的豆@Component并在二传手的性质与@Autowired注释

+0

陪同只是补充CarlosZ的答案。如果你正在创建一个新的组件,并且你想自动调用某个bean,那么你不必创建getter和setter,只需在属性声明中添加@Autowired注解,然后就可以开始了。 – 2011-03-29 17:07:29

+0

更正@Lucas,你可以做setter注入,属性注入(不需要setter)或者构造器注入(我个人最喜欢的,因为我认为当构造函数被调用时,包含依赖关系的对象应该被完全插入)。 – CarlosZ 2011-03-29 17:09:20

1

我认为首先要做的是慢下来。只需选择一项服务,然后根据您的理由进行移动即可。用大量的spring配置来Mu is是一种可靠的方式,让你在一段时间内只需要一次依赖就可以完成自己的生产。

0

我将分别anotation基于配置相当于给你与它的XML配置,让你会很容易看到如何你的bean从XML改变到Java,反之亦然

`<?xml version="1.0" encoding="UTF-8"?> 

     <beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:context="http://www.springframework.org/schema/context" 
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:p="http://www.springframework.org/schema/p" 
       xsi:schemaLocation=" 
       http://www.springframework.org/schema/beans  
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-4.0.xsd 
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 

      <context:component-scan base-package="com.mycompany.backendhibernatejpa.controller" /> 
      <mvc:annotation-driven /> 
      <bean id="iAbonneDao" class="com.mycompany.backendhibernatejpa.daoImpl.AbonneDaoImpl"/> 
      <bean id="iAbonneService" class="com.mycompany.backendhibernatejpa.serviceImpl.AbonneServiceImpl"/> 

      <!-- couche de persistance JPA --> 
      <bean id="entityManagerFactory" 
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
       <property name="dataSource" ref="dataSource" /> 
       <property name="jpaVendorAdapter"> 
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">    
         <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> 
         <property name="generateDdl" value="true" /> 
         <property name="showSql" value="true" /> 
        </bean> 
       </property> 
       <property name="loadTimeWeaver"> 
        <bean 
         class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /> 
       </property> 
      </bean> 

      <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
       <property name="locations" value="classpath:bd.properties"/> 
      </bean> 

      <!-- la source de donnéees DBCP --> 
      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" > 
       <property name="driverClassName" value="${bd.driver}" /> 
       <property name="url" value="${bd.url}" /> 
       <property name="username" value="${bd.username}" /> 
       <property name="password" value="${bd.password}" /> 
      </bean> 

      <!-- le gestionnaire de transactions --> 

      <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
       <property name="entityManagerFactory" ref="entityManagerFactory" /> 
      </bean> 


      <!-- traduction des exceptions --> 
      <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> 

      <!-- annotations de persistance --> 
      <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 


     </beans>` 

此文件被命名为springrest-sevlet。其实你可以给名称 你想跟着“-servlet”,并在文件中提到的名字 web.xml中

` <web-app> 
      <display-name>Gescable</display-name> 
      <servlet> 
       <servlet-name>springrest</servlet-name> 
       <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> 
       <load-on-startup>1</load-on-startup> 
      </servlet> 
      <servlet-mapping> 
       <servlet-name>springrest</servlet-name> 
       <url-pattern>/</url-pattern> 
      </servlet-mapping> 
     </web-app>` 

两个文件应在文件夹“WEB-INF地方”。 现在基于anotation配置

`/* 
     * To change this license header, choose License Headers in Project Properties. 
     * To change this template file, choose Tools | Templates 
     * and open the template in the editor. 
     */ 
     package com.mycompany.backendhibernatejpaannotation.configuration; 

     import com.mycompany.backendhibernatejpaannotation.daoImpl.AbonneDaoImpl; 
     import com.mycompany.backendhibernatejpaannotation.daoInterface.IAbonneDao; 
     import com.mycompany.backendhibernatejpaannotation.serviceImpl.AbonneServiceImpl; 
     import com.mycompany.backendhibernatejpaannotation.serviceInterface.IAbonneService; 
     import javax.sql.DataSource; 
     import org.springframework.context.annotation.Bean; 
     import org.springframework.context.annotation.ComponentScan; 
     import org.springframework.context.annotation.Configuration; 
     import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; 
     import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver; 
     import org.springframework.jdbc.datasource.DriverManagerDataSource; 
     import org.springframework.orm.jpa.JpaTransactionManager; 
     import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 
     import org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor; 
     import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; 
     import org.springframework.web.servlet.config.annotation.EnableWebMvc; 

     /** 
     * 
     * @author vivien saa 
     */ 
     @Configuration 
     @EnableWebMvc 
     @ComponentScan(basePackages = "com.mycompany.backendhibernatejpaannotation") 
     public class RestConfiguration { 

      @Bean 
      public IAbonneDao iAbonneDao() { 
       return new AbonneDaoImpl(); 
      } 

      @Bean 
      public IAbonneService iAbonneService() { 
       return new AbonneServiceImpl(); 
      } 

     // @Bean 
     // public PropertyPlaceholderConfigurer placeholderConfigurer() { 
     //  PropertyPlaceholderConfigurer placeholderConfigurer = new PropertyPlaceholderConfigurer(); 
     //  placeholderConfigurer.setLocations("classpath:bd.properties"); 
     //  return placeholderConfigurer; 
     // } 

      @Bean 
      public DataSource dataSource() { 
       DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
       dataSource.setDriverClassName("com.mysql.jdbc.Driver"); 
       dataSource.setUrl("jdbc:mysql://localhost:3306/gescable"); 
       dataSource.setUsername("root"); 
       dataSource.setPassword("root"); 
       return dataSource; 
      } 

      @Bean 
      public HibernateJpaVendorAdapter jpaVendorAdapter() { 
       HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); 
       jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQL5InnoDBDialect"); 
       jpaVendorAdapter.setGenerateDdl(true); 
       jpaVendorAdapter.setShowSql(true); 
       return jpaVendorAdapter; 
      } 

      @Bean 
      public InstrumentationLoadTimeWeaver loadTimeWeaver() { 
       InstrumentationLoadTimeWeaver loadTimeWeaver = new InstrumentationLoadTimeWeaver(); 
       return loadTimeWeaver; 
      } 

      @Bean 
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() { 
       LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean(); 
       entityManagerFactory.setDataSource(dataSource()); 
       entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter()); 
       entityManagerFactory.setLoadTimeWeaver(loadTimeWeaver()); 
       return entityManagerFactory; 
      } 

      @Bean 
      public JpaTransactionManager jpaTransactionManager() { 
       JpaTransactionManager jpaTransactionManager = new JpaTransactionManager(); 
       jpaTransactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); 
       return jpaTransactionManager; 
      } 

      @Bean 
      public PersistenceExceptionTranslationPostPro`enter code here`cessor persistenceExceptionTranslationPostProcessor() { 
       return new PersistenceExceptionTranslationPostProcessor(); 
      } 

      @Bean 
      public PersistenceAnnotationBeanPostProcessor persistenceAnnotationBeanPostProcessor() { 
       return new PersistenceAnnotationBeanPostProcessor(); 
      } 

     }` 

这个文件相当于必须通过这一个

`    
     package com.mycompany.backendhibernatejpaannotation.configuration; 

     import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 

     /** 
     * 
     * @author vivien saa 
     */ 
     public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

      @Override 
      protected Class<?>[] getRootConfigClasses() { 
       return new Class[]{RestConfiguration.class}; 
      } 

      @Override 
      protected Class<?>[] getServletConfigClasses() { 
       return null; 
      } 

      @Override 
      protected String[] getServletMappings() { 
       return new String[]{"/"}; 
      } 

     } 
     `