如何在Spring Boot项目中配置非依赖的WebLogic相关Bean
Hey there! Converting that XML bean setup to Spring Boot's Java-based configuration is straightforward. Let's break it down for you:
Direct Equivalent Java Configuration
This code mirrors exactly what your XML config does, defining the nested bean and injecting it into the SaajSoapMessageFactory:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.ws.soap.saaj.SaajSoapMessageFactory; @Configuration public class SoapMessageConfig { // Defines the inner WebLogic MessageFactory bean @Bean public weblogic.xml.saaj.MessageFactoryImpl wlsMessageFactory() { return new weblogic.xml.saaj.MessageFactoryImpl(); } // Creates the main SaajSoapMessageFactory and injects the WebLogic bean @Bean public SaajSoapMessageFactory messageFactory(weblogic.xml.saaj.MessageFactoryImpl wlsMessageFactory) { SaajSoapMessageFactory factory = new SaajSoapMessageFactory(); factory.setMessageFactory(wlsMessageFactory); return factory; } }
Fixing STS's Unrecognized Class Error
Since your local project doesn't include the weblogic.xml.saaj.MessageFactoryImpl class, STS will flag it as an error. You have two solid fixes for this:
Option 1: Add WebLogic Dependency as Provided
Add the WebLogic SAAJ dependency to your build file with a provided scope. This lets your IDE recognize the class without packaging it into your app (since WebLogic already provides it on the server).
For Maven (add to pom.xml):
<dependency> <groupId>com.oracle.weblogic</groupId> <artifactId>weblogic-saaj</artifactId> <version>your-weblogic-version</version> <scope>provided</scope> </dependency>
For Gradle (add to build.gradle):
providedCompile group: 'com.oracle.weblogic', name: 'weblogic-saaj', version: 'your-weblogic-version'
Option 2: Use Reflection to Avoid IDE Errors
If you don't want to add the WebLogic dependency locally, use reflection to dynamically instantiate the class at runtime. This keeps your IDE happy while still working on the WebLogic server:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.ws.soap.saaj.SaajSoapMessageFactory; import javax.xml.soap.MessageFactory; @Configuration public class SoapMessageConfig { @Bean public SaajSoapMessageFactory messageFactory() throws Exception { SaajSoapMessageFactory factory = new SaajSoapMessageFactory(); // Dynamically load and instantiate the WebLogic MessageFactory Class<?> wlsMessageFactoryClass = Class.forName("weblogic.xml.saaj.MessageFactoryImpl"); MessageFactory wlsMessageFactory = (MessageFactory) wlsMessageFactoryClass.getDeclaredConstructor().newInstance(); factory.setMessageFactory(wlsMessageFactory); return factory; } }
Just note that this approach skips compile-time checks—if the class name or constructor changes, you'll only catch it at runtime.
内容的提问来源于stack exchange,提问作者hudi

