Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do you define spring bean configuration files

I am separating my spring bean configuration files as follows:

myapp-service.xml myapp-servlet.xml

However I am getting the error;

Error creating bean with name 'beanName' defined in ServletContext resource [/WEB-INF/myapp-servlet.xml]: Cannot resolve reference to bean 'beanService' while setting bean property 'beanService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'beanService' is defined

All I need to do (I think) is figure out how to tell Spring to read the myapp-service.xml file where the path to beanService is defined.

Which file/location is that done in?

Thanks

like image 394
Ankur Avatar asked Jan 22 '23 22:01

Ankur


2 Answers

It's defined in your web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

Alternatively in myapp-servlet.xml you could put:

<import resource="myapp-service.xml"/>
like image 200
cletus Avatar answered Jan 25 '23 12:01

cletus


if you like to include more applicationContext files and are indeed developing a web application:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-1.xml,
/WEB-INF/applicationContext-2.xml
</param-value>
</context-param>

also wildcarding works, applicationContext* will have the same effect here.

if you are bootstrapping spring context by hand e.g from code:

ApplicationContext context = 
new ClassPathXmlApplicationContext(new String[] { "applicationContext-1.xml", "applicationContext-2.xml" });
like image 37
abalogh Avatar answered Jan 25 '23 12:01

abalogh