为什么JNDI无法从dataSource识别bean

时间:2019-04-15 17:43:38

标签: java mysql spring-data-jpa datasource jndi

嗨,我想将DataSource与来自Liberty的Server.xml分开,并在类中明确包含连接参数,如以下链接所示,但是,当我添加@Resource批注时,出现了以下问题: 我的连接器是mysql

没有名为'jndi / mysql'的bean:在JNDI环境中找不到

基于主题的任何贡献或帮助都会受到好评。问候

我试图做一个方法,用正确的符号@Bean和@Primary返回bean的数据源,但是日志仍然相同

 @DataSourceDefinition(
                name= "java:comp/env/jdbc/mysql", 
                className = "com.mysql.jdbc.Driver",
                url = "jdbc:mysql://localhost/mini_blog_es2?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", 
                user = "root",
                password = "tuna"
                ) })

`public class JPAConfigDatabase  { 
@Resource(lookup = "jndi/mysql")
    private DataSource dataSource; 
`

    // Get Method

    @Primary
        @Bean(name = "dataSource")
        public DataSource getDataSource()  {
            DataSource data = null;
            try {
                data = (DataSource) new JndiTemplate().lookup("jndi/mysql");
                System.out.println("Conexion Exitosa");
            } catch (Exception e) {
                System.out.println("NO JALO" + e.getMessage());
            }   
            return data;
        }
    }

`

/ ************************* SERVER.xml Liberty服务器**************** *********** //

<!-- Enable features -->
<featureManager>
    <feature>webProfile-7.0</feature>
    <feature>localConnector-1.0</feature>
</featureManager>

<!-- To access this server from a remote client add a host attribute to 
    the following element, e.g. host="*" -->
<httpEndpoint httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint"/>

<!-- Automatically expand WAR files and EAR files -->
<applicationManager autoExpand="true"/>

<library id="JTDS">
    <fileset dir="resources/sqlserver" includes="mysql-connector-java-6.0.3"/>
</library>

<webApplication id="moii-srv-war" location="moii-srv-war-0.0.1-SNAPSHOT.war" name="moii-srv-war">
<classloader commonLibraryRef="JTDS"/>
 </webApplication>

我希望控制台打印消息Conexion Exitosa可以将此数据源用于另一个模块:

错误日志:由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为'jndi / mysql'的bean可用:在JNDI环境中找不到

0 个答案:

没有答案