0

我在将 Spring 3.0 组件扫描功能与 JBoss 5.1.2 一起使用时面临挑战。我无法使用@Autowired 功能。以下是我配置的代码。

我的 Web.xml

 <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/spring-config/applicationContext.xml</param-value>
</context-param>
   <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

applicationContext.xml 由以下行组成,我已在文件中导入了 beans,context xsd。applicationContext-hibernate.xml 文件包含 Hibernate 配置详细信息。

    <ctx:component-scan base-package="com.mycom.cmc" />
    <import resource="applicationContext-hibernate.xml" />

下面是我用来做 DAO 操作的类。

@Repository("batchProcessingTxDAO")
 public class BatchProcessingTxDAOImpl extends HibernateRepository implements
    BatchProcessingTxDAO {

private static final Logger log = Logger
        .getLogger(BatchProcessingTxDAOImpl.class);

@Override
public void saveBatchProcessingTx(BatchProcessingTx batchProcessingTx) {

    try {
        if (batchProcessingTx != null) {
            log.info(String
                    .format("saving BatchProcessingTx details for batchProcessingTxId :: %s",
                            batchProcessingTx.getBatchProcessingTxId()));
            hibernateTemplate.save(batchProcessingTx);
        }
    } catch (Exception e) {
        log.error(String
                .format("Exception occured while saving the BatchProcessingTx caused by :: %s",
                        e));

    }
    log.info(String
            .format("Sucessfully saved BatchProcessingTx details for batchProcessingTxId ::%s",
                    batchProcessingTx.getBatchProcessingTxId()));

}

@Override
public BatchProcessingTx getBatchProcessingTxByBatchProcessingTxId(
        BigDecimal batchProcessingTxId) {
    System.out.println("called Sucessfully 1212121111111111111111111");
    BatchProcessingTx batchProcessingTx = null;
   /*
    if (batchProcessingTxId != null) {
        log.info(String.format(
                "BatchProcessingTx details for batchProcessingTxId :: %s",
                batchProcessingTxId));
        batchProcessingTx = hibernateTemplate.get(BatchProcessingTx.class,
                batchProcessingTxId);
    }
     */
    return batchProcessingTx;

}

}

public abstract class HibernateRepository {

private SessionFactory sessionFactory;
protected HibernateTemplate hibernateTemplate;

@Required
@Autowired(required =true)
@Qualifier("sessionFactory")
public void setSessionFactory(final SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
    hibernateTemplate = new HibernateTemplate(sessionFactory);
}

protected Session getSession() {
    return sessionFactory.getCurrentSession();
}

}

当我在 JBoss 容器上部署应用程序时,我可以看到 .hbm 文件并且 JNDI 连接正常进行。但我看不到该类正在服务器日志 JBoss Container 中进行扫描。

19:49:54,958 INFO  [[/CMC]] Initializing Spring root WebApplicationContext
19:49:55,598 INFO  [HbmBinder] Mapping class: com.myapp.cmc.domain.model.BatchProcessingTx -> BATCH_PROCESSING_TX
19:49:55,598 INFO  [HbmBinder] Mapping class: com.myapp.cmc.domain.model.BrandMapping -> BRAND_MAPPING
19:49:55,614 INFO  [HbmBinder] Mapping class: com.myapp.cmc.domain.model.ClientAccessRule -> CLIENT_ACCESS_RULE
19:49:55,614 INFO  [HbmBinder] Mapping class: com.myapp.cmc.domain.model.ContractInfo -> CONTRACT_INFO
19:49:55,630 INFO  [HbmBinder] Mapping class: com.myapp.cmc.domain.model.ExceptionInfo -> EXCEPTION_INFO
19:49:55,630 INFO  [HbmBinder] Mapping class: com.myapp.cmc.domain.model.PdfDocumentMapping -> PDF_DOCUMENT_MAPPING
19:49:55,645 INFO  [HbmBinder] Mapping class: com.myapp.cmc.domain.model.ReferenceInfo -> REFERENCE_INFO
19:49:55,645 INFO  [HbmBinder] Mapping class: com.myapp.cmc.domain.model.WebServiceTx -> WEB_SERVICE_TX
19:49:55,645 INFO  [HbmBinder] Mapping collection: com.myapp.cmc.domain.model.ContractInfo.batchProcessingTxes -> BATCH_PROCESSING_TX
19:49:55,645 INFO  [HbmBinder] Mapping collection: com.myapp.cmc.domain.model.ContractInfo.exceptionInfos -> EXCEPTION_INFO
19:49:55,645 INFO  [HbmBinder] Mapping collection: com.myapp.cmc.domain.model.PdfDocumentMapping.contractInfosForFairUsePolicyId -> CONTRACT_INFO
19:49:55,645 INFO  [HbmBinder] Mapping collection: com.myapp.cmc.domain.model.PdfDocumentMapping.contractInfosForTermsOfServiceId -> CONTRACT_INFO
19:49:55,645 INFO  [HbmBinder] Mapping collection: com.myapp.cmc.domain.model.PdfDocumentMapping.contractInfosForPrivacyPolicyId -> CONTRACT_INFO
19:49:55,645 INFO  [HbmBinder] Mapping collection: com.myapp.cmc.domain.model.PdfDocumentMapping.contractInfosForAddedClausesId -> CONTRACT_INFO
19:49:55,645 INFO  [HbmBinder] Mapping collection: com.myapp.cmc.domain.model.ReferenceInfo.contractInfosForContractReasonId -> CONTRACT_INFO
19:49:55,645 INFO  [HbmBinder] Mapping collection: com.myapp.cmc.domain.model.ReferenceInfo.contractInfosForContractTypeId -> CONTRACT_INFO
19:49:55,645 INFO  [HbmBinder] Mapping collection: com.myapp.cmc.domain.model.ReferenceInfo.contractInfosForContractMediaId -> CONTRACT_INFO
19:49:55,645 INFO  [HbmBinder] Mapping collection: com.myapp.cmc.domain.model.ReferenceInfo.contractInfosForContractDistributionTypeId -> CONTRACT_INFO
19:49:55,645 INFO  [HbmBinder] Mapping collection: com.myapp.cmc.domain.model.WebServiceTx.contractInfos -> CONTRACT_INFO
19:49:55,661 INFO  [ConnectionProviderFactory] Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider
19:49:55,661 INFO  [SettingsFactory] RDBMS: Oracle, version: Oracle Database 10g     Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
19:49:55,661 INFO  [SettingsFactory] JDBC driver: Oracle JDBC driver, version: 10.1.0.4.0
19:49:55,661 INFO  [Dialect] Using dialect: org.hibernate.dialect.Oracle10gDialect
19:49:55,661 INFO  [JdbcSupportLoader] Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
19:49:55,661 INFO  [TransactionFactoryFactory] Transaction strategy: org.springframework.orm.hibernate3.SpringTransactionFactory
19:49:55,661 INFO  [TransactionManagerLookupFactory] No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
19:49:55,661 INFO  [SettingsFactory] Automatic flush during beforeCompletion(): disabled
19:49:55,661 INFO  [SettingsFactory] Automatic session close at end of transaction: disabled
19:49:55,661 INFO  [SettingsFactory] JDBC batch size: 15
19:49:55,661 INFO  [SettingsFactory] JDBC batch updates for versioned data: disabled
19:49:55,661 INFO  [SettingsFactory] Scrollable result sets: enabled
19:49:55,661 INFO  [SettingsFactory] JDBC3 getGeneratedKeys(): disabled
19:49:55,661 INFO  [SettingsFactory] Connection release mode: auto
19:49:55,661 INFO  [SettingsFactory] Default batch fetch size: 1
19:49:55,661 INFO  [SettingsFactory] Generate SQL with comments: disabled
19:49:55,661 INFO  [SettingsFactory] Order SQL updates by primary key: disabled
19:49:55,661 INFO  [SettingsFactory] Order SQL inserts for batching: disabled
19:49:55,661 INFO  [SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
19:49:55,661 INFO  [ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
19:49:55,661 INFO  [SettingsFactory] Query language substitutions: {}
19:49:55,661 INFO  [SettingsFactory] JPA-QL strict compliance: disabled
19:49:55,661 INFO  [SettingsFactory] Second-level cache: enabled
19:49:55,661 INFO  [SettingsFactory] Query cache: disabled
19:49:55,661 INFO  [SettingsFactory] Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
19:49:55,661 INFO  [SettingsFactory] Optimize cache for minimal puts: disabled
19:49:55,661 INFO  [SettingsFactory] Structured second-level cache entries: disabled
19:49:55,661 INFO  [SettingsFactory] Statistics: disabled
19:49:55,661 INFO  [SettingsFactory] Deleted entity synthetic identifier rollback: disabled
19:49:55,661 INFO  [SettingsFactory] Default entity-mode: pojo
19:49:55,661 INFO  [SettingsFactory] Named query checking : enabled
19:49:55,661 INFO  [SessionFactoryImpl] building session factory
19:49:55,723 INFO  [SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured

当我尝试保存对象时,我得到 NullPointerException。我认为 Spring 组件扫描功能不起作用。因此,我无法获得 hibernateTemplate 实例。而且我在 JBoss 服务器日志的日志文件中没有看到任何对该类的扫描。请告诉我我错过了什么。还是 JBoss 5.1.2 和 Spring 3.0 的问题。

4

1 回答 1

0

使用 @Component 或 @Repository(首选)注释 HibernateRepository 类。它应该工作。

于 2013-09-05T04:34:33.110 回答