【问题标题】:calling a method from a different class in springBoot在 Spring Boot 中从不同的类调用方法
【发布时间】:2019-04-11 10:09:39
【问题描述】:

我想调用一个包含 applicationContext 的方法,该方法在独立运行时工作正常,但是当我尝试从不同的类调用它时 appContext.getBean(DataSource.class) 返回 null。

public class Action {

private static Logger log = LoggerFactory.getLogger(Action.class);

@Autowired
MessageConfigProperties messageProperties;

@Autowired
AutomatorApp automatorApp;

@Autowired
Apps gapps;

@Autowired
Deploy d;

@Autowired
Deploy depstatusChk;

@Autowired
private ApplicationContext appContext;

@Autowired
CreateSaltFileService createSaltFile;

@Autowired
DeployFactory depFactory;

@Autowired
IMoveAppsService moveAppsService;

@Autowired
IUserEnvironmentService userEnvService;

@Autowired
IEnvironmentService envService;

@Autowired
Session session;

private Logger logger = LoggerFactory.getLogger(Action.class);
private ServletContext context;

@Context
public void setServletContext(ServletContext context) {
    System.out.println("servlet context set here");
    this.context = context;
}

@POST
@Path("/register/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RequiresRoles("admin")
@RequiresPermissions("automator:register")
public Session register(Session credentials, @BeanParam Session springContext) throws AppException {
    System.out.println("into action class");
    System.out.println("-->>>" +appContext.getBean(DataSource.class));
    appContext.getBean(DataSource.class);
    logger.info(messageProperties.getGreetings());
    // logger.trace("Inside Session");
    System.out.println("Inside Session");
    credentials.setDatasource(springContext.getDatasource());

当这个 Action 方法被调用时

这个方法agentGroup在不同的类中

@POST
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get List of agent group", response = 
AgentGroup.class, responseContainer = "List")
public ArrayList<AgentGroup> agentGroup(Session credentials, @BeanParam Session springContext) throws AppException, ConfigException, InterruptedException {

    Session r=objA.register(credentials, springContext);
    int sessionId=r.getSessionId();
}

【问题讨论】:

  • objA是如何创建的?
  • 简单喜欢-> Action objA = new Action( );
  • 在这种情况下,Spring 无法进行任何自动装配,因此所有自动装配的变量都为空。你的代码很奇怪。通过new 创建rest 端点并从另一个端点调用它没有多大意义。

标签: java spring


【解决方案1】:

@Autowire ApplicationContext appContext只是为了获取数据源(appContext.getBean(DataSource.class))。

为什么不直接@Autowire DataSource datasource? 它将使您的 Action 类的业务逻辑独立于 Spring。

如果你真的想访问ApplicationContext,你可以尝试另一种方法

制作Action implements ApplicationContextAware

私有 ApplicationContext ctx;

  @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        ctx = applicationContext;
    }

详情请见Circular dependency in Spring

编辑: 您的 Action 类没有注释。没有org.springframework.web.bind.annotation. RestController,没有@Component@Service。你确定它是由Spring管理的吗?如果它只是使用new 创建的,那么 Autowire 将不起作用并且字段将为空。

【讨论】:

  • 但是,现在我在这一行得到空指针-> GenesysConnectionManager manager = (GenesysConnectionManager) context.getAttribute("GenesysConnectionManager");
  • 私有 ServletContext 上下文; public void setServletContext(ServletContext context) { System.out.println("servlet context set here"); this.context = 上下文; } 公共会话注册(会话凭据,@BeanParam 会话 springContext)抛出 AppException { credentials.setDatasource(springContext.getDatasource()); GenesysConnectionManager manager = (GenesysConnectionManager) context.getAttribute("GenesysConnectionManager");
猜你喜欢
  • 2020-02-22
  • 1970-01-01
  • 2019-06-10
  • 1970-01-01
  • 1970-01-01
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
相关资源
最近更新 更多