DataSourceConfig:
Code:
@Configuration
@ComponentScan("net.findme.server")
@PropertySource("classpath:datasource.properties")
@ImportResource("classpath:spring/datasource-config.xml")
@EnableTransactionManagement
public class DataSourceConfig
{
@Inject
Environment env;
/**
* @return
*/
@Bean
public PlatformTransactionManager transactionManager() {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());
return transactionManager;
}
/**
* @return
*/
@Bean
public DriverManagerDataSource dataSource() {
final DriverManagerDataSource datasource = new DriverManagerDataSource();
datasource.setUsername(env.getProperty("jdbc.username"));
datasource.setPassword(env.getProperty("jdbc.password"));
datasource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
datasource.setUrl(env.getProperty("jdbc.url"));
return datasource;
}
/**
* @return
*/
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(dataSource());
emf.setPersistenceUnitName(env.getProperty("jdbc.persistenceUnit"));
emf.setJpaVendorAdapter(jpaVendorAdapter());
return emf;
}
/**
* @return
*/
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
final HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setShowSql(Boolean.parseBoolean(env.getProperty("jdbc.showSql")));
hibernateJpaVendorAdapter.setGenerateDdl(Boolean.parseBoolean(env.getProperty("jdbc.generateDdl")));
hibernateJpaVendorAdapter.setDatabasePlatform(env.getProperty("jdbc.databasePlattform"));
hibernateJpaVendorAdapter.setDatabase(Database.MYSQL);
return hibernateJpaVendorAdapter;
}
}
WebConfig:
Code:
@Configuration
@EnableWebMvc
@ComponentScan("net.findme.server.controller")
public class WebConfig extends WebMvcConfigurerAdapter
{
@Bean
public static InternalResourceViewResolver jspViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setPrefix("/WEB-INF/views/");
bean.setSuffix(".jsp");
return bean;
}
/* (non-Javadoc)
* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addResourceHandlers(org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry)
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
...
}
At last, my web.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
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_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<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>net.findme.server.config</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>net.findme.server.config</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Disables servlet container welcome file handling. Needed for compatibility
with Servlet 3.0 and Tomcat 7.0 -->
<welcome-file-list>
<welcome-file />
</welcome-file-list>
</web-app>
Anyone an idea? I cannot figure out the missing piece..
Thanks in advance!!
Markus