1

webapp を起動すると、次のエラーが表示されます。Spring Data ドキュメントを読み上げようとしましたが、不完全です。どんな助けでも大歓迎です。

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private dao.UserDao controller.LoginController.userDao; nested exception is     org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [dao.UserDao] 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:555)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 56 more

以下は私のプロジェクトの詳細です:

**構造**
ソース
 主要
   ジャワ
    豆
        Person.java
    構成
        アプリケーション構成
        Cassandra構成    
    コントローラ
        ログインコントローラ
    ダオ
        ユーザーダオ

contextClass org.springframework.web.context.support.AnnotationConfigWebApplicationContext

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>configuration.ApplicationConfiguration</param-value>
    </context-param>



    <servlet>
        <servlet-name>applicationContext</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>
                org.springframework.web.context.support.AnnotationConfigWebApplicationContext
            </param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>configuration</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>applicationContext</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>WEB-INF/log4j.xml</param-value>
    </context-param>

ApplicationConfiguration.java

@Configuration
@ComponentScan(basePackages = "controller, dao")
@EnableWebMvc

public class ApplicationConfiguration {

private static final Logger LOG = LoggerFactory.getLogger(ApplicationConfiguration.class);

    @Bean
    public User user() {
        return new User();
    }
    @Autowired
    private CassandraOperations cassandraOps;

    @Bean
    TilesViewResolver viewResolver(){
        TilesViewResolver viewResolver = new TilesViewResolver();
        return viewResolver;
    }

    @Bean
    TilesConfigurer tilesConfigurer(){
        TilesConfigurer tilesConfigurer = new TilesConfigurer();
        tilesConfigurer.setDefinitions("WEB-INF/tiles-definitions.xml");
        tilesConfigurer.setPreparerFactoryClass(org.springframework.web.servlet.view.tiles3.SpringBeanPreparerFactory
            .class);
        return tilesConfigurer;
    }
}

CassandraConfiguration.java

@Configuration
@PropertySource(value = { "classpath:cassandra.properties" })
@EnableCassandraRepositories(basePackages = { "dao" })
public class CassandraConfiguration {

    private static final Logger LOG = LoggerFactory.getLogger(CassandraConfiguration.class);
    @Autowired
    private Environment env;

    @Bean
    public CassandraClusterFactoryBean cluster() {

        CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
        cluster.setContactPoints(env.getProperty("cassandra.contactpoints"));
        cluster.setPort(Integer.parseInt(env.getProperty("cassandra.port")));

        return cluster;
    }

    @Bean
    public CassandraMappingContext mappingContext() {
        return new BasicCassandraMappingContext();
    }

    @Bean
    public CassandraConverter converter() {
        return new MappingCassandraConverter(mappingContext());
    }

    @Bean
    public CassandraSessionFactoryBean session() throws Exception {

        CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
        session.setCluster(cluster().getObject());
        session.setKeyspaceName(env.getProperty("cassandra.keyspace"));
        session.setConverter(converter());
        session.setSchemaAction(SchemaAction.NONE);

        return session;
    }

    public CassandraOperations cassandraTemplate() throws Exception {
        return new CassandraTemplate(session().getObject());
    }

ログインコントローラ

@Controller
public class LoginController {
    private final static Logger logger = Logger.getLogger(LoginController.class);

    @Autowired
    private UserDao userDao;

    @RequestMapping(value = Constants.WEBAPP_CONTEXT + "/login.do", method = RequestMethod.GET)
    public ModelAndView login() {
        userDao.addUser();
        return new ModelAndView("login-tile");
    }
   }

UserDao @Repository("userDao") public class UserDao {

    public static final String TIME_BUCKET = "2014-01-01";

    @Autowired
    private CassandraOperations cassandraOps;

    public void addUser() {
        cassandraOps.insert(new Person("1234567890", "David", 40));
    }
}
4

1 に答える 1

0

クラスの定義に@Bean注釈を付ける必要があります。そうしないと、Bean としてインスタンス化されず、Spring はそれを自動配線できません。CassandraOperationsCassandraConfiguration

このサンプルのように:

@Bean <---
public CassandraOperations cassandraTemplate() throws Exception {
    return new CassandraTemplate(session().getObject());
}
于 2015-10-19T15:08:37.147 に答える