Spring注释:将XML配置转换为注释

时间:2013-12-20 05:51:54

标签: java spring spring-annotations

我在Spring应用程序中使用xml配置。现在我想将现有的类转换为使用注释(如@service,@ Repository等)而不是xml配置。

业务逻辑(与此问题无关,仅供理解):  服务连接到美洲数据库并找到skus(产品)并停用skus。  服务连接到EMEA数据库并找到skus(产品)并停用skus。

以下是示例代码。

/* Service code, which has 2 instances of SkuDAO, one connecting to US database and one connecting to EMEA database */

public class DeactivationService {

    private static final Logger LOG = Logger.getLogger(DeactivationService.class);

    private SkuDAO amerdao; //Dependency Injection Amer
    private SkuDAO emeadao; //Dependency Injection EMEA

    public DeactivationService(SkuDAO amerdao,SkuDAO emeadao) {
        this.amerdao=amerdao;
        this.emeadao=emeadao;
    }

    /*
     * Step 1: find inactive sku in americas skudao1.find()
     * Step 2: find inactive sku in emea skudao2.find()
     * Step 3: deactivate sku in americas
     * Step 4: deactivate sku in emea
 */
    public void deactivateSku() {
        List<Sku> totalList = new ArrayList<Sku>();
        List<Sku> amerList = amerdao.find();
        List<Sku> emeaList = emeadao.find();
              amerdao.deactivate(amerList);
        emeaList.deactivate(emeaList);
        }

}

/* DAO interface */

public interface SkuDAO {
     public List<Sku> find();
     public void deactivate(List<Sku>);
}

/* DAO Implementation
   Here one constructor in which DataSource is injected

 */

    public class SkuDAOImpl implements SkuDAO {

        private DataSource datasource; //Dependency injection
        private JdbcTemplate jdbcTemplate;

        public SkuDAOImpl(DataSource datasource) {
            this.datasource=datasource;
        }

        public List<Sku> find() {
            //some processing to find the sku, purposely left empty as it is a sample code
        }

        public void deactivate(List<Sku>) {
            //some processing to deactivate the sku, purposely left empty as it is a sample code
            }
    }

Spring配置:

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

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

    <context:property-placeholder location="file:${dbconfiguration}"/>

    <bean id="AmericasDataSource" class="dell.harmony.data.HarmonyBasicDataSource" destroy-method="close" >
       <property name="url"><value>${HarmonyAmericasDb.url}</value></property>
       <property name="driverClassName"><value>${HarmonyAmericasDb.driverClassName}</value></property>
       <property name="username"><value>${HarmonyAmericasDb.username}</value></property>
       <property name="password"><value>${HarmonyAmericasDb.password}</value></property>
       <property name="initialSize"><value>${HarmonyAmericasDb.initialSize}</value></property>
       <property name="maxActive"><value>${HarmonyAmericasDb.maxActive}</value></property>
       <property name="maxWait"><value>${HarmonyAmericasDb.maxWait}</value></property>
       <property name="maxIdle"><value>${HarmonyAmericasDb.maxIdle}</value></property>
       <property name="minIdle"><value>${HarmonyAmericasDb.minIdle}</value></property>
       <property name="removeAbandoned"><value>${HarmonyAmericasDb.removeAbandoned}</value></property>
       <property name="removeAbandonedTimeout"><value>${HarmonyAmericasDb.removeAbandonedTimeout}</value></property>
    </bean>

    <bean id="EMEADataSource" class="dell.harmony.data.HarmonyBasicDataSource" destroy-method="close" >
       <property name="url"><value>${HarmonyEMEADb.url}</value></property>
       <property name="driverClassName"><value>${HarmonyEMEADb.driverClassName}</value></property>
       <property name="username"><value>${HarmonyEMEADb.username}</value></property>
       <property name="password"><value>${HarmonyEMEADb.password}</value></property>
       <property name="initialSize"><value>${HarmonyEMEADb.initialSize}</value></property>
       <property name="maxActive"><value>${HarmonyEMEADb.maxActive}</value></property>
       <property name="maxWait"><value>${HarmonyEMEADb.maxWait}</value></property>
       <property name="maxIdle"><value>${HarmonyEMEADb.maxIdle}</value></property>
       <property name="minIdle"><value>${HarmonyEMEADb.minIdle}</value></property>
       <property name="removeAbandoned"><value>${HarmonyEMEADb.removeAbandoned}</value></property>
       <property name="removeAbandonedTimeout"><value>${HarmonyEMEADb.removeAbandonedTimeout}</value></property>

    </bean>

     **<!--  Sku Deactivation  -->**
     <bean id="SkuAmerDao" class="dell.harmony.service.skudeactivation.dao.SkuDAOImpl">
        <constructor-arg index="0"><ref bean="AmericasDataSource"/></constructor-arg>
     </bean>

     <bean id="SkuEMEADao" class="dell.harmony.service.skudeactivation.dao.SkuDAOImpl">
        <constructor-arg index="0"><ref bean="EMEADataSource"/></constructor-arg>
     </bean>

     <bean id="ServiceManager" class="dell.harmony.service.skudeactivation.service.DeactivationService">
        <constructor-arg index="0"><ref bean="SkuAmerDao"/></constructor-arg>
        <constructor-arg index="1"><ref bean="SkuEMEADao"/></constructor-arg>
     </bean>    

</beans>

现在我想将上面的类转换为xml(“Sku Deactivation”)中的突出显示为annodation。

我的转换代码如下:

@Service
public class DeactivationService {

    private static final Logger LOG = Logger.getLogger(DeactivationService.class);

    private SkuDAO amerdao; //Dependency Injection Amer
    private SkuDAO emeadao; //Dependency Injection EMEA

    @Autowired(required=true)
    public DeactivationService( @Qualifier("SkuAmerDao") SkuDAO amerdao, @Qualifier("SkuEMEADao") SkuDAO emeadao) {
        this.amerdao=amerdao;
        this.emeadao=emeadao;
    }

}

在上面的构造函数中,现在'amerdao'实例,应该用EMEADataSource注入AmericasDataSource和'emeadao',怎么做?

请注意,我在SkuDAOImpl中没有一个二传手。此外,SkuDAOImpl中只有一个数据源实例。

  1. 你能否给出带有日期的SkuDAOImpl示例代码。
  2. 任何建议,如果可以更好的方式完成从服务到dao的编码。 (不要求回答这个问题)
  3. 现在编辑:为了清楚问题1,我想在Spring xml中删除以下两行,并使用注释代替我的DeactivationService。有可能吗?

     <bean id="SkuAmerDao" class="dell.harmony.service.skudeactivation.dao.SkuDAOImpl">
        <constructor-arg index="0"><ref bean="AmericasDataSource"/></constructor-arg>
     </bean>
    
     <bean id="SkuEMEADao" class="dell.harmony.service.skudeactivation.dao.SkuDAOImpl">
        <constructor-arg index="0"><ref bean="EMEADataSource"/></constructor-arg>
     </bean>
    

1 个答案:

答案 0 :(得分:3)

怎么样:

@Service
public class DeactivationService {

    private static final Logger LOG = Logger.getLogger(DeactivationService.class);

    @Autowired  
    @Qualifier("SkuAmerDao")
    private SkuDAO amerdao; //Dependency Injection Amer

    @Autowired 
    @Qualifier("SkuEMEADao")
    private SkuDAO emeadao; //Dependency Injection EMEA

    // no constructor needed.
}

public abstract class BaseDao implements SkuDAO {

    private final JdbcTemplate jdbcTemplate;       

    protected BaseDao() {
        this.jdbcTemplate = new JdbcTemplate(getDataSource());
    }

    protected abstract DataSource getDataSource();

    public List<Sku> find() {
        //some processing to find the sku, purposely left empty as it is a sample code
    }

    public void deactivate(List<Sku>) {
        //some processing to deactivate the sku, purposely left empty as it is a sample code
    }
}

@Repository("SkuAmerDao")
public class SkuAmerDAOImpl extends BaseDao {
    @Autowired 
    @Qualifier("AmericasDataSource")
    private DataSource datasource; //Dependency injection

    @Override
    protected DataSource getDatasource() {
        return dataSource;
    }
}

@Repository("SkuEMEADao")
public class SkuEMEADAOImpl extends BaseDao {
    @Autowired 
    @Qualifier("EMEADataSource")
    private DataSource datasource; //Dependency injection

    @Override
    protected DataSource getDatasource() {
        return dataSource;
    }
}

始终遵循相同的原则:

  • 类是通过注释@Service@Component@Repository创建的bean(这些注释可以将bean的名称作为值)
  • @Autowired的字段进行依赖注入,如果有多个相应的bean(在您的情况下,您有两个DataSource),请添加@Qualifier以指定哪个之一。

完整文档here