0

如果配置的数据库不存在,我在哪里可以查看数据源连接错误。我正在设置 spring security 3.1 以验证使用具有自定义模式的数据库。我花费的大部分时间是由于我的自定义 jdbc.properties 文件中的数据库名称中的拼写错误。

如果我注意到错误是由于这个错字造成的,那会容易得多。有没有办法配置 spring 数据源以在无法获得有效数据源连接的情况下显示错误。一种方法是检查返回的数据源引用对象并确定它是否返回 null。但基本上,我正在调查 Spring 报告的错误。

  //Spring datasource

     <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 

        <property name="driverClassName" value="${jdbc.driverClassName}" />  
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="100" />
        <property name="maxIdle" value="30" /> 

 </bean>


    <sec:authentication-manager alias="authenticationManager">
   <sec:authentication-provider user-service-ref="jdbcUserService"/>
</sec:authentication-manager>


    <beans:bean id = "jdbcUserService" 
    class="com.company.portal.helper.CustomJDBCDaoImpl">
    <beans:property name="dataSource" ref="dataSource" />
     <beans:property name="enableAuthorities" value="true"/>
    <beans:property name="usersByUsernameQuery">
        <beans:value>
            select username,password,enabled from users where username = ?
        </beans:value>
    </beans:property>
    <beans:property name="authoritiesByUsernameQuery">
        <beans:value>
            select username,authority from user_roles where username = ?
        </beans:value>
    </beans:property>   
</beans:bean>

如果这可以使用 log4j 实现,请解释一下。我能够为我的项目模块设置 log4j,但不能为 spring 模块设置。如果使用新的 spring 模块(例如:spring social),是否需要更新日志记录?

谢谢你的时间。

4

1 回答 1

0

我不t know any build in way, and it is but it should be easy to implement your own. But instead of invoking the statements, it is much more easieer to invoke the complete使用 loadUserByUsername` 方法。

@Service
public class StatementChecker implements ApplicationListener<ContextStartedEvent> {

    @Autowired
    private CustomJDBCDaoImpl jdbcDaoImpl;

    public void onApplicationEvent(E event) {
      String username = "iKnowYouExist";
      try {
         UserDetails userDetails = jdbcDaoImpl.loadUserByUsername(username);
      } catch (UsernameNotFoundException e) {
         LOGGER.ERROR ("the user does not exist", e);
      } catch (DataAccessException) {
         LOGGER.ERROR ("some thing is wrong with the query, database or database connetion");
      }
    }
}

但老实说,我坚信写一个测试用例会更好,也许你可以重用这段代码进行测试。

于 2013-10-08T19:44:18.670 回答