【发布时间】:2013-04-11 10:23:13
【问题描述】:
在我的 Web 应用程序中,我没有使用 applicationContext.xml。如何获取@Service注解类的bean?
如果我在我的 Web 应用程序中使用了 applicationContext.xml,我必须每次都加载 applicationContext.xml 以获取 @Service 注释类的 bean。
我是这样用的
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext);
ServerConnection con = (ServerConnection ) ctx.getBean("con");
我的服务类将是,
@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;
}
}
有没有其他方法可以得到这个类的bean?
或
对于Spring3.x中的核心应用程序和Web应用程序,获取@Service注释类的bean的正确方法是什么?
【问题讨论】:
标签: java spring web-applications spring-mvc spring-annotations