Usually in J2EE development, datasource configured on the server through JNDI is used, for example, the final application runs on WebLogic, while Tomcat may be used on the local machine in the development environment, at this time, we need to configure a datasource on Tomcat. on the Internet, we can see that many of the solutions are to change the server in the Tomcat runtime environment. XML or context. XML, in fact, the simplest way is to add a context in the project's META-INF directory. in this way, you do not need to change the tomcat configuration on your machine for every member of the project team. For example:
The content of context is as follows:
<?xml version="1.0" encoding="UTF-8"?><Context><Resource name="xxxDS" auth="Container" type="javax.sql.DataSource"maxActive="20" maxIdle="30" maxWait="10000" username="xx" password="xx"driverClassName="oracle.jdbc.OracleDriver"url="jdbc:oracle:thin:@147.151.240.xx:1521:orcl" /></Context>
In this case, the following Java code accesses the JNDI to obtain the datasource. In the eclipse development environment, Tomcat can be integrated and finally released to WebLogic for running.
private void testJNDIDataSource(){try {InitialContext context = new InitialContext();javax.sql.DataSource ds = (javax.sql.DataSource) context.lookup ("java:/comp/env/xxxDS");System.out.println(ds);} catch (Exception e) {e.printStackTrace();};}
If it is integrated with spring, you do not need to configure the context. xml method. The following configuration is used to solve the problem. However, this introduces the configuration that should not be done in the production environment to adapt to the test environment. It is better to use this method.
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>xxxDS</value> </property> <property name="defaultObject" ref="dataSource1"/> </bean> <bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@147.151.240.xx:1521:orcl" /> <property name="username" value="xxx" /> <property name="password" value="xxx" /></bean>
The profile configuration was introduced in spring3.1 to use different bean configurations in different environments.
<beans profile="weblogic"><bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>xxxDS</value> </property><property name="resourceRef" value="true"/> </bean> </beans><beans profile="tomcat"><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /><property name="url" value="jdbc:oracle:thin:@147.151.240.89:1521:orcl" /><property name="username" value="but" /><property name="password" value="but" /></bean></beans>
In web. XML, specify the profile to be enabled.
<context-param> <param-name>spring.profiles.active</param-name> <param-value>tomcat</param-value></context-param>