Spring:限制在Controller中注入Repository模块

时间:2016-12-21 11:27:07

标签: java spring maven spring-boot

我正在使用Spring Boot(v 1.4.2.RELEASE)构建REST Web服务。我正在尝试关注分离关注模式,所以我创建了3个maven项目, myapp-repository,myapp-service和myapp-rest。 myapp-service依赖于myapp-repository,myapp-rest依赖于myapp-service。以下是我做的初始配置。

MyApp的存储库

的pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
</dependencies>

存储库配置

@Configuration
@EnableJpaRepositories
@ComponentScan("com.mycompany.myapp.repository")
@EntityScan("com.mycompany.myapp.entity")
@PropertySource("classpath:repository.properties")
public class RepositoryConfig {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "myapp.primary.datasource")
    public DataSource getDataSource() {
        return DataSourceBuilder.create().build();
    }

}

MyApp的服务

的pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.mycompany.myapp</groupId>
        <artifactId>myapp-repository</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

服务配置

@Configuration
@ComponentScan("com.mycompany.myapp.service")
@Import(RepositoryConfig.class)
@PropertySource("classpath:service.properties")
public class ServiceConfig {

}

MyApp的静止

的pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.mycompany.myapp</groupId>
        <artifactId>myapp-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

休息配置

@SpringBootApplication
@ComponentScan("com.mycompany.myapp.rest")
@Import(ServiceConfig.class)
public class RestConfig extends SpringBootServletInitializer {

    /**
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(RestConfig.class, args);
    }
}

现在,我想限制我的存储库服务在myapp-rest中自动装配,但是使用这个当前的实现,它是可能的。只允许myapp-service模块内的服务在myapp-rest中自动装配。有可能吗?

0 个答案:

没有答案