Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将XMLbean自动连接到@Configuration类_Java_Xml_Spring_Annotations_Autowired - Fatal编程技术网

Java 将XMLbean自动连接到@Configuration类

Java 将XMLbean自动连接到@Configuration类,java,xml,spring,annotations,autowired,Java,Xml,Spring,Annotations,Autowired,我在Spring项目中通常使用以下三个文件中的xml配置: applicationContext.xml: 此文件包含主xml配置:组件扫描、注释配置,以及其他两个xml配置文件的包含: applicationContext db.xml 此文件包含所有数据库bean:dataSource、SessionFactory、 applicationContext security.xml 此文件包含所有spring安全配置 我还需要使用Spring安全ACL,为此我创建了一个配置类: AclMeth

我在Spring项目中通常使用以下三个文件中的xml配置:

applicationContext.xml: 此文件包含主xml配置:组件扫描、注释配置,以及其他两个xml配置文件的包含:

applicationContext db.xml 此文件包含所有数据库bean:dataSource、SessionFactory、

applicationContext security.xml 此文件包含所有spring安全配置

我还需要使用Spring安全ACL,为此我创建了一个配置类:

AclMethodSecurityConfiguration.java

package com.medkhelifi.tutorials.todolist.conf;

/**
/* all imports goes here.
**/

@Configuration
@ImportResource({"classpath*:conf/applicationContext-db.xml"})
@EnableGlobalMethodSecurity (prePostEnabled = true, securedEnabled = true)
public class AclMethodSecurityConfiguration extends GlobalMethodSecurityConfiguration {

    @Autowired
    DataSource dataSource;

    @Bean
    public MethodSecurityExpressionHandler defaultMethodSecurityExpressionHandler() {
        DefaultMethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler();
        AclPermissionEvaluator permissionEvaluator = new AclPermissionEvaluator(aclService());
        expressionHandler.setPermissionEvaluator(permissionEvaluator);
        return expressionHandler;
    }


    @Bean
    public JdbcMutableAclService aclService() {
        return new JdbcMutableAclService(dataSource, lookupStrategy(), aclCache());
    }

    @Bean
    public AclAuthorizationStrategy aclAuthorizationStrategy() {
        return new AclAuthorizationStrategyImpl(new SimpleGrantedAuthority("ROLE_ADMIN"));
    }


    @Bean
    public PermissionGrantingStrategy permissionGrantingStrategy() {
        return new DefaultPermissionGrantingStrategy(new ConsoleAuditLogger());
    }


    @Bean
    public EhCacheBasedAclCache aclCache() {
        return new EhCacheBasedAclCache(
                    aclEhCacheFactoryBean().getObject(),
                    permissionGrantingStrategy(),
                    aclAuthorizationStrategy()
                    );
    }

    @Bean
    public EhCacheFactoryBean aclEhCacheFactoryBean() {
        EhCacheFactoryBean ehCacheFactoryBean = new EhCacheFactoryBean();
        ehCacheFactoryBean.setCacheManager(aclCacheManager().getObject());
        ehCacheFactoryBean.setCacheName("aclCache");
        return ehCacheFactoryBean;
    }

    @Bean
    public EhCacheManagerFactoryBean aclCacheManager() {
        return new EhCacheManagerFactoryBean();
    }

    @Bean
    public LookupStrategy lookupStrategy() {
        return new BasicLookupStrategy(
                    dataSource,
                    aclCache(),
                    aclAuthorizationStrategy(),
                    new ConsoleAuditLogger());
    }
}
我的问题是自动连接到配置文件中的数据源为空,如果我遗漏了什么,我不知道该怎么办

我的XMLs文件都在:src/main/resources/conf下/

applicationContext-db.xml中有我的数据源bean定义

<!--        DATASOURCE                      -->
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driver}"/>
    <property name="url" value="${database.url}"/>
    <property name="username" value="${database.username}"/>
    <property name="password" value="${database.password}"/>
</bean>

我已经在同一applicationContext-db.xml文件中定义的sessionfactorybean中使用了这个bean


PS:当我删除扩展类
GlobalMethodSecurityConfiguration
时,定义了我的数据源,但我需要这个
org.springframework.security.config.annotation.method.configuration
类来设置我的Spring安全ACL配置。

请将您的bean重命名为
name=“datasource”

我找到了一种使用
BeanFactoryAware
接口定义数据源bean的方法。 BeanFactoryAware用于注入BeanFactory对象。这样我们就可以访问创建该对象的BeanFactory

@EnableGlobalMethodSecurity (prePostEnabled = true, securedEnabled = true)
@Configuration
@ImportResource({"classpath:/conf/applicationContext-db.xml"})
public class AclMethodSecurityConfiguration extends GlobalMethodSecurityConfiguration implements BeanFactoryAware {

    DataSource dataSource;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.dataSource = beanFactory.getBean("dataSource", DataSource.class);
    }
    // rest of code goes here 
}

我了解到,如果我们使用这项技术意味着我们做错了什么,我将继续寻找合适的解决方案。

您的
com.medkhelifi.tutorials.todolist.conf
是否可用于组件扫描?我的意思是spring能够找到您的配置类来注入数据源吗?是的,我在applicationContext.xml中使用:
,您能在没有类路径的情况下尝试吗?就像
{“conf/applicationContext db.xml”}
No一样,通过使用您的命题,我得到:
IOException解析来自ServletContext资源的xml文档[/conf/applicationContext db.xml]是否可以放入完整的日志文件?它无法找到给定的文件或无法解析它?我重命名了它,但它无法解决我的问题,我的数据源始终为空。我们是否可以在不扩展
全局方法安全配置的情况下尝试?只要保持Java配置简单,就可以一步一步地检查问题。我看不到任何其他问题,只是从其他配置文件扩展时可能存在依赖顺序问题。是的,通过删除扩展类定义了我的数据源(我遇到了其他问题),但定义了我的数据源。还有,要点。您是否也添加了注释配置<代码>
?那么,在删除extend之后,您还需要帮助来解决它吗?也许您可以将这个@autowire数据源添加到父类中?