Configuring Jndi under JBoss leverages Spring's IOC to get

Source: Internet
Author: User
Tags jboss

A generic data source is used within the project. Consider that there is no distributed data access. The APACHE-COMMONS-DBCP was used. However, a third party report making tool was added to the project. The tool forces the use of Jndi to obtain a connection (damn) There's no way. It's only added. The configuration steps and an error note are shown below:

1. Environment: Windows XP system. The jbossGA-4.2.2 sqlserver2000 is used.

The project architecture is spring+ibatis+struts.

2. Configure Step 1. Because JBoss automatically finds the **-ds.xml file in the Server\default\deploy directory. and read the contents of it. To get the corresponding datasource, my file details are as follows:

<?xml version="1.0" encoding="UTF-8"?>
   <datasources>
   <local-tx-datasource>
      <jndi-name>jdbc/DataSource</jndi-name>
      <connection-url>jdbc:jtds:sqlserver://localhost:1433/fnx</connection-url>
      <driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
      <user-name>sa</user-name>
      <password>1234</password>
      <metadata>
      <type-mapping>MS SQLSERVER2000</type-mapping>
      </metadata>
     </local-tx-datasource>
</datasources>

Configure Step 2. (Some of the content is from the Web) because you use spring to get jndi. So Factorybean, Initializingbean.

Get/set method did not add

public class Datasourcefactorybean implements Factorybean, Initializingbean {





/** Cache Data Source Object * *


private DataSource DataSource;


/** whether the Jndi data source/


private Boolean Jndi;


/** The name of the data source (if it is a JNDI data source) * *


private String Jndiname;


/** JDBC Driver Class (non-JNDI) * *


private String Driverclassname;


/** The URL of the database (non-JNDI) * *


private String URL;


/** username (non-jndi) * *


private String username;


/** Password (non-jndi) * *


private String password;





public void Afterpropertiesset () throws Exception {


if (This.isjndi ()) {


if (!org.springframework.util.stringutils.hastext (This.jndiname)) {


throw new IllegalArgumentException ("Jndiname is required");


  }


} else {


if (!org.springframework.util.stringutils


. HasText (This.driverclassname)) {


throw new IllegalArgumentException (


"Driverclassname is required");


  }


if (!org.springframework.util.stringutils.hastext (This.url)) {


throw new illegalargumentexception ("URL is required");


  }


if (!org.springframework.util.stringutils.hastext (This.username)) {


throw new IllegalArgumentException ("username is required");


  }


if (!org.springframework.util.stringutils.hastext (This.password)) {


throw new IllegalArgumentException ("Password is required");


  }


  }


//Create a data source when initializing


This.createdatasource ();


}





public Object GetObject () throws Exception {


DataSource ds = This.createdatasource ();


return DS;


}





public Class Getobjecttype () {


return javax.sql.DataSource.class;


}





public boolean Issingleton () {


return true;


}





protected DataSource CreateDataSource () {


DataSource ds = This.datasource;


if (ds = = null) {


try {


if (This.isjndi ()) {


ds = new Jndidatasourcesupport ()


. Lookupdatasource (This.jndiname);


} else {


ds = new Drivermanagerdatasource (driverclassname, URL,


username, password);


   }


} catch (Throwable err) {


throw new RuntimeException (ERR);


  }


  }


This.datasource = ds;


return DS;


}





Private class Jndidatasourcesupport extends Jndilocatorsupport {


public DataSource Lookupdatasource (String jndiname)


throws Namingexception {


return (DataSource) super.lookup (Jndiname,getobjecttype ());


  }


}





}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.