Tag: spring

使用Java Generics为实体实现转换器

我正在使用Spring和Hibernate开发JSF项目,其中包括许多遵循相同模式的Converter : getAsObject接收对象id的字符串表示forms,将其转换为数字,并获取给定种类的实体和给定的id getAsString接收和实体并返回转换为String的对象的id 代码基本上如下(省略检查): @ManagedBean(name=”myConverter”) @SessionScoped public class MyConverter implements Converter { private MyService myService; /* … */ @Override public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) { int id = Integer.parseInt(value); return myService.getById(id); } @Override public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) { return ((MyEntity)value).getId().toString(); } } 鉴于大量的Converter与此完全相同(当然除了MyService和MyEntity的类型),我想知道是否值得使用单个通用转换器。 通用本身的实现并不困难,但我不确定声明Beans的正确方法。 可能的解决方案如下: 1 – […]

如何在异步任务执行程序中启用请求范围

在我的应用程序中,我有一些异步Web服务。 服务器接受请求,返回OK响应并使用AsyncTaskExecutor启动处理请求。 我的问题是如何在此处启用请求范围,因为在此处理中我需要获取由以下内容注释的类: @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS) 现在我得到例外: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘scopedTarget.requestContextImpl’: Scope ‘request’ is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring […]

Spring Boot,Spring Data JPA,带有多个DataSources

我正在尝试使用Spring Boot和Spring Data JPA将每个@Repositories连接到不同的DataSource。 我使用以下内容http://xantorohara.blogspot.com/2013/11/spring-boot-jdbc-with-multiple.html作为参考。 以下是我试图使用Spring Data JPA实现类似解决方案时使用的代码。 CustomerDbConfig.java (第一个数据源连接) @Configuration @EnableJpaRepositories( entityManagerFactoryRef = “orderEntityManager”, transactionManagerRef = “orderTransactionManager”, basePackages = {“com.mm.repository.customer”}) public class CustomerDbConfig { @Bean(name = “customerEntityManager”) public LocalContainerEntityManagerFactoryBean entityManagerFactory(){ LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource()); em.setPackagesToScan(new String[] {“com.mm.domain.customer”}); JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); em.setJpaVendorAdapter(vendorAdapter); em.setJpaProperties(additionalJpaProperties()); em.setPersistenceUnitName(“customerPersistence”); em.setPackagesToScan(“com.mm.domain.customer”); return em; } Properties additionalJpaProperties(){ Properties […]

注册为Spring bean时,过滤调用两次

我想将@Autowire与Filter一起使用。 所以我在SecurityConfig定义了我的filter,如下所示: @Override protected void configure(HttpSecurity http) throws Exception { http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); http.addFilterBefore(getA(), BasicAuthenticationFilter.class); http.csrf().disable(); } @Bean public A getA(){ return new A(); } 这个filterA扩展了Spring的GenericFilterBean 。 当我调用控制器时,我得到低于输出,这显示filter命中两次。 filter A before filter A before mycontroller invoke filter A after filter A after 我的观察是,这个额外的调用使用Spring容器调用,因为如果filter没有注册为bean,它只会被命中一次。 是什么原因,我该如何解决?

如何为Spring Boot应用程序配置端口

如何配置Spring Boot应用程序侦听的TCP / IP端口,因此它不使用默认端口8080。

是什么导致“java.lang.IllegalStateException:BindingResult和bean name’命令的普通目标对象’都不可用作请求属性”?

对于这些类型的问题,这是一个广泛的规范问答post。 我正在尝试编写一个Spring MVC Web应用程序,用户可以在其中添加电影名称到内存中的集合。 它的配置是这样的 public class Application extends AbstractAnnotationConfigDispatcherServletInitializer { protected Class[] getRootConfigClasses() { return new Class[] {}; } protected Class[] getServletConfigClasses() { return new Class[] { SpringServletConfig.class }; } protected String[] getServletMappings() { return new String[] { “/” }; } } 和 @Configuration @ComponentScan(“com.example”) public class SpringServletConfig extends WebMvcConfigurationSupport { @Bean public InternalResourceViewResolver […]

Spring Security:多个HTTP配置不起作用

我正在尝试使用Spring Security,我有一个用例,我想要保护不同的登录页面和不同的URL集。 这是我的配置: @Configuration @Order(1) public static class ProviderSecurity extends WebSecurityConfigurerAdapter{ @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers(“/”, “/home”).permitAll() .antMatchers(“/admin/login”).permitAll() .antMatchers(“/admin/**”).access(“hasRole(‘BASE_USER’)”) .and() .formLogin() .loginPage(“/admin/login”).permitAll() .defaultSuccessUrl(“/admin/home”) .failureUrl(“/admin/login?error=true”).permitAll() .usernameParameter(“username”) .passwordParameter(“password”) .and() .csrf() .and() .exceptionHandling().accessDeniedPage(“/Access_Denied”); } } @Configuration @Order(2) public static class ConsumerSecurity extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { […]

您是否需要数据库事务来读取数据?

当我尝试从数据库中读取数据时,至少使用 ((Session)em.getDelegate()).createCriteria() 抛出一个例外,表示交易不存在。 当我添加注释时: @Transactional( value = SomeClass.TRANSACTIONAL_MANAGER, propagation = Propagation.SUPPORTS, readOnly = true ) 它工作正常。 但是,由于读取将每秒发生数百万次访问和读取数据,我想确保我们的环境不会被不必要地堵塞。 如果没有,创建只读Propagation.Supports事务的成本是多少? 没有事务,我可以不与Spring一起创建一个Hibernate Criteria Query吗?

如何使用Spring @Value从java属性文件中填充HashMap

是否可以使用Spring @Value将属性文件中的值映射到HashMap。 目前我有类似的东西,映射一个值不是问题。 但我需要在HashMap过期中映射自定义值。 这样的事情可能吗? @Service @PropertySource(value = “classpath:my_service.properties”) public class SomeServiceImpl implements SomeService { @Value(“#{conf[‘service.cache’]}”) private final boolean useCache = false; @Value(“#{conf[‘service.expiration.[]’]}”) private final HashMap expirations = new HashMap(); 属性文件:’my_service.properties’ service.cache=true service.expiration.name1=100 service.expiration.name2=20 像这个键映射是否可行:值集 name1 = 100 name2 = 20

具有任意AND子句的动态spring数据jpa存储库查询

我正在使用Spring data jpa repositories ,需要为不同的字段提供搜索function。 在搜索之前输入字段是可选的。我有5个字段表示EmployeeNumber , Name , Married , Profession和DateOfBirth 。 这里我只需要用户查询给定的值,其他字段应该被忽略.Ex, Input : EmployeeNumber: ,Name:St,Married: ,Professsion:IT,DateOfBirth: Query : Select * from Employee e where Name like ‘St%’ and Profession like ‘IT%’; Input : EmployeeNumber:10,Name: ,Married: ,Professsion:IT,DateOfBirth: Query : Select * from Employee e where EmployeeNumber like ‘10%’ and Profession like ‘IT%’; 所以我们在这里考虑输入和查询的值。 […]