Trouble spring injection in JSF2 Bean with annotation - spring

I have a trouble with Spring Injection in my web project. I must use in a JSF2 bean.
Show my work :
SgbdServiceImpl.java (shorted)
#Service
public class SgbdServiceImpl implements SgbdService {
#Override
public List<Sgbd> findAll() {
return null;
}
#Override
public Sgbd findOneByName(String nom) {
return null;
}
}
SgbdBean
#Component
#SessionScoped
#ManagedBean(name="sgbd")
public class SgbdBean {
#Autowired
SgbdService sgbdService;
public List<Sgbd> findAll(){
return sgbdService.findAll();
}
}
I put this configuration in the file : web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
This Spring configuration in applicationContext.xml :
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="main.java.com.erdf.agir.services" />
</beans>
And, in faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd"
version="2.1">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
I would like call findAll() from service but i obtain all time nullPointerException from sgbdService attribut (Autowired failled ?)
I follow this example : http://rsuna.blogspot.fr/2013/05/how-to-integrate-jsf-20-with-spring-3.html
Did I miss anything ?

You have a context clash; using both #Component and #ManagedBean on the same class definition put your bean in two contexts: JSF and Spring's. Let's now establish that #Autowired will not work in the JSF context.
You could get rid of the spring-based annotations and go with a JSF-centric setup. What this will leave with you with
#SessionScoped
#ManagedBean(name="sgbd")
public class SgbdBean {
#ManagedProperty(value="#{sgbdServiceImpl}")
SgbdService sgbdService;
public List<Sgbd> findAll(){
return sgbdService.findAll();
}
}
You could stick with a strictly spring-centric approach with
#Component
public class SgbdBean {
#Autowired
SgbdService sgbdService;
public List<Sgbd> findAll(){
return sgbdService.findAll();
}
}

Things I have encountered
1) Better to mention service name with annotation - #Service("sgbdService")
2) Rather than #ManagedBean it is better to use #Qualifier annotation -
#Qualifier("sgbdBean")
3) Add <context:sping-configured/> entry to applicationContext.xml file
4) Try with top level component scan entry as
<context:component-scan base-package="main.java.com.erdf" />

Related

#PostConstuct does not appear to work and #autowire gives an error

I am new to spring and am creating a spring web application.
The application I'm writing has a Class PreLoadService. In this class is a method defined with #PostConstruct that calls a DAO to load the data. The DAO instance is declared in the class with the #autowired.
The Controller for the JSP then declares an instance of the PreLoadService and calls the getter to retrieve the data that should have been loaded in the #PostConstruct. The data is never loaded and an exception is also thrown on the #autowired.
Since this did not work I tried a simple Hello World version to write a message and received the same issue. I will post this. In the WEB_INF folder I have a web.xml and a spring3-servlet.xml. In the SRC folder I have an applicationContext.xml. I am running on Tomcat 7.
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring3MVC</display-name>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>root.webpath</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring3</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring3</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>/</url-pattern>
</servlet-mapping></web-app>
spring3-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--will allow Spring to load all the components from package and all its child packages-->
<mvc:annotation-driven />
<context:component-scan
base-package="com.nikki.spring3.controller" />
<!-- will resolve the view and add prefix string /WEB-INF/jsp/ and suffix .jsp to the view in ModelAndView. -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.nikki.spring3">
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<bean id="helloWorldService"
class="com.nikki.spring3.beansit.HelloWorldService">
<property name="message" value="Preloading Init Config and Data" />
HelloWorldService
public class HelloWorldService {
private String message;
public void setMessage(String message){
this.message = message;
}
public String getMessage(){
System.out.println("Your Message : " + message);
return message;
}
#PostConstruct
public void init(){
System.out.println("Bean is going through init.");
}
#PreDestroy
public void destroy(){
System.out.println("Bean will destroy now.");
}
}
HelloWorldController
#Controller
public class HelloWorldController {
#Autowired
HelloWorldService helloWorldService;
/* RequestMapping annotation tells Spring that this Controller should
* process all requests beginning with /hello in the URL path.
* That includes /hello/* and /hello.html.
*/
#RequestMapping("/hello")
public ModelAndView helloWorld() {
String message =helloWorldService.getMessage();
//"Hello World, Spring 3.0!";
return new ModelAndView("hello", "message", message);
}
}
Error Message
Exception
SEVERE: StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0':
Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'helloWorldController':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: com.nikki.spring3.beansit.HelloWorldService com.nikki.spring3.controller.HelloWorldController.helloWorldService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [com.nikki.spring3.beansit.HelloWorldService] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
I appreciate any help. Thanks.
If you have config files other than xxx-servlet.xml you need to let know spring that these files exists. To do that you have to use contextConfigLocation along with ContextLoadListener. Try to add the following lines in your web.xml. If the applicationContext.xml exists in WEB-INF folder of the project use the following.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
I think you had your applicationContext.xml under src folder. In that case use as below
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
Try creating an interface for HelloWorldService and autowire with that interface in your controller. Spring create bean of proxy of class HelloWorldService, so HelloWorldService itself may not be available to be autowired. Try it.
in your case you want to inject a bean that has not yet created
add #Service
#Service
public class HelloWorldService { ...... }

Spring Injection with Servlets: NoSuchBean

New to Spring, and working with Spring 3.2.5 trying to get injection to work with a servlet in a vanilla web app (i.e., it's not a Spring MVC web app - it's a pre-existing app I'm extending using the Spring framework). The container is Tomcat 7.0.47.
My problem is that I'm getting NoSuchBeanDefinitionException errors (No bean named 'MyServlet' is defined) when I hit the servlet. There are no errors at startup, so at least one of my beans (the ServiceImplementation bean) is getting successfully instantiated. The problem appears to be with finding the HttpRequestHandler-derived bean (MyServlet) when a new HTTP request comes in.
The full stack trace for the exception is:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'MyServlet' is defined
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:570)
org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1114)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:279)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1121)
org.springframework.web.context.support.HttpRequestHandlerServlet.init(HttpRequestHandlerServlet.java:58)
com.random.webapp.MySpringServlet.init(Unknown Source)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
java.lang.Thread.run(Thread.java:662)
I followed this pattern for my setup:
http://andykayley.blogspot.com/2008/06/how-to-inject-spring-beans-into.html
...with one minor (I think) twist. I have a class derived from HttpRequestHandlerServlet so that I can override the init method with some application-specific stuff. The extension class looks like this:
public class MySpringServlet extends HttpRequestHandlerServlet
{
public void init() throws ServletException
{
super.init();
appSpecificInit();
}
}
The servlet I want injected looks like this:
public class MyServlet implements HttpRequestHandler
{
private IService _service = null;
public void setService( IService theService ) {
_service = theService;
}
#Override
public void handleRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
_service.DoSomething();
}
}
The implementation I want it injected with looks like this:
public class ServiceImplementation implements IService
{
#Override
public void DoSomething()
{
// some code goes here
}
}
These are the relevant entries in web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml /WEB-INF/implementation.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.random.webapp.MySpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/myservlet/*</url-pattern>
</servlet-mapping>
</web-app>
This is the applicationContext.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="MyServlet" class="com.random.webapp.MyServlet">
<property name="Service" ref="ServiceImplementation" />
</bean>
</beans>
...and this is what implementation.xml looks like:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="ServiceImplementation" class="com.random.webapp.ServiceImplementation">
</bean>
</beans>
I've been back and forth between the web.xml, applicationContext.xml, and implementation.xml files to double and triple check my configuration, and I don't see anything wrong with any of them, but I'm obviously missing something.
Anyone have any ideas?
The exception you are getting
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'MyServlet' is defined
occurs in the init() method of the HttpRequestHandlerServlet which tries to load a delegate HttpRequestHandler object from your context based on the name you give the HttpRequestHandlerServlet in your web.xml
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.random.webapp.MySpringServlet</servlet-class>
</servlet>
In the configuration above, that would be MyServlet. Although it appears you have it correct in
<bean id="MyServlet" class="com.random.webapp.MyServlet">
<property name="Service" ref="ServiceImplementation" />
</bean>
make sure you are loading the correct context file as declared here
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml /WEB-INF/implementation.xml</param-value>
</context-param>

Spring: How to define database config?

I try to execute simple request to MySql database via jdbcTemplate but I have an error when framework load and parse xml file whicj define my datasource:
<?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:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring_training"/>
<property name="username" value="root"/>
<property name="password" value="pass"/>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringTrainingTemplate</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml /WEB-INF/jdbc-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
and Controller that invoke it:
#Controller
public class HomeController {
#Autowired
private ExampleService exampleService;
#RequestMapping(value = "/details", method = RequestMethod.GET)
public String details(Model model) {
ApplicationContext context = new ClassPathXmlApplicationContext("jdbc-config.xml");
ExampleDao dao = (ExampleDao) context.getBean("ExampleDao");
List<Application> list = dao.getAllApplications();
model.addAttribute("application", list.get(0).getName());
model.addAttribute("descriptionOfApplication", list.get(0).getDescription());
return "details";
}
}
public class ExampleDao {
private String request = "select * from application";
private JdbcTemplate jdbcTemplate;
#Autowired
private DataSource dataSource;
public ExampleDao(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public List<Application> getAllApplications() {
List<Application> applications = this.jdbcTemplate.query(request, new RowMapper<Application>() {
#Override
public Application mapRow(ResultSet rs, int i) throws SQLException {
Application application = new Application();
application.setName(rs.getString("name"));
application.setType(rs.getString("type"));
application.setDescription(rs.getString("description"));
application.setDownloads(rs.getInt("downloads"));
return application;
}
});
return applications;
}
}
Whe I run it and input http://localhost:8080/details I have got an 500 exception with stacktrace with this message:
root cause
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [jdbc-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [jdbc-config.xml] cannot be opened because it does not exist
Can you explain me how to configure jdbc connection in rigth way or if my approach is correct where I should look for a solution of my issue? All help would be appreciated. Thanks.
Spring cannot find your jdbc-config.xml configuration file.
You can put it in your classpath instead of the WEB-INF folder and load it in your web.xml like this:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config.xml,classpath:jdbc-config.xml</param-value>
</context-param>
A good practice is to create folders main and resources in your src folder and to add them in the classpath. Then you can put the spring config file in the src/resources folder.
class path resource [jdbc-config.xml] cannot be opened because it does
not exist
Is the file name correct, where is it located? the file specifying the db connection is not where you said it should be - on the classpath.

Spring configuration in JBossWS

I'm trying to expose a web service using JBossWS (native stack) and also take advantage of Spring's dependency injection. Here is a scrubbed down version of my code:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Test Service</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>EndpointService</servlet-name>
<servlet-class>com.blah.webservice.EndpointService</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EndpointService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:spring-configured />
<context:load-time-weaver />
<context:annotation-config />
<context:component-scan base-package="com.blah.webservice" />
</beans>
EndpointService.java
package com.blah.webservice;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
#Service
#WebService
#SOAPBinding(parameterStyle=SOAPBinding.ParameterStyle.BARE)
public class EndpointService {
private TestService testService;
public EndpointService() {}
#Autowired
public EndpointService(TestService testService) {
this.testService = testService;
}
#WebMethod
public String endpointEcho(String echo) {
return echo;
}
#WebMethod
public String serviceEcho(String echo) {
return testService.serviceEcho(echo);
}
}
TestService.java:
package com.blah.webservice;
import org.springframework.stereotype.Service;
#Service
public class TestService {
public TestService() {}
public String serviceEcho(String echo) {
return echo;
}
}
When I build this and deploy to JBoss, it starts up just fine and I can see Spring is pre-instantiating my classes but when I issue calls to the web service, endpointEcho works as expected while serviceEcho throws a NullPointerException. It seems that when JBossWS instantiates the endpoint class, it isn't finding out about my Spring configuration. Is there a simple way that I can tell JBossWS about Spring? I feel like I'm either missing some very small detail, or I'm approaching this all wrong. Any ideas?
Your service must extend SpringBeanAutowiringSupport to be able to take advantage of the autowiring support.

How to #Autowire objects in Validator classes?

Is it possible to Autowire an object in a Validation class? I keep getting null for the object that is supposed to be Autowired...
Are your Validation class an enabled Spring bean ??? If not, you always will get null for your object autowired. Make sure you have enabled your Validation class.
And do not forget enable The Annotation config bean post-processor (see <context:annotation-config /> element)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
</beans>
How to enable your Validation class as a managed Spring bean. Either
1° By using xml (As shown above)
<beans ...>
<bean class="AccessRequestValidator"/>
<context:annotation-config />
</beans>
2° By using annotation instead (Notice #Component just above class)
#Component
public class AccessRequestValidator implements Validator {
}
But to enable Spring annotated component scanning, you must enable a bean-post processor (notice <context:component-scan element)
<beans ...>
<context:annotation-config />
<context:component-scan base-package="<PUT_RIGHT_HERE_WHICH_ROOT_PACKAGE_SHOULD_SPRING_LOOK_FOR_ANY_ANNOTATED_BEAN>"/>
</beans>
Inside your Controller, just do it (Do not use new operator)
Choose one of the following strategies
public class MyController implements Controller {
/**
* You can use FIELD #Autowired
*/
#Autowired
private AccessRequestValidator accessRequestValidator;
/**
* You can use PROPERTY #Autowired
*/
private AccessRequestValidator accessRequestValidator;
private #Autowired void setAccessRequestValidator(AccessRequestValidator accessRequestValidator) {
this.accessRequestValidator = accessRequestValidator;
}
/**
* You can use CONSTRUCTOR #Autowired
*/
private AccessRequestValidator accessRequestValidator;
#Autowired
public MyController(AccessRequestValidator accessRequestValidator) {
this.accessRequestValidator = accessRequestValidator;
}
}
UPDATE
Your web app structure should looks like
<CONTEXT-NAME>/
WEB-INF/
web.xml
<SPRING-SERVLET-NAME>-servlet.xml
business-context.xml
classes/
/com
/wuntee
/taac
/validator
AccessRequestValidator.class
lib/
/**
* libraries needed by your project goes here
*/
Your web.xml should looks like (NOTICE contextConfigLocation context-param and ContextLoaderListener)
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<!--If your business-context.xml lives in the root of classpath-->
<!--replace by classpath:business-context.xml-->
<param-value>
/WEB-INF/business-context.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name><SPRING-SERVLET-NAME></servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name><SPRING-SERVLET-NAME></servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
Your <SPRING-SERVLET-NAME>-servlet.xml should looks like (Notice i am using Spring 2.5 - replace if you are using 3.0)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!--ANY HANDLER MAPPING-->
<!--ANY VIEW RESOLVER-->
<context:component-scan base-package="com.wuntee.taac"/>
<context:annotation-config/>
</beans>
Trying to follow what you show above, I am still getting a null pointer:
context.xml:
<context:annotation-config />
<context:component-scan base-package="com.wuntee.taac"/>
AccessRequestValidator.java
package com.wuntee.taac.validator;
#Component
public class AccessRequestValidator implements Validator {
#Autowired
private UserAccessCache userAccessCache;
...
}
business-context.xml:
<bean id="userAccessCache" class="com.wuntee.taac.controller.UserAccessCache">
<property name="cadaDao" ref="cadaDao" />
<property name="adDao" ref="adDao" />
</bean>
Does the scanner recursively scan the tree?

Resources