我实现了两个基于maven的独立Web项目,使用Spring MVC,hibernate和Jax-RS实现。 但我的要求发生了变化,现在我需要将项目作为子项目合并到另一个项目中,这是我们的父项目。所以我使用maven multimodule配置。
<packaging>pom</packaging>
<modules>
<module>../child1</module>
<module>../child2</module>
</modules>
<packaging>jar</packaging>
<parent>
<groupId>com.xyz.alpha</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent</relativePath>
</parent>
<packaging>jar</packaging>
<dependency>
<groupId>com.xyz.alpha</groupId>
<artifactId>child1</artifactId>
<version>2.0.2</version>
</dependency>
<parent>
<groupId>com.xyz.alpha</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent</relativePath>
</parent>
但我需要在java中配置项目,以便它将扫描父项的组件以及子项目和执行项目。 目前我对每个项目都有单独的配置:
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { AppConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.x.y")
public class AppConfig extends WebMvcConfigurerAdapter{
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.x.y.configuration" })
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfiguration {
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "com.x.y.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
return properties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
}
答案 0 :(得分:0)
第二个模块如果是弹簧Mvc或任何webapplication模块,那么它应该具有第一个模块的依赖性。在下面起草一个样本结构,希望这会有所帮助:
child1 pom:
<parent>
<groupId>com.xyz.alpha</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>child1-app</artifactId>
<packaging>jar</packaging>
<build>
</build>
<dependencies>
</dependencies>
child2 pom:
<parent>
<groupId>com.xyz.alpha</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>child2-Webapp</artifactId>
<packaging>war</packaging>
<build>
</build>
<dependencies>
<dependency>
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>child1-app</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
此外,您可以根据应用程序配置中的Hibernate配置进行连线,例如:
@Import({HibernateConfiguration.class})
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.x.y")
public class AppConfig extends WebMvcConfigurerAdapter{
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
答案 1 :(得分:0)
为此的最佳配置是使用称为Maven Reactor项目的东西。很好,似乎您正在使用它。
首先,您的父级不应具有任何spring组件,它应该仅由POM(BOM-材料明细表)组成,以仅定义依赖项版本,也许还可以在子模块之间共享某些插件。这些依赖关系应该在DependencyManagement标记中,而插件应该在PluginManagement中,但这实际上取决于您是否需要在子模块中强制执行某些行为。
如果您有父POM和两个子模块,并且想扫描两个子模块中的Spring组件,那么我将亲自创建具有相同父POM和两个子模块作为依赖项的第三个子模块。在您的主类中,我只需要使用自定义@SpringBootApplication
定义@ComponentScan
即可,该自定义<input type="number" [ngStyle]="resultStyling" [ngClass]="result>0 ? 'green' : 'red'" value="{{ result }}" name="resultInput">
<br>
的参数定义了要扫描的程序包。您可以在两个模块的程序包前缀中填充该参数,这很好。
另一种方法是创建依赖于那些子模块的完全独立的项目。但是,这要求您在本地maven存储库中安装了这些子模块。在第一个解决方案中,如果您总是一次构建整个项目,则一定不要在回购中安装这些软件。