在 Web 应用程序中,我们真的不需要做 ..
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-context.xml");
ctx.getBean("beanId");
因为一般的做法是加载上下文文件并使用 web.xml 中的 ContextLoaderServlet 注入所有具有依赖关系的 bean,如下所示。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- or use the ContextLoaderServlet instead of the above listener
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
-->
但是,在没有容器的独立 Java 应用程序中,我最终会执行ctx.getBean("xyz"); . 有没有一种干净的方法可以做到这一点,在网上找不到一个例子。
我检查了..简单的 Spring,将 ClasspathApplicationContext 用于独立应用程序,如何重用?,其中谈到使用SingletonBeanFactoryLocator,但它最终使用 context.getBean()。
我还查看了ServiceLocatorFactoryBean,但这又是通过使用代理来按需获取 bean。
我正在寻找一种解决方案来从我的独立 java 应用程序的 main() 程序加载上下文文件(所有 bean),这样我就不想按需获取 bean。
示例代码:
public interface IReader {
public String read();
}
public class TextFileReader implements IReader {
private StringBuilder builder = null;
private Scanner scanner = null;
public TextFileReader(String fileName) throws FileNotFoundException {
scanner = new Scanner(new File(fileName));
builder = new StringBuilder();
}
public String read() {
while (scanner.hasNext()) {
builder.append(scanner.next());
builder.append(",");
}
return builder.toString();
}
}
public class SpringNoConextDataReaderClient {
private IReader reader = null;
public void setReader(TextFileReader reader) {
this.reader = reader;
}
private String fetchDataOne() {
return reader.read();
}
private String fetchDataTwo() {
return reader.read();
}
public static void main(String[] args) {
final ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
String fetchedData = context.getBean(SpringNoConextDataReaderClient.class).fetchDataOne(); // <-- reader is injected as TextFileReader in fetchDataOne which reads the file
SpringNoConextDataReaderClient client = new SpringNoConextDataReaderClient();
client.fetchDataOne(); // <-- reader is null and throws NPE, probably its lifetime ended with previous call?
System.out.println("Example 1.1: Got data without context: " + fetchDataOne);
}
}
弹簧上下文.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="reader" class="com.himalay.spring.core.basic.readers.TextFileReader">
<constructor-arg value="src/main/resources/data.txt" />
</bean>
<bean id="springNoConextDataReaderClient" class="com.himalay.spring.core.basic.SpringNoConextDataReaderClient">
<property name="reader"><ref bean = "reader" /></property>
</bean>
</beans>
谢谢你。