0

I am developing two spring based application(Ex app1 and app2) fully on Java configuration with Maven and no XML config. Through Maven -WAR plugin , I have created Jar reference of app2 and using mvn install:install file I have binded the app2 jar with app1. app2 - Its used to fetch inforamtion from data source.

I have autowired the app2 serive in app1 implementation class to fetch the details which was annotated with @Service in app2 application.

My first doubt is:

Both app1 and app2 have separate AppConfig.java file.Is it possible to simply autowiring one of the @Service which is availble in Jar format or else I need to define or import App2's AppConfig java file into App1's AppConfig.jave file.

I have tried with the simple autowired the external JAR @Service class and ended with error.

Kindly help me on what needs to be done to autowire external Jar's @Service to the implementation class.

Below is my App1 repository class

@Repository
public class VehicleRepository {   

    @Autowired  
    VehicleService vehicleservice;   

    public Map<String, item> getAllTypes(String type) {     

            Vehicke vehicle = vehicleservice.getAllVehicle(type);

            // handle response here...
        } catch (Exception ex) {

            // handle exception here
        } finally {

        }
        return vehicleDetails;
    }

}

VehicleService is available in external Jar.

VehicleService Class:

@Service
public class VehicleService {

    public VehicleService() {
    }

    @Autowired
    PortRepository portRepository;

    @Autowired
    MessageSource messageSource;


    public Vehicle getAllVehicles(String type) {
        List<Vehicle> cehicles = portRepository.getPorts();
        return vehicle;
    }
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Rithik_Star
  • 651
  • 5
  • 14
  • 39
  • Beans are defined by a configuration class. If Spring is not initialized with one of the config classes, then obviously the beans defined by this config class are not known to Spring. – JB Nizet Oct 19 '14 at 14:01
  • @JBNizet Is there any way to call App2's config before autowire service in the repository class or How to call/Include App2's AppConfig file to App1's Appcofig – Rithik_Star Oct 19 '14 at 14:04
  • By importing App2's config class from App1's config class: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Import.html. But if those are really applications, designed to be run independantly, you'll certainly have conflicting beans. You'll probably have to write a seperate config class to load only the beans from App2 that make sense when used as part of App1. – JB Nizet Oct 19 '14 at 14:13

1 Answers1

1

Let's make it simple.
Your App1 depends on App2.
So you use @Import(App2Config.class) class App1Config {}, that's it.

And by the way, instead of tricks with 'mvn install:install file' you can just use parent pom.xml with modules app1 and app2, and declare dependency of module app1 on module app2 in pom.xml <dependencies> section. You then run 'mvn install' to build your project.
See an example here: http://books.sonatype.com/mvnex-book/reference/multimodule.html

Alexander
  • 2,761
  • 1
  • 28
  • 33
  • When I use Import of another App config and created WAR deployed in Tomcat but I got below errror "Caused by: java.lang.IllegalArgumentException: Failed to register servlet with name 'dispatcher'.Check if there is another servlet registered under th e same name." – Rithik_Star Oct 20 '14 at 07:01
  • One more doubt .Is it simple Import is enough or I need to autowire and define what are the definition available in App2 config into App1 config – Rithik_Star Oct 20 '14 at 07:08
  • Regarding the first comment: you should have only one web.xml file in which you define only a single servlet with the given name. Regarding the second comment: @Import causes your App1Config to include all beans defined in App2Config, as if they were defined there. – Alexander Oct 21 '14 at 13:01