gpt4 book ai didi

java - Spring 注释 : Converting XML Configuration to Annotation

转载 作者:行者123 更新时间:2023-11-30 08:27:43 25 4
gpt4 key购买 nike

我在我的 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") 中突出显示,转换为注释。

我的转换代码如下:

@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' 实例应该用 AmericasDataSource 注入(inject),而 'emeadao' 应该用 EMEADataSource 注入(inject),该怎么做?

请注意,我在 SkuDAOImpl 中没有 setter。 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 成为一个 bean(这些注释可以使用 bean 的名称作为值(value))
  • 使用 @Autowired 在字段上注入(inject)依赖项,如果有多个对应的 bean(在您的情况下,您有两个 DataSource),添加一个 @Qualifier 指定哪一个。

完整文档 here .

关于java - Spring 注释 : Converting XML Configuration to Annotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20697183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com