V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Newyorkcity
V2EX  ›  问与答

Java 如何要求 spring 对 new 出来的实例内的 @Autowired 成员变量注入?

  •  
  •   Newyorkcity · 2020-01-20 11:46:04 +08:00 · 1960 次点击
    这是一个创建于 1548 天前的主题,其中的信息可能已经有所发展或是发生改变。
    10 条回复    2020-01-20 16:03:50 +08:00
    dddddddddd
        1
    dddddddddd  
       2020-01-20 11:50:48 +08:00
    为什么要手动 new, 可以直接从容器了取呀
    Newyorkcity
        2
    Newyorkcity  
    OP
       2020-01-20 11:51:51 +08:00
    @dddddddddd 没给那个类加 @Component 之类的注解。。。。。
    nutting
        3
    nutting  
       2020-01-20 11:55:35 +08:00
    不能,用 spring 最好就都让它管理
    Newyorkcity
        4
    Newyorkcity  
    OP
       2020-01-20 12:00:58 +08:00
    @nutting 那工厂模式咋整?工厂类我只调用它的静态方法,这把它 @Component 感觉有点得不偿失。。工厂类返回的实例按理来说应该是 new 出来的,给可能被工厂用来制造实例的类都给注解 @Component ?然后用 getBean 得到来返回?
    可是 getBean 前面的 spring 上下文怎么得到呢。。。
    dddddddddd
        5
    dddddddddd  
       2020-01-20 12:01:56 +08:00
    如何没有注册这个 bean,Autowired 是不管用的, 可以查一下 spring 如何动态注册 bean,应该是可以的
    Newyorkcity
        6
    Newyorkcity  
    OP
       2020-01-20 12:02:49 +08:00
    @Newyorkcity new 出来 --》 反射反出来
    richard1122
        7
    richard1122  
       2020-01-20 13:22:13 +08:00   ❤️ 1
    ```java
    private @Autowired AutowireCapableBeanFactory beanFactory;

    public void doStuff() {
    MyBean obj = new MyBean();
    beanFactory.autowireBean(obj);
    // obj will now have its dependencies autowired.
    }
    ```
    https://stackoverflow.com/questions/3813588/how-to-inject-dependencies-into-a-self-instantiated-object-in-spring

    把对象传进去帮你做 autowire
    urzz
        8
    urzz  
       2020-01-20 15:22:53 +08:00
    ApplicationContextAware
    jaylee4869
        9
    jaylee4869  
       2020-01-20 16:02:44 +08:00   ❤️ 1
    给你工具类:

    ```java

    package com.utils;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;

    /**
    * 以静态变量保存 Spring ApplicationContext, 可在任何代码任何地方任何时候中取出 ApplicaitonContext.
    *
    */
    public class SpringContextHolder implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    /**
    * 实现 ApplicationContextAware 接口的 context 注入函数, 将其存入静态变量.
    */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
    // NOSONAR
    SpringContextHolder.applicationContext = applicationContext;
    }

    /**
    * 取得存储在静态变量中的 ApplicationContext.
    */
    public static ApplicationContext getApplicationContext() {
    checkApplicationContext();
    return applicationContext;
    }

    /**
    * 从静态变量 ApplicationContext 中取得 Bean, 自动转型为所赋值对象的类型.
    */
    public static <T> T getBean(String name) {
    checkApplicationContext();
    return (T) applicationContext.getBean(name);
    }

    /**
    * 从静态变量 ApplicationContext 中取得 Bean, 自动转型为所赋值对象的类型.
    */
    public static <T> T getBean(Class<T> clazz) {
    checkApplicationContext();
    return (T) applicationContext.getBean(clazz);
    }

    /**
    * 清除 applicationContext 静态变量.
    */
    public static void cleanApplicationContext() {
    applicationContext = null;
    }

    private static void checkApplicationContext() {
    if (applicationContext == null) {
    throw new IllegalStateException(
    "applicaitonContext 未注入,请在 applicationContext.xml 中定义 SpringContextHolder");
    }
    }
    }
    ```
    69partner
        10
    69partner  
       2020-01-20 16:03:50 +08:00
    既然不想用 @Autowired 你都说了 new 那就 new 呀
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3920 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 05:07 · PVG 13:07 · LAX 22:07 · JFK 01:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.