Spring boot Liberty配置文件无法检测到Spring Boot componentscan注释,并且未加载应用程序上下文
我正在Liberty 16.0.0.3中部署Spring Boot web应用程序。我已按照提供和配置的全局共享库提供了所有依赖范围。但当我启动应用程序时,Liberty无法加载应用程序上下文 我的配置如下 我的pom.xmlSpring boot Liberty配置文件无法检测到Spring Boot componentscan注释,并且未加载应用程序上下文,spring-boot,websphere-liberty,Spring Boot,Websphere Liberty,我正在Liberty 16.0.0.3中部署Spring Boot web应用程序。我已按照提供和配置的全局共享库提供了所有依赖范围。但当我启动应用程序时,Liberty无法加载应用程序上下文 我的配置如下 我的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/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>
<groupId>com.myapp.testapp</groupId>
<artifactId>testsharedlib</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>testsharedlib</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<runtime.scope>provided</runtime.scope>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>${runtime.scope}</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-ws</artifactId>
<scope>${runtime.scope}</scope>
</dependency>
<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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- <version>2.3</version> -->
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<skip>true</skip>
<!-- <failOnMissingWebXml>false</failOnMissingWebXml> -->
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
<archive>
<manifest>
<mainClass>com.myapp.testapp.Appconfig</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<DisableIBMJAXWSEngine>true</DisableIBMJAXWSEngine>
<Class-Path>conf</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.0.0
com.myapp.testapp
testsharedlib
1.0.0-SNAPSHOT
战争
testsharedlib
SpringBoot的演示项目
org.springframework.boot
spring启动程序父级
1.4.3.1发布
UTF-8
UTF-8
1.8
假如
org.springframework.boot
SpringBootStarterWeb
${runtime.scope}
org.springframework.boot
弹簧靴起动器
${runtime.scope}
org.springframework.boot
弹簧启动机tomcat
假如
org.springframework.boot
弹簧起动试验
测试
org.springframework.boot
springbootmaven插件
重新包装
真的
org.apache.maven.plugins
maven战争插件
假的
WEB-INF/lib/*.jar
com.myapp.testapp.Appconfig
真的
解放党/
真的
形态
org.apache.maven.plugins
maven编译器插件
1.8
1.8
maven资源插件
Liberty server.xml
<server description="Default Server">
<featureManager>
<feature>webProfile-7.0</feature>
<feature>localConnector-1.0</feature>
</featureManager>
<library id="global">
<fileset dir="${server.config.dir}/lib/jars" includes="*.jar"/>
</library>
<application context-root="HelloWorld" location="text-1.0.0-SNAPSHOT.war">
<classloader commonLibraryRef="global" delegation="parentFirst"/>
</application>
<!-- Define the host name for use by the collective.
If the host name needs to be changed, the server should be
removed from the collective and re-joined. -->
<variable name="defaultHostName" value="localhost"/>
<keyStore id="defaultKeyStore" password="adminpwd"/>
<!-- Define an Administrator and non-Administrator -->
<basicRegistry id="basic">
<user name="admin" password="adminpwd"/>
<user name="nonadmin" password="nonadminpwd"/>
</basicRegistry>
<!-- Assign 'admin' to Administrator -->
<administrator-role>
<user>admin</user>
</administrator-role>
<webContainer deferServletLoad="false"/>
<applicationManager autoExpand="true"/>
<httpEndpoint host="*" httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint"/>
<logging consoleLogLevel="INFO" traceSpecification="*=info:com.ibm.ws.classloading.*=ALL"/>
</server>
webProfile-7.0
localConnector-1.0
管理
我可以通过在Liberty中提供全球共享库来实现它。找到下面的示例配置
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>
<groupId>com.myapp.testapp</groupId>
<artifactId>testsharedlib</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>testsharedlib</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<runtime.scope>provided</runtime.scope>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>${runtime.scope}</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-ws</artifactId>
<scope>${runtime.scope}</scope>
</dependency>
<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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
<featureManager>
<feature>webProfile-7.0</feature>
<feature>localConnector-1.0</feature>
</featureManager>
<library id="global">
<fileset dir="${server.config.dir}/lib/jars" includes="*.jar"/>
</library>
<application context-root="HelloWorld" location="text-1.0.0-SNAPSHOT.war">
<classloader commonLibraryRef="global" delegation="parentLast"/>
</application>
<!-- Define the host name for use by the collective.
If the host name needs to be changed, the server should be
removed from the collective and re-joined. -->
<variable name="defaultHostName" value="localhost"/>
<keyStore id="defaultKeyStore" password="adminpwd"/>
<!-- Define an Administrator and non-Administrator -->
<basicRegistry id="basic">
<user name="admin" password="adminpwd"/>
<user name="nonadmin" password="nonadminpwd"/>
</basicRegistry>
<!-- Assign 'admin' to Administrator -->
<administrator-role>
<user>admin</user>
</administrator-role>
<webContainer deferServletLoad="false"/>
<applicationManager autoExpand="true"/>
<httpEndpoint host="*" httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint"/>
<logging consoleLogLevel="INFO" traceSpecification="*=info:com.ibm.ws.classloading.*=ALL"/>
</server>
<?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">
<display-name>pctext</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.myapp.testpkg.ApplicationConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这适用于
WEB-INF/lib
中的依赖项而不适用于全局共享lib的原因是,初始化Spring应用程序的servlet(org.springframework.WEB.servlet.DispatcherServlet
)来自一个jar依赖项。Liberty将只扫描web上下文根目录(即web-INF/*
)中的servlet类。它不会扫描共享库中的servlet类
正如您在另一个答案中所发现的,链接来自共享库中jar的servlet的唯一方法是在web.xml中显式声明它(因为Liberty不会像在web-INF/*
中对类或jar那样自动扫描它)
为了证实这一点,我编写了一个简单的servlet:
@WebServlet("/LibServlet")
public class LibServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.getWriter().append("Served at: ").append(request.getContextPath());
}
}
然后我将这个servlet打包到
servletLib.jar
中。当我使用共享库将这个库提供给我的应用程序时,我无法在http://localhost:9080/testapp/LibServlet
。但是,当我将servletLib.jar
移动到WEB-INF/lib
中时,就可以访问servlet了。That war插件配置看起来很奇怪。mainClass
与不可执行的war有什么关系?是否存在与失败相关的异常/堆栈?@StephaneNicoll我提供了main类调用应用程序上下文加载的自由。因为即使我不单独提供war插件,它也不会加载。@covener我没有看到任何异常应用程序正常启动,但当我点击URL时,我的控制器调用失败,出现404错误。这是因为当我们使用共享库时,应用程序上下文没有加载。当我在应用程序WEB-INF/lib中绑定jar时,同一个应用程序工作没有任何问题s:为什么在WEB-INF/lib中使用依赖项,而在共享lib中不使用依赖项?从这个答案中不清楚是什么改变了工作。是WEB.xml中的1
吗?看起来pom.xml和server.xml与原始问题中的完全相同。@AndyGuibert为了使它工作,我需要添加WEB.xml,并且应该证明如果我使用@SpringBootApplication(scanbasepackages={com.testapp.textpakage})之类的注释提供Servletilizer,则在web.xml.xml中提供ide DispatcherServlet配置当我使用全局共享库时,Liberty没有加载应用程序上下文。但是,如果我在WEB-INF/Lib中使用所有依赖jar打包应用程序,同样可以。我的上述配置还有另一个问题。如果我使用at配置和at配置属性加载属性/yml文件值,Liberty无法加载这些值。基本上它无法在启动期间加载@Configuration类。
@WebServlet("/LibServlet")
public class LibServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.getWriter().append("Served at: ").append(request.getContextPath());
}
}