目测只能把springboot打成war包,部署到weblgic上。
因为开发环境下用main函数运行项目,会报错:
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
注意:如果使用的weblogic12c是老版本,符合servlet2.5规范,建议使用springboot版本为1.5.x,项目打包时需要配置weblogic.xml和web.xml
部署可以参考博客:https://segmentfault.com/a/1190000016327181
springboot项目添加web.xml的方法:https://blog.csdn.net/hcrw01/article/details/80248390
踩坑提示:web.xml中需要修改的地方,看里面的注释即可
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1" >
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 注意!!!这里写你自己的springboot启动类的路径 -->
<param-value>com.trs.xxx.network.SpringbootApplication</param-value>
</context-param>
<listener>
<listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
本博主使用的版本是weblogic12c 12.2.1.3.0,符合servlet3.0规范,可不配置web.xml和weblogic.xml
weblogic下载安装教材:https://blog.csdn.net/qq_36868342/article/details/79967606
正式开始:
一:首先加入连接weblogic的包:去网上下载wlfullclient.jar,作为第三方jar包添加到项目中。
springboot添加第三方包:https://blog.csdn.net/yanzhenjingfan/article/details/90517877
二:在application.properties加入jndi-name,添加mybatis的xml映射文件路径
#JNDI-NAME
spring.datasource.jndi-name=jndi/cmsdb
#mybatis映射文件
mybatis.mapper-locations=classpath:mapping/**/*.xml
三:pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>ccb</groupId>
<artifactId>xxx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>xxx</name>
<packaging>war</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 移除tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 移除嵌入式tomcat插件 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 自己第三方添加的weblogic包 -->
<dependency>
<groupId>com.weblogic</groupId>
<artifactId>weblogic</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
四:配置weblogic的jndi数据源
package com.trs.xxx.network.configuration;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jndi.JndiObjectFactoryBean;
/**
*
* @author : 雁阵惊凡
* @version :2019年5月20日 下午2:57:00
*
* TODO :配置weblogic的jndi数据源
*
*/
@Configuration
public class DataSourceConfig {
@Bean(name = "dataSource")
public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, "t3://localhost:7001");
//weblogic账号
properties.put(Context.SECURITY_PRINCIPAL, "weblogic");
//weblogic密码
properties.put(Context.SECURITY_CREDENTIALS, "weblogic@123");//
bean.setJndiEnvironment(properties);
bean.setResourceRef(true);
bean.setJndiName("jndi/cmsdb");
bean.setProxyInterface(DataSource.class);
bean.setLookupOnStartup(false);
bean.afterPropertiesSet();
return (DataSource) bean.getObject();
}
}
五:mybatis获取jndi数据源
package com.trs.xxx.network.configuration;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
/**
*
* @author : 雁阵惊凡
* @version :2019年5月20日 下午3:01:19
*
* TODO :mybatis获取jndi数据源
*
*/
@Configuration
@MapperScan(basePackages = "com.trs.xxx.network.mapper", sqlSessionFactoryRef = "sqlSessionFactory")
public class DSConfig {
//获取dataSourcec
@Autowired
@Qualifier("dataSource")
private DataSource dataSource;
//获取application.propertis配置文件中的mybatis.mapper-locations
@Value("${mybatis.mapper-locations}")
private String mapperLocations;
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
factoryBean.setMapperLocations(resolver.getResources(mapperLocations));
return factoryBean.getObject();
}
@Bean
public DataSourceTransactionManager transactionManager() throws Exception {
return new DataSourceTransactionManager(dataSource);
}
@Bean
public SqlSessionTemplate sqlSessionTemplate() throws Exception {
return new SqlSessionTemplate(sqlSessionFactory());
}
}
六:运行weblogic,然后在浏览器中看是否可以登陆了:http://localhost:7001/console
D:\Oracle\Middleware\Oracle_Home\user_projects\domains\base_domain
七:成功开启weblogic后,写一个测试,运行testConn方法打印看是否拿到datasource
注意,直接运行springboot中的main函数,会报错。我们还是写一个test,看是否拿到jndi数据源
package com.trs.xxx.network;
import java.sql.Connection;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.jndi.JndiObjectFactoryBean;
import org.springframework.test.context.junit4.SpringRunner;
import com.trs.ccb.network.entity.integratedMgr.NWNode;
import com.trs.ccb.network.service.integratedMgr.NWNodeService;
@SpringBootTest
@RunWith(SpringRunner.class)
public class MybatisApplicationTests {
@Test
public void testConn() throws Exception {
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, "t3://localhost:7001");
properties.put(Context.SECURITY_PRINCIPAL, "weblogic");
properties.put(Context.SECURITY_CREDENTIALS, "weblogic@123");
bean.setJndiEnvironment(properties);
bean.setResourceRef(true);
bean.setJndiName("jndi/cmsdb");
bean.setProxyInterface(DataSource.class);
bean.setLookupOnStartup(false);
bean.afterPropertiesSet();
DataSource dataSource = (DataSource) bean.getObject();
//打印出jndi数据源
System.err.println(dataSource);
}
}
上面红色打印出来的,表示已经拿到dataSource对象了。
八:springboot启动文件
package com.trs.xxx.network;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.WebApplicationInitializer;
@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class SpringbootApplication extends SpringBootServletInitializer implements WebApplicationInitializer {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringbootApplication.class);
}
}
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}),加入这个注解,是为了排除运行程序时,springboot回去读取spring.datasource.xxx=xx的配置。我们这里连接的是jndi,不需要加入任何其他的数据库连接配置,不加这个配置,有可能会报错。
九:pom.xml中
注意:打包时记得把测试案例注释掉,有可能会导致打包不成功。
eclipse怎么打包应该都会吧,用IDEA的童鞋自行百度,先maven clean一下,再maven install,在target目录下就生成war包了,步骤:
1:eclipse中右键项目,Run As ----> maven clean
2:eclipse中右键项目,Run As ----> maven install
部署测试,不同版本的部署方式可能有些细微差别,大同小异,应该可以搞定的,很简单
在weblogic上部署web应用可以参考 https://www.cnblogs.com/DFX339/p/8515200.html
十:浏览器访问测试: