java 获取@Service 注释类的bean?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15946371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get bean of @Service annotated class?
提问by Human Being
In my web application, I am not using applicationContext.xml
. How can I get the bean of @Service
annotated class?
在我的 Web 应用程序中,我没有使用applicationContext.xml
. 如何获得带@Service
注释的类的 bean ?
If I used the applicationContext.xml
in my web application, I have to load the applicationContext.xml
every time to get the bean of the @Service
annotated class.
如果我applicationContext.xml
在我的 Web 应用程序中使用了,我必须applicationContext.xml
每次都加载以获取带@Service
注释的类的 bean 。
I used this way
我用这种方式
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext);
ServerConnection con = (ServerConnection ) ctx.getBean("con");
My Service class will be as ,
我的服务类将是,
@Service or @Service("con")
public class ServerConnection {
private TServerProtocol tServerProtocol;
private URI uri;
public TServerProtocol getServerConnection(){
System.out.println("host :"+host+"\nport :"+port);
try {
uri = new URI("tcp://" + host + ":" + port);
} catch (URISyntaxException e) {
System.out.println("Exception in xreating URI Path");
}
tServerProtocol = new TServerProtocol(new Endpoint(uri));
return tServerProtocol;
}
}
Is there any other way to get a beanof this class ?
有没有其他方法可以获得这个类的bean?
or
或者
What is the proper way to get a bean of @Service
annotated class in case of core application and web application in Spring3.x?
@Service
在 Spring3.x 中的核心应用程序和 Web 应用程序的情况下,获取带注释类的 bean 的正确方法是什么?
采纳答案by Donal Fellows
If you are using annotation-based configuration, you use @Autowired
to get the bean by its type or @Resource
to get the bean by its name. Use only one of these for any particular property (to keep confusion down).
如果您使用基于注解的配置,您可以使用@Autowired
bean 的类型或名称@Resource
来获取 bean。对于任何特定属性仅使用其中之一(以减少混淆)。
@Autowired
private ServerConnection connection;
@Resource(name = "con")
private ServerConnection connection;
There's also @Inject
, but I don't like that as it gets less nice as the number of beans goes up. YMMV.
还有@Inject
,但我不喜欢那样,因为随着豆子数量的增加它变得不那么好。天啊。
回答by manoj jangam
ApplicationContext context=SpringApplication.run(YourProjectApplication.class, args);
ApplicationContext context=SpringApplication.run(YourProjectApplication.class, args);
this context can be used to get the beans created by annotation @Bean
and @Service
as
此上下文可用于获取通过注释创建的 bean@Bean
并@Service
作为
context.getBean(className.class);
context.getBean(className.class);
回答by Maksym Demidas
If you working in some bean outside spring then you can use normal Spring annotations:
如果你在 spring 之外的一些 bean 中工作,那么你可以使用普通的 Spring 注释:
@Autowired
private Dependency1 dependency1;
@Autowired
private Dependency2 dependency2;
@Autowired
private Dependency2 dependency2;
then call one time from inside of this class:
然后从这个类内部调用一次:
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
to inject all dependencies. It may be more convinient if you have multiple dependencies.
注入所有依赖项。如果您有多个依赖项,可能会更方便。
回答by cRx
Since you are making use of annotation, I believe you would prefer the use of @Autowired. If you have a class that will call ServerConnection
(lets say CallingClass
under package com.foo
), then this is how it would look like:
由于您正在使用注释,我相信您更喜欢使用@Autowired。如果你有一个会调用的类ServerConnection
(让我们说CallingClass
在 package 下com.foo
),那么它看起来像这样:
package com.foo;
@Component
public class CallingClass{
@Autowired
private ServerConnection serverConnection
// add your getters and setters
}
Then on your applicationContext.xml just add the following
然后在您的 applicationContext.xml 上添加以下内容
<context:component-scan base-package="com.foo" />
Hope I answered your question.
希望我回答了你的问题。