user5707585
user5707585

Reputation:

How to avoid creating bean for ServiceImpl classes in Spring MVC application?

I am developing the Spring MVC + Spring Security + Spring Rest example using mongoDB. In this example to, I see that I've more than 200 of classes (Service and ServiceImpl) and to create an instance of these classes in Controller classes, so for that I've to configure the bean in the applicationContext.xml. Is there any way to have all those beans created automatically?

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>

Please let me know if you need any other details. I'm using XML based configurations.

Upvotes: 0

Views: 276

Answers (1)

kuhajeyan
kuhajeyan

Reputation: 11027

Since you have enabled component-scan, you should be able mark the classes with stereo type markers so that there automatically picked up. So no need to define them in your xml

ex.

  • Services -> @Service

  • Controllers -> @Controller

  • Components or other beans -> @Component

Upvotes: 1

Related Questions