春天靴子不能@Autowired dao

时间:2016-04-07 08:26:58

标签: spring-boot

我遇到了一个问题,我尝试启动项目,但它失败了

这些是我的配置,dao和服务,任何人都可以告诉我为什么,thx很多

这是DataBaseConfiguration:

@Configuration
@EnableTransactionManagement
public class DataBaseConfiguration implements EnvironmentAware {

    private RelaxedPropertyResolver propertyResolver;

    private static Logger log = LoggerFactory.getLogger(DataBaseConfiguration.class);

    @Override
    public void setEnvironment(Environment env) {
        this.propertyResolver = new RelaxedPropertyResolver(env, "dataSource.");
    }

    @Bean(destroyMethod = "close")
    public DataSource dataSource() {
        log.debug("Configuring DataSource");

        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(propertyResolver.getProperty("url"));
        druidDataSource.setDriverClassName(propertyResolver.getProperty("driverClassName"));
        druidDataSource.setUsername(propertyResolver.getProperty("username"));
        druidDataSource.setPassword(propertyResolver.getProperty("password"));

        return druidDataSource;
    }
}

这是MybatisConfiguration:

    @Configuration
    @ConditionalOnClass({ EnableTransactionManagement.class,    EntityManager.class})
    @AutoConfigureAfter({ DataBaseConfiguration.class })
    @MapperScan("com.future.api.**.dao")
    public class MybatisConfiguration implements EnvironmentAware {

        private static Log logger = LogFactory.getLog(MybatisConfiguration.class);

        private RelaxedPropertyResolver propertyResolver;

        @Autowired
        private DataSource dataSource;

        @Override
        public void setEnvironment(Environment env) {
            this.propertyResolver = new RelaxedPropertyResolver(env, "mybatis.");
        }

        @Bean
        @ConditionalOnMissingBean
        public SqlSessionFactory sqlSessionFactory() {
            try {
                SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
                sessionFactory.setDataSource(dataSource);
                sessionFactory.setTypeAliasesPackage(propertyResolver
                        .getProperty("typeAliasesPackage"));
                sessionFactory
                        .setMapperLocations(new PathMatchingResourcePatternResolver()
                                .getResources(propertyResolver
                                        .getProperty("mapperLocations")));
                sessionFactory
                        .setConfigLocation(new DefaultResourceLoader()
                                .getResource(propertyResolver
                                        .getProperty("configLocation")));
                return sessionFactory.getObject();
            } catch (Exception e) {
                logger.warn("Could not configure mybatis session factory");
                return null;
            }
        }

        @Bean
        @ConditionalOnMissingBean
        public DataSourceTransactionManager transactionManager() {
            return new DataSourceTransactionManager(dataSource);
        }
    }

这是道歉

 public interface UserMapper extends BaseDao<User,UserCriteria ,String> {
        @SelectProvider(type=UserSqlProvider.class, method="countByExample")
        int countByExample(UserCriteria example);

        @DeleteProvider(type=UserSqlProvider.class, method="deleteByExample")
        int deleteByExample(UserCriteria example);

        @Delete({
            "delete from t_user",
            "where id = #{id,jdbcType=VARCHAR}"
        })
        int deleteByPrimaryKey(String id);

        @Insert({
            "insert into t_user (id, username, ",
            "password, created_by, ",
            "created_date, last_modified_by, ",
            "last_modified_date)",
            "values (#{id,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, ",
            "#{password,jdbcType=VARCHAR}, #{createdBy,jdbcType=VARCHAR}, ",
            "#{createdDate,jdbcType=DATE}, #{lastModifiedBy,jdbcType=VARCHAR}, ",
            "#{lastModifiedDate,jdbcType=DATE})"
        })
        int insert(User record);
    }

这是服务

@Service
@Transactional
public class UserServiceImp extends BaseServiceImpl<User,UserCriteria> implements UserService {

    @Autowired
    private UserMapper userDao;

    @Override
    protected BaseDao<User, UserCriteria, String> getDao() {
        return userDao;
    }
}

此错误信息

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.future.api.user.dao.UserMapper com.future.api.user.service.imp.UserServiceImp.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.future.api.user.dao.UserMapper] 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.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    ... 22 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.future.api.user.dao.UserMapper] 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.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    ... 24 common frames omitted

1 个答案:

答案 0 :(得分:0)

我相信你的问题可能是这个注释:

@MapperScan("com.future.api.**.dao")

我以前(代码和文档中)没有看到可以使用通配符。请尝试使用确切的包名称。

我相信它也会扫描子包,因此您可能希望更改包结构,以便在单个包下将所有DAO放在树中。