I am developing spring-mvc application.
I am not able to access beans in my filter. I am getting below exception
org.springframework.beans.factory.NoSuchBeanDefinitionException : No qualifying bean of type [com.abc.app.SessionValue] is defined
I went throw https://stackoverflow.com/a/11709272/3898076, but not able to find the problem.
I have below entry in my web.xml
<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring_xyz-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
And spring_xyz-servlet.xml contains component-scan entry.
<context:component-scan base-package="com.abc.app" />
<context:annotation-config />
<context:spring-configured />
Filter code:
WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(fConfig.getServletContext());
springContext.getBean(SessionValue.class);
Is there any configuration issue in this?
Thanks.
If there is no other constraints you should try to use Spring MVC Handler Interceptors, because you are in Spring context, and handlers are almost similar to Filters.
http://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/HandlerInterceptor.html
Example:
http://www.journaldev.com/2676/spring-mvc-interceptors-example-handlerinterceptor-and-handlerinterceptoradapter
Related
There is a web application based on Spring 3.2. Here is how it's configured in web.xml:
<servlet>
<servlet-name>MY-REST-API</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MY-REST-API</servlet-name>
<url-pattern>/foo/*</url-pattern>
<url-pattern>/bar/*</url-pattern>
</servlet-mapping>
Spring context is configured by XML file MY-REST-API-servlet.xml placed in a classpath. Nothing unusual as you can see.
There is a code which is not in ApplicationContext. I'm looking for a way to give this code an ability to reach some beans in my Spring MVC app. AFAIK ContextSingletonBeanFactoryLocator can be used to solve such problems. But small investigation I've done with dubugger shows that Spring MVC doesn't use it by default while loading a context. None of getInstance() methods in ContextSingletonBeanFactoryLocator was called as Spring MVC bootstrapping. Hence a context received through ContextSingletonBeanFactoryLocator will be not the same as one loaded during bootstrap. I think the problem will be resolved if I make Spring MVC to use ContextSingletonBeanFactoryLocator in a context loading process.
Is there a way to configure Spring MVC to use ContextSingletonBeanFactoryLocator during bootstrap?
change your web.xml file add context-param and specify the xml file as describe below
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:MY-REST-API-servlet.xml
</param-value>
</context-param>
<servlet>
<servlet-name>MY-REST-API</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>classpath:MY-REST-API-servlet.xml </init-param>
<load-on-startup>1</load-on-startup>
</servlet>
I am new here, I am working on a project using hibernate and spring dependency injection and SOAP web service.
My problem is when I run my project in the console using this class:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-beans.xml");
IServicesPharmacie pharmacieService = (IServicesPharmacie) context.getBean("service");
context.close();
Endpoint.publish("http://localhost:3597/Pharmacies", pharmacieService);
System.out.println("The service has been published with success!");
my project work fine, because with these 3 lines:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-beans.xml");
IServicesPharmacie pharmacieService = (IServicesPharmacie) context.getBean("service");
context.close();
I can tell about my spring dependancy injection.
BUT i don't know how to run my project on a glassfish server, and tell him about my spring dependancy injection, I guess that I most have a web.xml!!!!
My spring-beans.xml is like that :
<bean class="dao.PharmImpl" id="dao"></bean>
<bean class="metier.PharMetier" id="metier">
<property name="phardao" ref="dao"></property>
</bean>
<bean class="services.ServicesPharmacie" id="service">
<property name="servmetier" ref="metier" />
</bean>
</beans>
You need to configure a ContextLoaderListener to bootstrap spring in your application. like below:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Perhaps if you are using springMVC its done as below:
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Please note URL-pattern is based on your requirement.
In my web.xml I'm declaring a ContextLoaderListener to configure spring application this way:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
In one of my spring configuration xml files, I'm using different beans profile for development and production.
<beans profile="production">
<bean />
</beans
<beans profile="development">
<bean />
</beans
How I could set the default beans profile in the web.xml? is there something similar to the following when using ContextLoaderListener instead of spring servlet:
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>production</param-value>
</init-param>
You can configure web.xml at the level of ContextLoaderListener with:
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>profileName</param-value>
</context-param>
and the level of DispatcherServlet with:
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>profileName</param-value>
</init-param>
Reference: http://spring.io/blog/2011/02/11/spring-framework-3-1-m1-released/
I have a question here, how beans defined in "applicationContext.xml" could be available to controllers defined in let's say "spring-servlet.xml", so i can skip this kind of errors i have.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '/home' defined in ServletContext resource [/WEB-INF/mmapp-servlet.xml]: Cannot resolve reference to bean 'equipementService' while setting bean property 'equipementService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'equipementService' is defined
applicationContext.xml
<?xml version="1.0" ?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="equipementService"
class="mmapp.service.SimpleEquipementService" />
<bean name="equipement1"
class="mmapp.domain.Equipement" />
</beans>
mmapp-servlet.xml
<?xml version="1.0" ?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="/home" class="mmapp.web.HelloController">
<property name="equipementService" ref="equipementService" />
</bean>
</beans>
A Spring based web-application normally has multiple runtime Spring application contexts -
A root application context(which is loaded up during the start-up of the servlet container), here is where you normally put your beans for your services - the root application context is loaded up by using a ContextLoaderListener, typically using these entries in your web.xml file:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>
One or more web application contexts, which is considered a child of the root application context, holding the beans related to the UI - controllers etc. This is configured by using a Dispatcher Servlet, a special servlet provided by Spring that knows to delegate the calls appropriately to the Spring Container. The beans in the root application context are visible to the beans defined here, BUT not the other way around - this way providing a level of separation between the UI layer and the services layer . This is a typical configuration entry in web.xml file:
<servlet>
<servlet-name>lovemytasks</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/mmapp-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
If you have defined the beans this way, the equipementService should be visible to your controller.
I'm not an expert and I'm not sure if this might be a problem, but I have a suggestion.
Could you post your web application descriptor (web.xml)? Does it contain the context-param?
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
I am trying to find a better way to access beanFactory in Spring3 Web App. Right now I setup a config.xml file with all my services that my system is going to use and in the controller I ad a line of code like:
private static XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("config.xml"));
in each controller.. Does anyone know of any better way to do this?
If you're using Spring MVC, presumably you've defined a servlet in web.xml to handle the requests, like:
<servlet>
<description></description>
<display-name>dispatcher</display-name>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
In which case you should have a Spring config file named something like dispatcher-servlet.xml in your web-inf directory. Put your bean definitions in there and they will get defined and be available when the servlet starts up.
EDIT:
Importing one bean configuation file into another, from section 3.2.2.1 of the Spring reference:
<beans>
<import resource="services.xml"/>
<import resource="resources/messageSource.xml"/>
<import resource="/resources/themeSource.xml"/>
<bean id="bean1" class="..."/>
<bean id="bean2" class="..."/>
</beans>
Autowiring bean example in controller:
#Controller
public class MyController {
#Autowired
private MyBeanClass myBeanName;
...
}