How to autowire @service from external Jar in Spring

自古美人都是妖i 提交于 2019-12-12 01:55:53

问题


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;
    }

回答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



来源:https://stackoverflow.com/questions/26451192/how-to-autowire-service-from-external-jar-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!