Java – multi module project configuration based on spring MVC annotations
•
Java
I implemented two Maven based independent web projects using spring MVC, hibernate and Jax rs
Item 1: parent program
<packaging>pom</packaging>
<modules>
<module>../child1</module>
<module>../child2</module>
</modules>
Child 1:
<packaging>jar</packaging>
<parent>
<groupId>com.xyz.alpha</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent</relativePath>
</parent>
Child 2:
<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>
But I need to configure the project in Java so that it will scan the parent's components as well as child and execution projects At present, I configure it separately for each project:
AppIntializer. 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[] { "/" };
}
}
AppConfig. java
@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/");
}
}
HibernateConfiguration. java
@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;
}
}
Solution
If I were you, I would create a third module among parents and use two new children as dependencies Then, you only need to configure the spring for the new module as required
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
