I'm trying to set up a Spring configuration based on Java @Configuration class and web.xml file. I have defined two different configurations: KatherineConfig.java that holds the configuration of JPA layer; KatherineWebConfig.java that imports the first configuration file and adds servlet mapping to Spring configuration.
Now I would like to add a ContextLoaderListener to my configuration. I have no idea on how to do it.
This is my web.xml configuration:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_3.xsd">
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>it.manuelgozzi.katherine.web.configuration.KatherineWebConfig
</param-value>
</context-param>
<display-name>Katherine</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
This is my KatherineWebConfig.java file:
package it.manuelgozzi.katherine.web.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import it.manuelgozzi.katherine.finance.configuration.KatherineConfig;
@Configuration
@Import(KatherineConfig.class)
@ComponentScan(basePackages = { "it.manuelgozzi.katherine.web" })
@EnableWebMvc
public class KatherineWebConfig {
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
}
And finally this is KatherineConfig.java file:
package it.manuelgozzi.katherine.finance.configuration;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
/**
* Configurazione di Katherine. I parametri sono gestiti esternamente mediante
* il file katherine.configuration.properties.
*
* @author Manuel Gozzi
* @date Jan 6, 2019
* @time 6:12:48 PM
*/
@Configuration
@ComponentScan(basePackages = {"it.manuelgozzi.katherine.finance", "it.manuelgozzi.katherine.web"})
@EnableJpaRepositories(basePackages = "it.manuelgozzi.katherine.finance.model.entity.repository")
@PropertySource("classpath:katherine.configuration.properties")
public class KatherineConfig {
@Value("${datasource.username}")
private String dataSourceUsername;
@Value("${datasource.password}")
private String dataSourcePassword;
@Value("${datasource.url}")
private String dataSourceUrl;
@Value("${datasource.driverclassname}")
private String dataSourceDriverClassName;
@Value("${entitymanager.packagestoscan}")
private String entityManagerPackagesToScan;
/**
* Configurazione del bean relativo al datasource.
*
* @author Manuel Gozzi
* @date Jan 6, 2019
* @time 6:13:01 PM
* @return
*/
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUsername(dataSourceUsername);
dataSource.setPassword(dataSourcePassword);
dataSource.setUrl(dataSourceUrl);
dataSource.setDriverClassName(dataSourceDriverClassName);
return dataSource;
}
/**
* Configurazione del TransactionManager.
*
* @author Manuel Gozzi
* @date Jan 6, 2019
* @time 6:13:12 PM
* @return
*/
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
/**
* Configurazione dell'EntityManagerFactory.
*
* @author Manuel Gozzi
* @date Jan 6, 2019
* @time 6:13:24 PM
* @return
*/
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { entityManagerPackagesToScan });
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
return em;
}
}
Could you kindly explain me how to implement this solution? My goal is to stand up with Java configuration and web.xml together.
I am trying to run my application as it is but I'm getting the following Exception:
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
Thanks in advice!
Aucun commentaire:
Enregistrer un commentaire