java - Why Spring only autowired beans in controller -



java - Why Spring only autowired beans in controller -

my web application implemented spring can @autowire beans in controllers, not services. find below description spring document(section 4.9) it's not giving answer.

looks annotations on beans in same application context in defined. means that, if set in webapplicationcontext dispatcherservlet, checks @autowired beans in controllers, , not services. see section 16.2, “the dispatcherservlet” more information.

in section 16.2, neither no explain. can help me? have code below.

in config package, there 3 classes

public class webapp extends abstractannotationconfigdispatcherservletinitializer { @override protected class<?>[] getrootconfigclasses() { homecoming new class<?>[]{persistconfig.class}; } @override protected class<?>[] getservletconfigclasses() { homecoming new class<?>[]{ webmvcconfig.class }; } @override protected string[] getservletmappings() { homecoming new string[]{ "/" }; } } @configuration public class persistconfig { @bean public jdbctemplate jdbctemplaterepository() { simpledriverdatasource datasource = new simpledriverdatasource(); datasource.setdriverclass(com.mysql.jdbc.driver.class); datasource.seturl("jdbc:mysql://localhost:3306/categorybags_db"); datasource.setusername("root"); datasource.setpassword(""); homecoming new jdbctemplate(datasource); } } @componentscan({"web.controller", "persistence.services"}) @enablewebmvc @configuration public class webmvcconfig extends webmvcconfigureradapter { @override public void addresourcehandlers(resourcehandlerregistry registry) { registry.addresourcehandler("/res/**").addresourcelocations("/res/"); } @bean public viewresolver viewresolver() { servletcontexttemplateresolver templateresolver = new servletcontexttemplateresolver(); templateresolver.setcacheable(false); templateresolver.setprefix("/web-inf/jsp/"); templateresolver.setsuffix(".jsp"); templateresolver.settemplatemode("html5"); springtemplateengine templateengine = new springtemplateengine(); templateengine.settemplateresolver(templateresolver); thymeleafviewresolver viewresolver = new thymeleafviewresolver(); viewresolver.setcharacterencoding("utf-8"); viewresolver.setorder(1); viewresolver.settemplateengine(templateengine); homecoming viewresolver; } }

in bundle persistence.services, class dbservices want autowire jdbctemplate null, utilize same statement in controller jdbctemplate not null.

@repository @configurable public class dbservices { @autowired jdbctemplate jdbctemplate; //some code here }

probably problem in configuration.

check bean definiton files , search <context:component-scan base-package="your.package" /> tags.

you need specify packages set kind of @component, @controller @service or @repository.

java spring spring-mvc

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -