简体   繁体   English

类对象为新对象时无法使用@spring批注

[英]Unable to use @spring annotations when class object is new

Actually i am having a spring main class as follows. 实际上,我的春季主要课程如下。

ClassLoader loader = null;

    try {
        loader = URLClassLoader.newInstance(new URL[]{new   

 File(plugins + "/" + pluginName + "/" + pluginName +   

 ".jar").toURI().toURL()}, getClass().getClassLoader());

    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

 Class<?> clazz = null;

    try {

        clazz = Class.forName("com.sample.Specific", true, loader);

    } catch (ClassNotFoundException e) {

        e.printStackTrace();

    }

Method method = null;
    try {
        method = clazz.getMethod("run",new Class[]{});
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }

 try {
        method.invoke(clazz.newinstance,new Object[]{});
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }

Specific Class is follow : 具体类别如下:

package com.sample
@Service
public class Specific {

    @Autowired
    private FD fd;


    public void run(){

        fd.init();

    }

}

@Autowired FD comes to be null. @Autowired FD为空。 Can anyone give me some solution as i also know new operator will not work for @autowired . 谁能给我一些解决方案,因为我也知道新的运算符不适用于@autowired As i am loading class with new instance then only it becomes null. 当我用新实例加载类时,只有它变为空。 Can anyone guide me in this thing 谁能在这件事上指导我

Add Specific Service bean inside your main class. 在您的主类中添加特定服务bean。 As long as the service is inside one your component scan packages then you shall be fine. 只要该服务位于组件扫描包中,就可以了。 Do not use new operator. 不要使用新的运算符。

@Autowired
private Specific specific;

Spring has its own way to provide you new objects. Spring有自己的方式为您提供新对象。 As long as you're consistent using @Autowired and @Component/@Service/@Repository/@Controller there should be no problem 只要您使用@Autowired@Component/@Service/@Repository/@Controller保持一致,就不会有问题

And since all "business" object instantiation is handled by Spring you should never use new . 而且由于所有“业务”对象实例化都是由Spring处理的,因此您永远不要使用new If you have no other way of getting an instance (something I realy doubt about it) you can use ApplicationContext.getBean() but as I said, in most cases this is not required (and this is also a bad practice) 如果您没有其他获取实例的方式(对此我真的很怀疑),则可以使用ApplicationContext.getBean()但正如我所说,在大多数情况下,这不是必需的(这也是一种不好的做法)

If you need several instances of a class instead of injecting them (by using @Autowired ) you can inject a Provider<T> 如果您需要一个类的多个实例而不是注入它们(通过使用@Autowired ),则可以注入Provider<T>

UPDATE UPDATE

Since the class is known at runtime you need to inject an ApplicationContext and use it to get the bean: 由于该类在运行时是已知的,因此您需要注入ApplicationContext并使用它来获取bean:

public class TheClassWhereYouAreCreatingTheObject {

    @Autowired
    private ApplicationContext context;                  // You definitely need this

    public void theMethodWhereYouAreCreatingTheObject() {
         Class<?> clazz = ...                            // getting the object class
         Object instance = context.getBean(clazz);     // getting and instance trough Spring

         // If you know that kind of object you will get cast it at call its methods
         ((Specific) instance).run();

         // If you know anything about the class you will have to use reflection
         Method method = clazz.getMethod("run", new Class[]{});
         method.invoke(instance, new Object[]{});
    }
}

If you want to take advantage of autowiring then I think we have to think from spring terms. 如果您想利用自动装配的优势,那么我认为我们必须从春季开始考虑。

you can use Beanutils to create a new instance and play with reflections supporting spring features. 您可以使用Beanutils创建一个新实例,并使用支持Spring功能的反射进行播放。 Please go through below methods: 请通过以下方法:

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/BeanUtils.html https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/BeanUtils.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM