2016-12-09 1 views
0

私はmongoDBを使って、Spring MVC + Spring Security + Spring Restサンプルを開発しています。この例では、200以上のクラス(Service and ServiceImpl)があり、にBeanを設定するために、これらのクラスのインスタンスをControllerクラスに作成することがわかりました。これらのすべてのBeanを自動的に作成する方法はありますか?Spring MVCアプリケーションでServiceImplクラスのBeanを作成しないようにする方法は?

のapplication-config.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:context="http://www.springframework.org/schema/context" xmlns:mongo="http://www.springframework.org/schema/data/mongo" 
      xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd"> 

    <context:annotation-config/> 
    <mvc:annotation-driven/> 

    <beans:bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <beans:property name="locations"> 
      <beans:list> 
       <beans:value>database.properties</beans:value> 
      </beans:list> 
     </beans:property> 
    </beans:bean> 

    <context:component-scan base-package="com.XXXX.XXX.XXX.model"/> 

    <mongo:mongo id="mongo" host="${mongo.db.host}" port="${mongo.db.port}"/> 

    <mongo:repositories base-package="com.XXXX.XXXX.XXX.repositories"/> 

    <beans:bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 
     <beans:constructor-arg ref="mongo"/> 
     <beans:constructor-arg name="databaseName" value="${mongo.db.name}"/> 
    </beans:bean> 

    <beans:bean id="employeeService" class="com.XXX.XXX.services.EmployeeServiceImpl"/> 
    <beans:bean id="departmentService" class="com.XXX.XXX.services.DepartmentServiceImpl"/> 
    <beans:bean id="financeService" class="com.XXX.XXX.services.FinanceServiceImpl"/> 
    <beans:bean id="siteService" class="com.XXX.XXX.services.SiteServiceImpl"/> 
    <beans:bean id="helpService" class="com.XXX.XXX.services.HelpServiceImpl"/> 
    <beans:bean id="facilityService" class="com.XXX.XXX.services.FacilityServiceImpl"/> 
    <beans:bean id="conditionService" class="com.XXX.XXX.services.ConditionServiceImpl"/> 
    <beans:bean id="gameService" class="com.XXX.XXX.services.GameServiceImpl"/> 
    <beans:bean id="reportService" class="com.XXX.XXX.services.ReportServiceImpl"/> 
    <beans:bean id="roleService" class="com.XXX.XXX.services.RoleServiceImpl"/> 
    ........... 
    ........... 
    ........... 
    ........... 
    <!-- So many service classes --> 

</beans:beans> 

あなたが他の詳細が必要な場合は私に知らせてください。 XMLベースの設定を使用しています。

答えて

1

コンポーネントスキャンを有効にしているので、自動的に選択されるようにステレオタイプのマーカーでクラスをマークすることができます。したがって、あなたのxmlでそれらを定義する必要はありません

ex。

  • サービス - > @Service

  • コントローラ - > @Controller

  • コンポーネントまたは他の豆 - > @Component

+0

私はその後、これらのBeanを定義していけない場合は@ ServiceMap Beanのコントローラクラスで –

+0

のAutowirningはまったく機能しません。サービスや他のクラスをインクルードするためにコンポーネントスキャンを組み込んだ場合、@autowireはちゃんと働く – kuhajeyan

関連する問題