2016-11-30 10 views
2

私のアプリのアクセス方法を変更したいと思います。私はcassandraのために春のデータを使用していましたが、UDTやその他のものをサポートしていないため、私はDAOパターンでdatastaxドライバを使用することに決めました。豆を初期化していない豆

しかし、私はいくつかの問題を抱えています、psringはdatastaxドライバのための豆を初期化していません、そして、私は間違いがどこにあるのか分かりません。

、これは誤りである:ここでは

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ro.automotive.app.core.DAO.interfaces.BaseDAO ro.automotive.app.core.service.BaseServiceImpl.baseDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersDAOImpl' defined in file [E:\Proiecte Intellij\Stefan\automotive\target\ROOT\WEB-INF\classes\ro\automotive\app\core\DAO\UsersDAOImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [ro.automotive.app.core.DAO.UsersDAOImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) 
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) 
... 72 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersDAOImpl' defined in file [E:\Proiecte Intellij\Stefan\automotive\target\ROOT\WEB-INF\classes\ro\automotive\app\core\DAO\UsersDAOImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [ro.automotive.app.core.DAO.UsersDAOImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) 
    ... 74 more 
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [ro.automotive.app.core.DAO.UsersDAOImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException 
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163) 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1098) 
    ... 85 more 
Caused by: java.lang.NullPointerException 
    at ro.automotive.app.core.DAO.BaseDAOImpl.<init>(BaseDAOImpl.java:29) 
    at ro.automotive.app.core.DAO.UsersDAOImpl.<init>(UsersDAOImpl.java:13) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422) 
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147) 
    ... 87 more 

は私の設定ファイルである:

@Configuration 
@EnableWebMvc 
@ComponentScan({ 
     "ro.automotive.app.core.controller", 
     "ro.automotive.app.admin.controller", 
     "ro.automotive.app.autoService.controller", 
     "ro.automotive.app.carPartsSupplier.controller", 

     "ro.automotive.app.core.service", 
     "ro.automotive.app.core.DAO" 
}) 
@Import({CassandraConfig.class, SpringSecurityConfig.class}) 
public class AppConfig extends WebMvcConfigurerAdapter { 


    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/res/**/**.js").addResourceLocations("/WEB-RES/js/"); 
     registry.addResourceHandler("/res/**/**.css").addResourceLocations("/WEB-RES/css/"); 
     registry.addResourceHandler("/res/img/**/**/**.png").addResourceLocations("/WEB-RES/img/"); 
     registry.addResourceHandler("/**/**.ico").addResourceLocations("/WEB-RES/img/favicon.ico"); 
    } 

    @Override 
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 
     configurer.enable(); 
    } 

    @Bean 
    public ViewResolver viewResolver(ContentNegotiationManager manager) { 
     InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
     resolver.setPrefix("/views/"); 
     resolver.setSuffix(".jsp"); 
     return resolver; 
    } 


} 

カサンドラの設定:

マッピングマネージャはnullです
@Configuration 
public class CassandraConfig{ 

    @Bean 
    public Cluster cluster(){ 
     PoolingOptions poolingOptions = new PoolingOptions(); 
     Cluster cluster = Cluster.builder() 
       .withClusterName("develop") 
       .addContactPoint("localhost") 
       .withPort(9042) 
       .build(); 
     return cluster; 
    } 

    @Bean 
    public MappingManager mappingManager() throws Exception { 
     MappingManager mappingManager = new MappingManager(session()); 
     return mappingManager; 
    } 

    @Bean 
    public Session session() throws Exception { 
     Session session = cluster().connect("automotive"); 
     return session; 
    } 

} 

そしてBaseDAO:

public abstract class BaseDAOImpl<T> implements BaseDAO<T> { 

    protected final Logger logger = LoggerFactory.getLogger(getClass()); 

    @Autowired 
    protected MappingManager manager; 

    protected Mapper<T> mapper; 

    public BaseDAOImpl(Class<T> clazz) { 
     mapper = manager.mapper(clazz); 
    } 

    @Override 
    public TableMetadata getTableMetadata() { 
     return mapper.getTableMetadata(); 
    } 

    @Override 
    public Statement saveQuery(T entity) { 
     return mapper.saveQuery(entity); 
    } 

    @Override 
    public Statement saveQuery(T entity, Mapper.Option... options) { 
     return mapper.saveQuery(entity, options); 
    } 

    @Override 
    public void save(T entity) { 
     mapper.save(entity); 
    } 

    @Override 
    public void save(T entity, Mapper.Option... options) { 
     mapper.save(entity, options); 
    } 

    @Override 
    public ListenableFuture<Void> saveAsync(T entity) { 
     return mapper.saveAsync(entity); 
    } 

    @Override 
    public ListenableFuture<Void> saveAsync(T entity, Mapper.Option... options) { 
     return mapper.saveAsync(entity, options); 
    } 

    @Override 
    public Statement getQuery(Object... objects) { 
     return mapper.getQuery(objects); 
    } 

    @Override 
    public T get(Object... objects) { 
     return mapper.get(objects); 
    } 

    @Override 
    public ListenableFuture<T> getAsync(Object... objects) { 
     return mapper.getAsync(objects); 
    } 

    @Override 
    public Statement deleteQuery(T entity, Mapper.Option... options) { 
     return mapper.deleteQuery(entity, options); 
    } 

    @Override 
    public Statement deleteQuery(T entity) { 
     return mapper.deleteQuery(entity); 
    } 

    @Override 
    public Statement deleteQuery(Object... objects) { 
     return mapper.deleteQuery(objects); 
    } 

    @Override 
    public void delete(T entity) { 
     mapper.delete(entity); 
    } 

    @Override 
    public void delete(T entity, Mapper.Option... options) { 
     mapper.delete(entity, options); 
    } 

    @Override 
    public ListenableFuture<Void> deleteAsync(T entity) { 
     return mapper.deleteAsync(entity); 
    } 

    @Override 
    public ListenableFuture<Void> deleteAsync(T entity, Mapper.Option... options) { 
     return mapper.deleteAsync(entity, options); 
    } 

    @Override 
    public void delete(Object... objects) { 
     mapper.delete(objects); 
    } 

    @Override 
    public ListenableFuture<Void> deleteAsync(Object... objects) { 
     return mapper.deleteAsync(objects); 
    } 

    @Override 
    public Result<T> map(ResultSet resultSet) { 
     return mapper.map(resultSet); 
    } 

    @Override 
    public ListenableFuture<Result<T>> mapAsync(ResultSetFuture resultSetFuture) { 
     return mapper.mapAsync(resultSetFuture); 
    } 

    @Override 
    public void setDefaultSaveOptions(Mapper.Option... options) { 
     mapper.setDefaultSaveOptions(options); 
    } 

    @Override 
    public void resetDefaultSaveOptions() { 
     mapper.resetDefaultSaveOptions(); 
    } 

    @Override 
    public void setDefaultGetOptions(Mapper.Option... options) { 
     mapper.setDefaultGetOptions(options); 
    } 

    @Override 
    public void resetDefaultGetOptions() { 
     mapper.resetDefaultGetOptions(); 
    } 

    @Override 
    public void setDefaultDeleteOptions(Mapper.Option... options) { 
     mapper.setDefaultDeleteOptions(options); 
    } 

    @Override 
    public void resetDefaultDeleteOptions() { 
     mapper.resetDefaultDeleteOptions(); 
    } 

    @Override 
    public T get(List objects) { 
     return mapper.get(objects.toArray()); 
    } 

    @Override 
    public ListenableFuture<T> getAsync(List objects) { 
     return mapper.getAsync(objects.toArray()); 
    } 
} 
ここで

はUserDAoImplです:

@Repository 
public class UsersDAOImpl extends BaseDAOImpl<Users> implements UsersDAO { 

    private UsersDAO accessor; 

    public UsersDAOImpl() { 
     super(Users.class); 
     accessor = manager.createAccessor(UsersDAO.class); 
    } 

    @Override 
    public Users findByUsername(String username) { 
     return accessor.findByUsername(username); 
    } 
} 

答えて

3

あなたの問題がある:

[ro.automotive.app.core.DAO.UsersDAOImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException

春がUsersDAOImplをインスタンス化した場合、あなたのBaseDAOImpl

@Autowired 
protected MappingManager manager; 

におけるマネージャーのautowireが実行されていませんその瞬間に。そういうわけで、あなたはNPEを手に入れています。

だから@PostConstruct方法であなたのmapperの初期化を行います。

@PostConstruct 
public void init(){ 
    [...] 
} 

@PostConstructのようなものですAfterBeanIsReadyAndAllFieldsAreAutowired

EDIT:

@Repository 
public abstract class BaseDAOImpl<T> implements BaseDAO<T> { 

    protected final Class<T> ENTITY_CLASS; 
    protected final Logger logger = LoggerFactory.getLogger(getClass()); 

    @Autowired 
    protected MappingManager manager; 

    protected Mapper<T> mapper; 

    public BaseDAOImpl() { 
     // get type of <T> hack... 
     this.ENTITY_CLASS = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; 
    } 

    @PostConstruct 
    public void init(){ 
     mapper = manager.mapper(ENTITY_CLASS); 
    } 

    [...] 
} 
: はこのような何かを行います