title: Spring: @Autowired for POJO, that not managed by Spring
body: <p>i use the @Autowired to inject some services in the POJOs. The servieces are registred as Beans.</p>
  
  <p>If once POJO is managed by Spring (it is registred as Bean on Spring Config too), so i have no problem with injected services.</p>
  
  <p>But if i created "classic" POJO and create it by "new" , so no Services will be injected.</p>
  
  <p>My question: is it possible to configurate Spring, to realize the @Autowired injection?</p>
  
  <p>I use the Spring 4.0.3 that is configured by JavaConfig:</p>
  
  <p>====</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.List;
  
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
  import org.springframework.core.Ordered;
  import org.springframework.http.MediaType;
  import org.springframework.security.oauth2.provider.ClientDetailsService;
  import org.springframework.security.oauth2.provider.approval.ApprovalStore;
  import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
  import org.springframework.security.oauth2.provider.token.TokenStore;
  import org.springframework.web.accept.ContentNegotiationManagerFactoryBean;
  import org.springframework.web.servlet.View;
  import org.springframework.web.servlet.ViewResolver;
  import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
  import org.springframework.web.servlet.view.InternalResourceViewResolver;
  import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
  
  import com.tr.oauth.service.PhotoInfo;
  import com.tr.oauth.service.PhotoService;
  import com.tr.oauth.service.impl.PhotoServiceImpl;
  import com.tr.oauth.service.oauth.ExtendedApprovalStoreUserApprovalHandler;
  
  @Configuration
  @EnableWebMvc
  public class WebMvcConfig extends WebMvcConfigurerAdapter {
  
      @Bean
      public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
          return new PropertySourcesPlaceholderConfigurer();
      }
  
     @Bean
      public ContentNegotiatingViewResolver contentViewResolver() throws Exception {
          ContentNegotiationManagerFactoryBean contentNegotiationManager = new ContentNegotiationManagerFactoryBean();
          contentNegotiationManager.addMediaType("json", MediaType.APPLICATION_JSON);
  
          InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
          viewResolver.setPrefix("/WEB-INF/jsp/");
          viewResolver.setSuffix(".jsp");
  
          MappingJackson2JsonView defaultView = new MappingJackson2JsonView();
          defaultView.setExtractValueFromSingleKeyModel(true);
  
          ContentNegotiatingViewResolver contentViewResolver = new ContentNegotiatingViewResolver();
          contentViewResolver.setContentNegotiationManager(contentNegotiationManager.getObject());
          contentViewResolver.setViewResolvers(Arrays.&lt;ViewResolver&gt;asList(viewResolver));
          contentViewResolver.setDefaultViews(Arrays.&lt;View&gt;asList(defaultView));
          return contentViewResolver;
      }
  
      @Override
      public void addViewControllers(ViewControllerRegistry registry) {
          registry.addViewController("/login").setViewName("login");
          registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
      }
  
      @Bean
      public PhotoServiceUserController photoServiceUserController(PhotoService photoService) {
         PhotoServiceUserController photoServiceUserController = new PhotoServiceUserController();
         return photoServiceUserController;
      }
  
      @Bean
      public PhotoController photoController(PhotoService photoService) {
          PhotoController photoController = new PhotoController();
          photoController.setPhotoService(photoService);
          return photoController;
      }
  
      @Bean
      public AccessConfirmationController accessConfirmationController(ClientDetailsService clientDetailsService, ApprovalStore approvalStore) {
          AccessConfirmationController accessConfirmationController = new AccessConfirmationController();
          accessConfirmationController.setClientDetailsService(clientDetailsService);
          accessConfirmationController.setApprovalStore(approvalStore);
          return accessConfirmationController;
      }
  
      @Bean
      public PhotoServiceImpl photoServices() {
          List&lt;PhotoInfo&gt; photos = new ArrayList&lt;PhotoInfo&gt;();
          photos.add(createPhoto("1", "marissa"));
          photos.add(createPhoto("2", "paul"));
          photos.add(createPhoto("3", "marissa"));
          photos.add(createPhoto("4", "paul"));
          photos.add(createPhoto("5", "marissa"));
          photos.add(createPhoto("6", "paul"));
  
          PhotoServiceImpl photoServices = new PhotoServiceImpl();
          photoServices.setPhotos(photos);
          return photoServices;
      }
  
  
      @Bean
      public AdminController adminController(TokenStore tokenStore, ConsumerTokenServices tokenServices,
              ExtendedApprovalStoreUserApprovalHandler userApprovalHandler) {
          AdminController adminController = new AdminController();
          adminController.setTokenStore(tokenStore);
          adminController.setTokenServices(tokenServices);
          adminController.setUserApprovalHandler(userApprovalHandler);
          return adminController;
      }
  
     private PhotoInfo createPhoto(String id, String userId) {
          PhotoInfo photo = new PhotoInfo();
          photo.setId(id);
          photo.setName("photo" + id + ".jpg");
          photo.setUserId(userId);
          photo.setResourceURL("/impl/resources/" + photo.getName());
          return photo;
      }
  
      @Override
      public void configureDefaultServletHandling(
              DefaultServletHandlerConfigurer configurer) {
          configurer.enable();
      }
  }
  
  
  -----------
  
  import javax.servlet.ServletContext;
  import javax.servlet.ServletException;
  
  import org.springframework.util.ClassUtils;
  import org.springframework.web.context.WebApplicationContext;
  import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
  import org.springframework.web.filter.DelegatingFilterProxy;
  import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
  
  /**
   * @author Dave Syer
   * 
   */
  public class ServletInitializer extends AbstractDispatcherServletInitializer {
  
      @Override
      protected WebApplicationContext createServletApplicationContext() {
          AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
          context.scan(ClassUtils.getPackageName(getClass()));
          return context;
      }
  
      @Override
      protected String[] getServletMappings() {
          return new String[] { "/" };
      }
  
      @Override
      protected WebApplicationContext createRootApplicationContext() {
          return null;
      }
  
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
          super.onStartup(servletContext);
          DelegatingFilterProxy filter = new DelegatingFilterProxy("springSecurityFilterChain");
          filter.setContextAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher");
          servletContext.addFilter("springSecurityFilterChain", filter).addMappingForUrlPatterns(null, false, "/*");
      }
  
  }
  </code></pre>