[英]Spring annotations for factory method
I am using Spring 3.0 for my project, I am having a class MySingletonClass, it is singleton as below : 我正在使用Spring 3.0作为我的项目,我有一个类MySingletonClass,它是singleton,如下所示:
//@Component("mySingletonClass")
public class MySingletonClass {
private static MySingletonClass obj = new MySingletonClass();
public static MySingletonClass getSingleObj() {
return obj;
}
}
spring xml bean configuration for this class is as below : 这个类的spring xml bean配置如下:
<bean id="mySingletonClass" class="app.MySingletonClass" factory-method="getSingleObj" />
I was trying to remove bean configuration and use annotation. 我试图删除bean配置并使用注释。 how do I write annotation for factory method? 如何为工厂方法编写注释?
Thanks in advance !! 提前致谢 !!
Spring creates instances like singleton by default. Spring默认创建像singleton这样的实例。 You can just do. 你可以这样做。
@Component("mySingletonClass")
public class MySingletonClass {
}
And if you don't change scope your component is singleton. 如果你不改变范围你的组件是单身。
Spring component's are automatically created as Singletons. Spring组件会自动创建为Singletons。
Declare the annotation @Controller on the class 在类上声明注释@Controller
@Controller
public class MySingletonClass {
}
Then in your Spring Config file, declare like: 然后在Spring Config文件中声明如下:
<bean id="mySingleton" class="com.package.MySingletonClass">
Then to use in another class you can use Autowiring or Setter/Constructor dependency injection. 然后在另一个类中使用,您可以使用Autowiring或Setter / Constructor依赖注入。
@Component
public class OtherClass {
@Autowired
private MySingletonClass mySingleton;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.