2017-07-15 2 views
0

Je travaille sur un projet et j'ai besoin d'être averti lorsqu'une entité est créée ou mise à jour. Mais malheureusement, @RepositoryEventHandler ne fonctionnait pas. Je prépare donc une version simple mais je n'ai pas eu de chance non plus. Ci-dessous, j'ai écrit tous les composants que j'ai pensé qu'il serait nécessaire pour régénérer mon problème.RepositoryEventHandler non invoqué

Ma configuration

@Configuration 
@ComponentScan(basePackages = "db") 
@EnableJpaRepositories(basePackages = {"db"}) 
//@EnableSpringDataWebSupport 
public class Config { 

@Bean 
@Autowired 
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) throws Exception { 
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); 
    vendorAdapter.setShowSql(Boolean.TRUE); 
    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); 
    factory.setJpaVendorAdapter(vendorAdapter); 

    factory.setPackagesToScan("db"); 
    factory.setDataSource(dataSource); 
    factory.setJpaProperties(getHibernateProperties()); 
    return factory; 
} 

@Bean 
public DataSource getDataSource() throws Exception { 
    Properties p = new Properties(); 
    Class.forName("org.h2.Driver"); 
    p.put("driverClassName", "org.h2.Driver"); 
    p.put("user", "admin"); 
    p.put("password", "123"); 
    DataSource ds = new DriverManagerDataSource("jdbc:h2:~/desktop/test;", p); 
    return ds; 
} 

@Bean(name = "transactionManager") 
public PlatformTransactionManager getTransactionManager(DataSource ds) { 
    DataSourceTransactionManager txManager = new DataSourceTransactionManager(); 
    try { 
     txManager.setDataSource(getDataSource()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return txManager; 
} 

private Properties getHibernateProperties() { 
    Properties properties = new Properties(); 
    properties.put("hibernate.show_sql", "true"); 
    properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); 
    properties.put("hibernate.hbm2ddl.auto", "create"); 
    properties.put("hibernate.connection.charSet", "UTF-8"); 
    properties.put("hibernate.id.new_generator_mappings", "false"); 
    properties.put("hibernate.max_fetch_depth", "3"); 

    properties.put("hibernate.connection.autocommit", "false"); 
    properties.put("hibernate.connection.release_mode", "on_close"); 

    return properties; 
} 

}

Ma définition POJO:

@javax.persistence.Entity 
public class Entity { 
private static final long serialVersionUID = 3501352854892096642L; 


@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
private long id ; 

private String name ; 

public long getId() { 
    return id; 
} 

public void setId(long id) { 
    this.id = id; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 
} 

Mon dépôt:

@RepositoryRestResource 
public interface EntityRep extends PagingAndSortingRepository<Entity,Long> { 
} 

et enfin, ceci est mon événement classe de définition de gestionnaire

@Component 
@RepositoryEventHandler(Entity.class) 
public class EventHandler { 

    @HandleBeforeSave 
    @HandleBeforeCreate 
    public void doSomething(Entity e){ 
     System.out.println("Hi..."); 
    } 
} 

je noter que j'utilise le repos de données Spring 2.6.4, Hibernate 5.2.10.Final et Tomcat 8.5.12 pour le conteneur. De plus, j'ai utilisé la configuration suivante pour mon Web.xml:

<listener> 
    <listener-class> 
     org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener> 

<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>db</param-value> 
</context-param> 

<servlet> 
    <servlet-name>rest</servlet-name> 
    <servlet-class>org.springframework.data.rest.webmvc.RepositoryRestDispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>rest</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

Merci de votre aide à l'avance.

+0

Que signifie "ne fonctionne pas"? Ça ne s'appelle pas? Avez-vous une exception? ... –

+0

On ne l'appelle pas. – Masoud

Répondre

0

J'ai le même problème. J'utilise spring-data-rest-core 2.6.4.RELEASE Hibernate - 5.1.8.Final