簡體   English   中英

Spring注釋:將XML配置轉換為注釋

[英]Spring Annotation : Converting XML Configuration to Annotation

我在Spring應用程序中使用xml配置。 現在我想將現有的類轉換為使用注解(例如@ service,@ Repository等)而不是xml配置。

業務邏輯(此問題僅作理解之用,僅代表理解):服務連接到Americas數據庫並找到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
            }
    }

彈簧配置:

<?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”)中突出顯示的內容。

我的轉換代碼如下:

@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;
    }

}

在上面的構造函數中,現在應該使用AmericasDataSource注入“ amerdao”實例,並使用EMEADataSource注入“ emeadao”實例,該怎么做?

請注意,我在SkuDAOImpl中沒有二傳手。 此外,SkuDAOImpl內部只有一個數據源實例。

  1. 您是否可以通過注釋給出SkuDAOImpl的示例代碼。
  2. 如果可以通過更好的方式來完成從服務到dao的編碼的任何建議。 (無需回答)

現在編輯:為了清楚地回答問題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>

關於什么:

@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 @Component@Repository制成bean(這些注釋可以將bean的名稱作為值)
  • 在具有@Autowired字段上進行依賴注入,如果有多個對應的bean(在您的情況下,您有兩個DataSource ),則添加一個@Qualifier來指定哪個。

完整的文檔在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM