简体   繁体   English

使用spring实例化静态工厂方法?

[英]Using spring to instantiate with static factory methods?

I am using spring3. 我正在使用spring3。 I have below classes. 我有以下课程。

Transport.java Transport.java

package com.net.core.ioc;

public interface Transport {
  public void getTransport();
}

Car.java 汽车.java

package com.net.core.ioc;

    public class Car implements Transport{
        public void getTransport(){
            System.out.println("Transport type is Car");
        }
    }

Bus.java Bus.java

package com.net.core.ioc;

    public class Bus implements Transport {
        public void getTransport() {
            System.out.println("Transport type is Car");
        }

SpringService.java SpringService.java

package com.net.core.ioc;

    public class SpringService {
        private static SpringService service = new SpringService();

        //Static factory method
        public static SpringService createService(){
            return service;
        }

        //Instance factory methods
        public Transport createCarInstance(){
        return new Car();
        }

    public Transport createBusInstance(){
            return new Bus();
        }

    }
 }

config.xml config.xml

<context:component-scan base-package="javabeat.net.core.ioc"/>
        <bean id="springServiceStaticFactory" factory-method="createService"/>
        <bean id="springServiceCarInstance" factory-bean="springServiceStaticFactory" factory-method="createCarInstance"/>
        <bean id="springServiceBusInstance" factory-bean="springServiceStaticFactory" factory-method="createBusInstance"/>
    </beans>

Now my question is does spring manage Car and Bus instances which are being instantiated using SpringService.java class?Because i have to annotate Car and Bus classes with @Transactional annotation. 现在我的问题是spring是否管理使用SpringService.java类实例化的Car和Bus实例?因为我必须使用@Transactional注释对Car和Bus类进行注释。 Is it possible? 可能吗?

Spring will manage your Car and Bus instances as they are part of the ApplicationContext . Spring将管理您的CarBus实例,因为它们是ApplicationContext一部分。 What you are doing is basically the same as normally would happen with a Spring FactoryBean but you are now using your own mechanism. 您所做的基本上与使用Spring FactoryBean的操作相同,但是现在您正在使用自己的机制。

However in your current setup the Car and Bus beans are singletons as they will be created once, not sure if that is what you want. 但是,在当前设置中, CarBus Bean是单例的,因为它们将创建一次,不确定是否是您想要的。

If you want to pass in values to the constructor simply modify your factory method. 如果要将值传递给构造函数,只需修改工厂方法即可。

public Transport createCarInstance(String name){
    return new Car(name);
}

Your bean definition should tell that it is a prototype scoped bean 您的bean定义应该告诉它它是原型作用域bean

    <bean id="springServiceCarInstance" factory-bean="springServiceStaticFactory" factory-method="createCarInstance" scope="prototype" />

Now in your code you can do a lookup and pass arguments to the lookup method (See javadoc ). 现在,您可以在代码中进行查找,并将参数传递给lookup方法(请参阅javadoc )。

@Autowired
private ApplicationContext ctx;

public void myMethod() {
    Car car = ctx.getBean(Car.class, "Mercedes"); 
}

The code above should give you a fresh Car instance with the name Mercedes . 上面的代码应为您提供一个名为Mercedes的新Car实例。

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

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