1

タイプ AuthenticationController の Bean を生成しようとしています。認証コントローラーのコンストラクターには、securityDataSource と呼ばれる DataSource 型のパラメーターが 1 つあります。セキュリティ コンテキスト xml で、次のコードを使用して Bean を宣言します。

webSecurityConfig.xml からの抜粋:

<bean id="authoritiesConroller" 
       class="honors.uh.edu.security.AuthoritiesController">
        <constructor-arg ref="securityDataSource"/>
</bean>

Bean securityDataSource の定義は次のとおりです。

<bean name="securityDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/test" />
        <property name="username" value="root" />
        <property name="password" value="" />
</bean>

AuthoritiesController.java

package honors.uh.edu.security;

import honors.uh.edu.pojo.User;

import java.sql.Types;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.SqlUpdate;
public class AuthoritiesController  extends JdbcDaoSupport {


    private InsertAuthority insertAuthority;

    public AuthoritiesController(DataSource securityDataSource) {
        super();
        insertAuthority = new InsertAuthority(securityDataSource);


    }

    public void create(User user, String authority) {
        insertAuthority.insert(user, authority);
    }

    /********* Inner Classes  ************/
    protected class InsertAuthority extends SqlUpdate {
        protected InsertAuthority(DataSource ds) {
            super(ds, "INSERT INTO authorities VALUES (?, ?)");
            declareParameter(new SqlParameter(Types.VARCHAR));
            declareParameter(new SqlParameter(Types.VARCHAR));
            compile();
        }

        protected void insert(User user, String authority) {
            Object[] objs = new Object[] { user.getUsername(), authority };
            super.update(objs);
        }

    }
}

返されるエラーはCaused by: java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required. なぜ今これを許可するのでしょうか?DriverManagerDataSource は DataSource の実装なので、パラメーターとして渡すことはできませんか?

編集:ここに完全なエラーがあります-

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authoritiesConroller' defined in class path resource [spring/webSecurityConfig.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) ~[spring-context-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) ~[spring-context-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) ~[spring-web-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) ~[spring-web-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) [spring-web-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939) [catalina.jar:7.0.42]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434) [catalina.jar:7.0.42]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:7.0.42]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) [catalina.jar:7.0.42]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) [catalina.jar:7.0.42]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_05]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_05]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_05]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_05]
Caused by: java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required
    at org.springframework.jdbc.core.support.JdbcDaoSupport.checkDaoConfig(JdbcDaoSupport.java:112) ~[spring-jdbc-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44) ~[spring-tx-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    ... 21 common frames omitted
4

1 に答える 1

0

問題が解決しました!<constructor-arg...../>ビットは決して問題ではありませんでした。問題は、クラス AuthenticationController がクラス JdbcDaoSupport を拡張していたため、「dataSource」というプロパティを宣言する必要があることでした。型の競合の問題ではありませんでした。

現在機能する Bean 宣言は次のようになります。

<bean id="authoritiesConroller" class="honors.uh.edu.security.AuthoritiesController">
        <property name="dataSource" ref="securityDataSource"/>
        <constructor-arg ref="securityDataSource"/>
</bean>

dataSource を 2 回渡さないようにするためのより良い方法があると確信していますが、これで少なくとも問題は解決しました。ご協力いただきありがとうございます!

于 2014-07-14T19:44:46.427 に答える