Please note that; the below code will create new application context instead of using the already loaded one.
private static final ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");
Also note that beans.xml
should be part of src/main/resources
means in war it is part of WEB_INF/classes
, where as the real application will be loaded through applicationContext.xml
mentioned at Web.xml
.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>META-INF/spring/applicationContext.xml</param-value>
</context-param>
It is difficult to mention applicationContext.xml
path in ClassPathXmlApplicationContext
constructor. ClassPathXmlApplicationContext("META-INF/spring/applicationContext.xml")
wont be able to locate the file.
So it is better to use existing applicationContext by using annotations.
@Component
public class OperatorRequestHandlerFactory {
public static ApplicationContext context;
@Autowired
public void setApplicationContext(ApplicationContext applicationContext) {
context = applicationContext;
}
}