jbosseap7.x无法从另一个jar自动连接springbean

8iwquhpp  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(177)

我在jboss\u home\standalone\deployments中部署了两个spring应用程序jar。当我试图在app2中访问app1的springbean服务时,我得到了nullpointerexception

  1. @Service
  2. public class App1ServiceImpl {
  3. public void checkApp1Service() {
  4. System.out.println("Service Called");
  5. }
  6. }
  1. @Service
  2. public class App2ServiceImpl {
  3. @Autowired
  4. private App1ServiceImpl app1ServiceImpl;
  5. public void checkService() {
  6. app1ServiceImpl.checkApp1Service();
  7. }
  8. }

收到的错误如下:

  1. 18:29:23,443 INFO [io.undertow.servlet] (default task-1) Initializing Spring DispatcherServlet 'dispatcherServlet'
  2. 18:29:23,444 INFO [org.springframework.web.servlet.DispatcherServlet] (default task-1) Initializing Servlet 'dispatcherServlet'
  3. 18:29:23,473 INFO [org.springframework.web.servlet.DispatcherServlet] (default task-1) Completed initialization in 27 ms
  4. 18:29:23,537 ERROR [org.springframework.boot.web.servlet.support.ErrorPageFilter] (default task-1) Forwarding to error page from request [/test/] due to exception [null]: java.lang.NullPointerException
  5. at com.myapp1.service.App2ServiceImpl.checkService(App2ServiceImpl.java:16)
  6. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  7. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  8. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  9. at java.lang.reflect.Method.invoke(Method.java:498)
  10. at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
  11. at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
  12. at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
  13. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878)
  14. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792)

代码结构如下:
我在每个maven项目的src\main\resources\web inf文件夹下都有jboss-spring.xml,如下所示:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:jee="http://www.springframework.org/schema/jee"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xmlns:task="http://www.springframework.org/schema/task"
  10. xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
  11. <context:component-scan base-package="com.myapp1.service" />
  12. </beans>

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:jee="http://www.springframework.org/schema/jee"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xmlns:task="http://www.springframework.org/schema/task"
  10. xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
  11. <context:component-scan base-package="com.myapp2.service" />
  12. </beans>

另外,我在src\main\resources\webinf下有jboss-deployment-structure.xml,如下所示
对于myapp1

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <jboss-deployment-structure>
  3. <deployment>
  4. <dependencies>
  5. <module name="com.spring"/>
  6. </dependencies>
  7. </deployment>
  8. </jboss-deployment-structure>

对于myapp2

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <jboss-deployment-structure>
  3. <deployment>
  4. <dependencies>
  5. <module name="com.spring"/>
  6. <module
  7. name="deployment.myapp1-0.0.1-SNAPSHOT.jar" annotations="true" services="import" meta-inf="import"/>
  8. </dependencies>
  9. </deployment>
  10. </jboss-deployment-structure>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题