设置限定符后,获取org.springframework.beans.factory.NoSuchBeanDefinitionException

时间:2017-05-05 13:45:59

标签: spring dependency-injection

我是SpringMVC的新手。在我设置NoSuchBeanDefinitionExcepiton之后,我仍然得到Qualifier。 代码:

RootConfig.java

@Configuration
@ComponentScan(basePackages = {"spittr"},
excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,value = EnableWebMvc.class)
})
@ActiveProfiles("mysql")
@Import(value = {SecurityConfig.class, SqliteDataConfig.class,MysqlDataConfig.class,BaseDataConfig.class})
public class RootConfig {}

WebInitializer.java

public class SpittrWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{
         RootConfig.class
        };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{
          WebConfig.class
        };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        registration.setMultipartConfig(new MultipartConfigElement(
                "/tmp/uploads/",2048*1024,4096*1024,0
        ));
    }
}

BaseDataConfig.java

@Configuration
    @PropertySource("classpath:META-INF/persistence.properties")
    public  abstract class BaseDataConfig {
        @Autowired
        Environment environment;
        @Bean
        @Autowired
        public HibernateTransactionManager transactionManager(SessionFactory sessionFactory){
            HibernateTransactionManager transactionManager=new HibernateTransactionManager();
            transactionManager.setSessionFactory(sessionFactory);
            return transactionManager;
        }

        @Bean
        public PersistenceExceptionTranslationPostProcessor exceptionTranslationPostProcessor() {
            return new PersistenceExceptionTranslationPostProcessor();
        }

        abstract Properties hibernateProperties();

    }

MysqlDataConfig.java

@Configuration
public class MysqlDataConfig extends BaseDataConfig{
    @Bean
    @Qualifier("mysql.datasource")
    public DriverManagerDataSource dataSource(){
        DriverManagerDataSource dataSource=new DriverManagerDataSource();
        dataSource.setUrl(environment.getProperty("m.jdbc.url"));
        dataSource.setDriverClassName(environment.getProperty("m.jdbc.driverClassName"));
        dataSource.setUsername(environment.getProperty("m.username"));
        dataSource.setPassword(environment.getProperty("m.password"));
        return dataSource;
    }

    @Bean
    @Qualifier("mysql.sessionfactory")
    public LocalSessionFactoryBean sessionFactoryBean(){
        LocalSessionFactoryBean sessionFactoryBean=new LocalSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource());
        sessionFactoryBean.setPackagesToScan("spittr.model");
        sessionFactoryBean.setHibernateProperties(hibernateProperties());
        return sessionFactoryBean;
    }

    @Override
    Properties hibernateProperties() {
       Properties properties=new Properties();
       properties.setProperty("hibernate.hbm2ddl.auto",environment.getProperty("hibernate.hbm2ddl.auto"));
       properties.setProperty("hibernate.dialect",environment.getProperty("m.hibernate.dialect"));
       properties.setProperty("hibernate.globally_quoted_identifiers","true");
       properties.setProperty("hibernate_show_sql",environment.getProperty("hibernate.show_sql"));
       return properties;
    }
}

SecurityConfig.java

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    @Qualifier("mysql.datasource")
    DataSource dataSource;
    @Override
    @Autowired
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource)
                .usersByUsernameQuery(
                        "select first_name,last_name,user_name,password,enabled from spitter where user_name=?"
                )
                .authoritiesByUsernameQuery("select user_name,role from spitter where user_name=?");

    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
                .antMatchers("/visit/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_USER')")
                .and().formLogin();
        System.out.println("http config");
    }
}

SecurityConfig我得到:

caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=mysql.datasource)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    ... 28 more

出了什么问题以及如何解决?提前谢谢。

0 个答案:

没有答案