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

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 { ...... }

Related

No mapping found for HTTP request with URI [/FitnessTracker/]

I am getting an warnning in springmvc
as No mapping found for HTTP request with URI [/FitnessTracker/] in DispatcherServlet with name 'fitTrackerServlet'
INFO: FrameworkServlet 'fitTrackerServlet': initialization completed in 2194 ms
Feb 26, 2017 9:43:08 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/FitnessTracker/] in DispatcherServlet with name 'fitTrackerServlet'
When i press url http://localhost:8080/fitnessTracker/ I am getting 404 error.
Thanks
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>fitTrackerServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>fitTrackerServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<display-name>Archetype Created Web Application</display-name>
</web-app>
and my servlet-config.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.pluralsight.controller"></context:component-scan>
<!--
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
</beans>
and my controller
#Controller
public class HelloController {
#RequestMapping(value = "/greeting")
public String sayHello (Model model) {
System.out.println("Test");
model.addAttribute("greeting", "Hello WorldX");
return "hello";
}
}
Does your component-scan base-package match the package where your HelloController is in? If not, that is the problem.
<context:component-scan base-package="com.pluralsight.controller"></context:component-scan>
Configures component scanning directives for use with #Configuration
classes. Either basePackageClasses() or
basePackages() (or its alias value()) may be specified to define
specific packages to scan.
Spring does not find your HelloController and so doesn't map anything.
Otherwise you could also follow these steps.
I am wondering why you have you application configured with XML. With this tool you can start a maven project with a Spring boot application that will be, by default, already configured to build exactly what you want.
Then you only need a class with:
#RestController
#RequestMapping("/fitnessTracker")
public class HelloController {
#RequestMapping(value = "/greeting")
public String sayHello (Model model) {
System.out.println("Test");
model.addAttribute("greeting", "Hello WorldX");
return "hello";
}
}
And that's it, Spring will do the rest for you ;)
In my case the problem was that i was using this url-pattern
<url-pattern>/*</url-pattern>
instead of this
<url-pattern>/</url-pattern>
and also i had to add
#RequestMapping(value = "/fitnessTracker")
under the
#Controller
annotation.
I found the solution at : https://www.baeldung.com/spring-mvc-404-error for the pattern.

I am getting a HTTP Status 500 - Servlet.init() for servlet HelloWorld threw exception

Looked int other posts of "Servlet.init() for servlet " didnt help.
This is not a Maven or Gradle, so no pom.This is a plain simple spring project, which I am still struggling with. Below the snippets is the actuall error.
Hellocontroller.java.
package controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.*;
//import com.sun.javafx.collections.MappingChange.Map;
public class Hellocontroller implements Controller {
#Override
public ModelAndView handleRequest(HttpServletRequest req,
HttpServletResponse res) throws Exception {
String name = req.getParameter("name");
Map m = new HashMap();
m.put("msg", "Hello ...."+name);
ModelAndView mav =new ModelAndView("success",m);
return mav;
}
}
HelloWorld-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean name="./hello.ds" class="controller.Hellocontroller">
</bean>
<bean class="org.springframework.web.servlet.InternalViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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-app_3_0.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>*.ds</url-pattern>
</servlet-mapping>
</web-app>
success.jsp
${msg}
index.jsp
<h1>HelloWorld</h1>
<form action ="./hello.ds">
NAME:<input type = "text" name="name">
SUBMIT<input type = "submit" value="sayHello">
</form>
Errors:
type Exception report
message Servlet.init() for servlet HelloWorld threw exception
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet.init() for servlet HelloWorld threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501
root cause
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.web.servlet.InternalViewResolver] for bean with name 'org.springframework.web.servlet.InternalViewResolver#0' defined in ServletContext resource [/WEB-INF/HelloWorld-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.web.servlet.InternalViewResolver
org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1208)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:570)
org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1277
There's an error in your configuration. The class org.springframework.web.servlet.InternalViewResolver doesn't exist (could it be you copy/pasted this from an ancient spring mvc example?).
Try replacing it with org.springframework.web.servlet.view.InternalResourceViewResolver. I cannot guarantee it works, but it's worth trying.

Trouble spring injection in JSF2 Bean with annotation

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" />

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.

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