Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring使用@Autowired字段不在@Component中_Java_Spring_Spring Mvc - Fatal编程技术网

Java Spring使用@Autowired字段不在@Component中

Java Spring使用@Autowired字段不在@Component中,java,spring,spring-mvc,Java,Spring,Spring Mvc,我的应用程序中有一个案例,我需要使用对象AccountsDao AccountsDao public class Account { @Autowired private AccountsDao accountsDao; 没有将属性@Component放入Account类(也没有任何其他方法将其标记为Springbean) 这个应用程序非常庞大,有一个客观的原因,为什么帐户不能是Springbean,必须手动初始化 我也知道,这是一个单一的情况,共同的结构是好的 有什么方法可

我的应用程序中有一个案例,我需要使用对象
AccountsDao AccountsDao

public class Account {

    @Autowired
    private AccountsDao accountsDao;
没有将属性
@Component
放入
Account
类(也没有任何其他方法将其标记为Springbean)

这个应用程序非常庞大,有一个客观的原因,为什么
帐户
不能是Springbean,必须手动初始化

我也知道,这是一个单一的情况,共同的结构是好的


有什么方法可以做到这一点吗?

您可以获取spring上下文程序并从中获取AccountsDoa。您可以在帐户构造函数中执行此操作。
第二个响应。

您可以从中获取spring上下文编程并获取AccountsDoa。您可以在帐户构造函数中执行此操作。
第二个响应。

如果在构建
帐户时有
帐户dao
可用,您可以自己进行构造函数注入

public class Account {

    private final AccountsDao accountsDao;

    public Account(AccountsDao accountsDao) {
        this.accountsDa = accountsDao;
    }
}
除此之外,还有
@可配置的
,但据我所知,这需要AspectJ


如果在构建
帐户时有
帐户dao
可用,您可以自己进行构造函数注入

public class Account {

    private final AccountsDao accountsDao;

    public Account(AccountsDao accountsDao) {
        this.accountsDa = accountsDao;
    }
}
除此之外,还有
@可配置的
,但据我所知,这需要AspectJ

添加SpringUtils.java

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

@Component
public class SpringUtils implements BeanFactoryPostProcessor {

    private static ConfigurableListableBeanFactory beanFactory;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException {
        // TODO Auto-generated method stub
        SpringUtils.beanFactory = arg0;
    }

    public static <T> T getBean(Class<T> clz) throws BeansException {
        T result = (T) beanFactory.getBean(clz);
        return result;
    }

}
添加SpringUtils.java

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

@Component
public class SpringUtils implements BeanFactoryPostProcessor {

    private static ConfigurableListableBeanFactory beanFactory;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException {
        // TODO Auto-generated method stub
        SpringUtils.beanFactory = arg0;
    }

    public static <T> T getBean(Class<T> clz) throws BeansException {
        T result = (T) beanFactory.getBean(clz);
        return result;
    }

}

您不应该这样做,尤其是不使用BeanUtils(绝对反模式),并且有一些原因,为什么默认情况下不支持此操作。如果你真的需要刀,做一些类似的事情

public class Account {
  public void doSomethingWithDao(AccountDao accountDao) {
    // TODO do somthing with dao, but do not store it in a field
  }
} 
编辑:您可以这样称呼它:

@Component
public class MyBusinessLogicClass {
  @Autowired
  private AccountDao accountDao;

  public void doMyBusinessLogic(Account account) {
    account.doSomethingWithDao(accountDao);
  }
}

您不应该这样做,尤其是不使用BeanUtils(绝对反模式),并且有一些原因,为什么默认情况下不支持此操作。如果你真的需要刀,做一些类似的事情

public class Account {
  public void doSomethingWithDao(AccountDao accountDao) {
    // TODO do somthing with dao, but do not store it in a field
  }
} 
编辑:您可以这样称呼它:

@Component
public class MyBusinessLogicClass {
  @Autowired
  private AccountDao accountDao;

  public void doMyBusinessLogic(Account account) {
    account.doSomethingWithDao(accountDao);
  }
}

这不在选项中,不可用。这不在选项中,不可用。谢谢,这是我所看到的最好的选项。这破坏了使用Spring时的控制模式反转。
帐户现在将自行收集其依赖项,而不是从外部获取它们。像这样的代码更难测试,因为您现在需要记住在测试中使用帐户时正确设置SpringUtils。感谢我所看到的这是最好的选择,这破坏了使用Spring时的控制反转模式。
帐户现在将自行收集其依赖项,而不是从外部获取它们。这样的代码更难测试,因为您现在需要记住在测试中使用帐户时正确设置SpringUtils。我没有理解您。从那时起,它从哪里得到AccountDao?在方法中,您只在需要它的地方有AccountDao。这取决于您需要accountDao做什么,但是Account的外部调用方将注入accountDao,并将其作为参数提供给Account,仅用于方法的上下文。我没有得到您的答案。从那时起,它从哪里得到AccountDao?在方法中,您只在需要它的地方有AccountDao。这取决于您需要accountDao做什么,但是Account的外部调用程序将有accountDao注入,并将其作为参数提供给Account,仅用于方法的上下文。