首页 > 解决方案 > Spring-boot applicationDatasource.xml 未从外部属性读取

问题描述

我正在尝试从外部属性读取数据源 JNDI,但显然它无法读取它。

我试过这个:

<jee:jndi-lookup id="clarifyDS" jndi-name="${bd.clarify.jndi}"
        lookup-on-startup="false" proxy-interface="javax.sql.DataSource" />

但它会引发以下错误。

SERVER: AdminServer [ERROR] [08-07-2019 14:16:18.088] (ClarifyDaoImpl.java:120) - [ obtenerColasAsoc idTx= ] - [obtenerColasAsoc]JndiObjectTargetSource failed to obtain new target object; nested exception is javax.naming.NameNotFoundException: While trying to lookup '${bd.clarify.jndi}' didn't find subcontext '${bd'. Resolved ''; remaining name '${bd/clarify/jndi}'-org.springframework.jndi.JndiLookupFailureException: JndiObjectTargetSource failed to obtain new target object; nested exception is javax.naming.NameNotFoundException: While trying to lookup '${bd.clarify.jndi}' didn't find subcontext '${bd'. Resolved ''; remaining name '${bd/clarify/jndi}'

我只需要数据源的名称,因为这是在 weblogic 服务器上配置的。无需用户名或密码。

这是完整的文件

应用程序上下文.xml

<?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:jee="http://www.springframework.org/schema/jee"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans          
                           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                           http://www.springframework.org/schema/aop          
                           http://www.springframework.org/schema/aop/spring-aop-3.1.xsd          
                           http://www.springframework.org/schema/context          
                           http://www.springframework.org/schema/context/spring-context-3.1.xsd        
                           http://www.springframework.org/schema/jee
                           http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">

    <context:annotation-config />
    <context:component-scan base-package="pe.com.claro.postventa.consultacolas" />
    <context:property-placeholder location="file:${file.properties}ConsultaColas/.properties" />

    <import resource="applicationDatasource.xml" />

</beans>

应用程序数据源.xml

<?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:jee="http://www.springframework.org/schema/jee"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-3.1.xsd
       http://www.springframework.org/schema/aop  
       http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
       http://www.springframework.org/schema/context  
       http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/jee
       http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">

    <jee:jndi-lookup id="clarifyDS" jndi-name="${bd.clarify.jndi}"
        lookup-on-startup="false" proxy-interface="javax.sql.DataSource" />

</beans>

这是我的Java类。

@Repository
public class ClarifyDaoImpl implements ClarifyDao{

    private static final Logger logger = Logger.getLogger(ClarifyDaoImpl.class);

    @Autowired
    @Qualifier(value = "clarifyDS")
    private DataSource clarifyDS;

    @Autowired
    Propiedades propiedades;

    private Utilitarios utilitarios = new Utilitarios();

    @Override
    public ObtenerColasAsocResponseBean obtenerColasAsoc(String mensajeTransaccion, ObtenerColasAsocRequestBean request)
            throws DBException {

        //Do something
    }

}

也许我没有使用正确的语法来读取属性文件

顺便说一句,如果有人知道我很高兴听到的更好方法,我不确定这是否是将 conexion 设置为数据库的最佳方法。

谢谢!

标签: javadatabasespringoraclespring-boot

解决方案


推荐阅读