2016-04-07 7 views
0

を@Autowiredことはできませんが、私はそれはこれらが私の設定、DAOおよびサービスです春ブーツは、私が質問に会った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); 
     } 
    } 

T彼のDAO

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 
+0

コードが追加されました。 –

+0

イメージを削除し、スニペットだけでなく、* full *スタックトレースを追加してください。また、まずは設定クラスを削除し、Springブートで提供されたデータソースを使用することをお勧めします(別の複雑さのレイヤーを追加する理由)、独自のロールバックではなくmybatisスプリングブートスターターを使用してください。 –

+0

申し訳ありませんが、あなたは何を意味するのか分かりません..... –

答えて

0

です私はあなたの問題は、この注釈かもしれ信じる:

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

私は持っていません前に見た(コードでもドキュメンテーションでも) )ワイルドカードを使用することが可能であることを示します。正確なパッケージ名を使用してみてください。

私はそれがサブパッケージもスキャンしていると思うので、パッケージ構造を変更して、すべてのDAOを単一パッケージのツリーに収めたいかもしれません。

+0

いいえ、APIの下に多数のモジュールがあります。正確なパッケージ名を使用すると、他のものはスキャンできません。 –

+0

私の回答が更新されました。その他のオプションはmybatis-springプロジェクトにワイルドカード機能を提供することです:)。しかし、あなたが望むのであれば、まずメンテナに話す必要があります。 – luboskrnac

+0

良い提案、私は問題を解決します。 MybatisConfigurationのEntityManage.classが原因ですが、その理由はわかりません。再びThx :) –

関連する問題