我正在开发一个项目,我需要在创建或更新实体时收到通知。但不幸的是,@RepositoryEventHandler
没有用。所以我准备了一个简单的版本,但我也没有运气。下面,我已经编写了我认为重新生成问题所需的所有组件。
我的配置
@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;
}
}
我的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;
}
}
我的存储库:
@RepositoryRestResource
public interface EntityRep extends PagingAndSortingRepository<Entity,Long> {
}
最后,这是我的事件处理程序定义类:
@Component
@RepositoryEventHandler(Entity.class)
public class EventHandler {
@HandleBeforeSave
@HandleBeforeCreate
public void doSomething(Entity e){
System.out.println("Hi...");
}
}
我应该注意到我正在使用Spring Data Rest 2.6.4,Hibernate 5.2.10.Final和Tomcat 8.5.12作为容器。此外,我在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>
提前感谢您的帮助。