2016-09-24 12 views
1

spring mvc 4設定でresourceHandlerを追加していますが、動作しません。私は追加していたときに、この日食のIDEがエラーを与え、ショーがここに@Override を削除するためにヒント私のコードはspring mvc 4でresourcehandlerを追加する方法java configuration

ある
package net.codejava.spring.config; 

import javax.servlet.ServletContext; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRegistration; 

import org.springframework.web.WebApplicationInitializer; 
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; 
import org.springframework.web.servlet.DispatcherServlet; 

public class SpringWebAppInitializer implements WebApplicationInitializer { 

@Override 
public void onStartup(ServletContext servletContext) throws ServletException { 
    AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext(); 
    appContext.register(ApplicationContextConfig.class); 

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
      "SpringDispatcher", new DispatcherServlet(appContext)); 
    dispatcher.setLoadOnStartup(1); 
    dispatcher.addMapping("/"); 

} 

} 

ApplicationContextConfig:

package net.codejava.spring.config; 

import java.util.Properties; 

import javax.sql.DataSource; 

import net.codejava.spring.dao.UserDAO; 
import net.codejava.spring.dao.UserDAOImpl; 
import net.codejava.spring.model.User; 

import org.apache.commons.dbcp2.BasicDataSource; 
import org.hibernate.SessionFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.orm.hibernate4.HibernateTransactionManager; 
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 
import org.springframework.web.servlet.view.InternalResourceViewResolver; 

@Configuration 
@ComponentScan("net.codejava.spring") 
@EnableTransactionManagement 
public class ApplicationContextConfig { 



public void addResourceHandlers(ResourceHandlerRegistry registry) { 
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); 
} 

    @Bean(name = "viewResolver") 
public InternalResourceViewResolver getViewResolver() { 
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
    viewResolver.setPrefix("/WEB-INF/views/"); 
    viewResolver.setSuffix(".jsp"); 
    return viewResolver; 
} 


@Bean(name = "dataSource") 
public DataSource getDataSource() { 
    BasicDataSource dataSource = new BasicDataSource(); 
    dataSource.setDriverClassName("com.mysql.jdbc.Driver"); 
    dataSource.setUrl("jdbc:mysql://localhost:3306/usersdb"); 
    dataSource.setUsername("root"); 
    dataSource.setPassword(""); 

    return dataSource; 
} 


private Properties getHibernateProperties() { 
    Properties properties = new Properties(); 
    properties.put("hibernate.show_sql", "true"); 
    properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); 
    return properties; 
} 

@Autowired 
@Bean(name = "sessionFactory") 
public SessionFactory getSessionFactory(DataSource dataSource) { 
    LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource); 
    sessionBuilder.addProperties(getHibernateProperties()); 
    sessionBuilder.addAnnotatedClasses(User.class); 
    return sessionBuilder.buildSessionFactory(); 
} 

@Autowired 
@Bean(name = "transactionManager") 
public HibernateTransactionManager getTransactionManager(
     SessionFactory sessionFactory) { 
    HibernateTransactionManager transactionManager = new HibernateTransactionManager(
      sessionFactory); 

    return transactionManager; 
} 

@Autowired 
@Bean(name = "userDao") 
public UserDAO getUserDao(SessionFactory sessionFactory) { 
    return new UserDAOImpl(sessionFactory); 
    } 
} 

とクラスの上WebMvcConfigurerAdapterクラスをextedingされていないなぜあなたは私を説明することができますありがとう

答えて

2

ApplicationContextConfigクラス定義にextends WebMvcConfigurerAdapterを追加してから、リソースハンドラをオーバーライド/追加することができます

@Configuration 
@ComponentScan("net.codejava.spring") 
@EnableTransactionManagement 
public class ApplicationContextConfig extends WebMvcConfigurerAdapter { 


@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
    registry.addResourceHandler("/images/**") 
      .addResourceLocations("classpath:/META-INF/resources/images/"); 
} 
関連する問題