为什么自动装配不起作用?

时间:2016-08-17 19:58:04

标签: java spring spring-mvc autowired

我在控制器类中使用了@Autowired注释的PublisherService bean 一切似乎都是正确的......但是程序运行时会发生这种异常:

      org.springframework.beans.factory.BeanCreationException: 
      Error creating bean with name 'publisherController'
     : Injection of autowired dependencies failed;
     nested exception is org.springframework.beans.factory.BeanCreationException: 
     Could not autowire field: com.phoenix.data.service.PublisherService
     com.phoenix.controller.PublisherController.publisherService;
     nested exception is java.lang.IllegalArgumentException:
Can not set com.phoenix.data.service.PublisherService field com.phoenix.controller.PublisherController.publisherService to com.sun.proxy.$Proxy51 

PublisherService类:

@Service
public class PublisherService implements PublisherServicesInterface {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    @Transactional(readOnly=true)
    public Publisher getPublisher(long userId) {
        Publisher publisher= null;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("FROM Publisher AS P WHERE P.Id = :xid");
        query.setParameter("xid", userId);
        List list = query.getResultList();
        if(list.size() > 0)
            publisher = (Publisher) list.get(0);
        return publisher;
    }
}

PublisherController类:

@RestController
@RequestMapping(value="/publisher")
public class PublisherController {

    @Autowired
    PublisherService publisherService;

    @RequestMapping(value="/getPublisher")
    public ResponseEntity<Publisher> getPublisher(HttpSession session)
    {
        Publisher publisher = publisherService
                .getPublisher((long)session.getAttribute("userId"));
        if(publisher != null)
            return new ResponseEntity<Publisher>(publisher,HttpStatus.OK);

        return new ResponseEntity<Publisher> (publisher, HttpStatus.NOT_FOUND);
    }
}

配置类:

1- context config:

@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.phoenix" })
@PropertySource(value = { "classpath:hibernate.properties" })
public class ApplicationConfig {

    @Autowired
    private Environment environment;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan("com.phoenix.data.entity");
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
    }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
        dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
        dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
        dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
        return dataSource;
    }

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
        properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
        return properties;
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory s) {
        HibernateTransactionManager txManager = new HibernateTransactionManager();
        txManager.setSessionFactory(s);
        return txManager;
    }
    @Bean
    public HttpHeaders responseHeader(){
    HttpHeaders responseTypeHeader = new HttpHeaders();
    responseTypeHeader.add("Content-Type", "application/json; charset=utf-8");
    return responseTypeHeader;
    }
}

2-dispatcher config:

@Configuration
@EnableWebMvc
@ComponentScan({"com.phoenix.controller","com.phoenix.data.service"})
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
        .addResourceLocations("/WEB-INF/resources/");
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        registry.viewResolver(viewResolver);
    }


}

0 个答案:

没有答案