1

我正在尝试生成一个 AuthenticationController 类型的 bean。Authentication 控制器的构造函数有一个 DataSource 类型的参数,称为 securityDataSource。在我的安全上下文 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 ,但这至少解决了问题。谢谢你的帮助!

于 2014-07-14T19:44:46.427 回答