Tag: spring

会话中的Spring存储对象

我想用Spring实现一个购物车,所以我需要在会话中保存一个对象Cart (它有像products,paymentType和deliveryType这样的属性)。 我试图用bean创建它,并将属性“scope”设置为“session”,但它只是不起作用,我应该在我的控制器或Cart类中使用一些额外的注释吗? 任何使用示例都非常有用:-)在此先感谢。

如何在一个Spring应用程序中的web.xml中注册多个servlet

我想在我的Spring web.xml中定义两个servlet – 一个用于应用程序html / jsp页面,另一个用于将由外部应用程序调用的Web服务。 这是web.xml: myservlet org.springframework.web.servlet.DispatcherServlet 1 myservlet *.htm contextConfigLocation WEB-INF/user-service-servlet.xml user-webservice org.apache.cxf.transport.servlet.CXFServlet 1 user-webservice /UserService/* 如果我有myservlet自己在文件中使用DispatcherServlet,它工作正常。 如果我的user-webservice带有context-param,那就是它的配置文件(user-service-servlet.xml),它可以正常工作。 但是,如果我同时在文件中,则myservlet不起作用,因为myservlet-servlet.xml文件未自动加载。 如果我删除了context-param,那么myservlet可以工作,但是user-webservice不能正常工作,因为它没有加载配置文件(user-service-servlet.xml)。 如何定义两个servlet并加载它们的两个配置文件?

spring singleton bean字段未填充

我需要一个服务(单身适合)与一些内部字段,如一个挂起的线程列表(是的一切都被编写为线程安全)问题是,如果我@autowire这个bean,字段似乎是空的。 调试我看到代理正确绑定到实例(字段CGLIB$CALLBACK_X正确链接到填充的bean),填充字段,但它提供的字段为空。 以下代码行概括了我正在谈论的内容。 @Service public class myService{ @Autowired private Monitor monitor; public List getSomething(){ return monitor.getList(); } } @Service public class myStatefulService{ //This field will be populated for sure by someone before getSomething() is called private List list; public synchronized List getSomething(){ return this.list; } //Called by other services that self inject this bean public […]

无法提交JPA事务:事务标记为rollbackOnly

我在我正在处理的一个应用程序中使用Spring和Hibernate,我遇到了处理事务的问题。 我有一个服务类,它从数据库中加载一些实体,修改它们的一些值,然后(当一切都有效时)将这些更改提交给数据库。 如果新值无效(我只能在设置后检查),我不想保留更改。 为了防止Spring / Hibernate保存更改,我在方法中抛出exception。 但是会导致以下错误: Could not commit JPA transaction: Transaction marked as rollbackOnly 这是服务: @Service class MyService { @Transactional(rollbackFor = MyCustomException.class) public void doSth() throws MyCustomException { //load entities from database //modify some of their values //check if they are valid if(invalid) { //if they arent valid, throw an exception throw new […]

使用AuthenticationFailureHandler在Spring Security中自定义身份validation失败响应

目前,每当用户validation失败时,Spring安全性响应: {“error”: “invalid_grant”,”error_description”: “Bad credentials”} 我想通过以下响应代码来增强此响应: {“responsecode”: “XYZ”,”error”: “invalid_grant”,”error_description”: “Bad credentials”} 经过一番探讨,看起来我需要做的就是实现一个AuthenticationFailureHandler,我已经开始做了。 但是,每当我提交无效的登录凭据时,似乎都无法访问onAuthenticationFailure方法。 我已经逐步完成了代码,并在onAuthenticationFailure方法中进行了登录,以确认它未被访问。 我的失败处理程序是: @Component public class SSOAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler{ @Override public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { super.onAuthenticationFailure(request, response, exception); response.addHeader(“responsecode”, “XYZ”); } } 我的WebSecurityConfigurerAdapter包含: @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired SSOAuthenticationFailureHandler authenticationFailureHandler; @Override protected […]

Spring-Hibernate – 找不到当前线程的Session

我有一个使用spring和hibernate的java stuts2 web应用程序。 我得到org.hibernate.HibernateException: No Session found for current thread 。 SpringBean.xml org.hibernate.dialect.MySQLDialect true update hibernate.cfg.xml <!– –> 的hibernate.cfg.xml CustomerServiceImpl.java package org.rohith.service.impl; import org.rohith.dao.impl.CustomerDaoImpl; import org.rohith.model.Customer; import org.rohith.service.CustomerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class CustomerServiceImpl implements CustomerService { @Autowired private CustomerDaoImpl customerDaoImpl; @Override public void saveCustomer(Customer customer) { customerDaoImpl.saveCustomer(customer); } public CustomerDaoImpl getCustomerDaoImpl() { […]

在Spring Boot 1.4中测试安全性

我正在尝试使用SecurityConfig类中定义的自定义安全设置来测试@WebMvcTest : @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers(“/admin*”).access(“hasRole(‘ADMIN’)”).antMatchers(“/**”).permitAll().and().formLogin(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().withUser(“user”).password(“password”).roles(“ADMIN”); } } 测试类是: @RunWith(SpringRunner.class) @WebMvcTest(value = ExampleController.class) public class ExampleControllerMockMVCTest { @Autowired private MockMvc mockMvc; @Test public void indexTest() throws Exception { mockMvc.perform(get(“/”)) .andExpect(status().isOk()) .andExpect(view().name(“index”)); } […]

使用Spring IoC和JavaConfig配置AspectJ方面?

根据Spring的文档使用Spring IoC配置AspectJ方面以配置Spring IOC的方面,必须在xml配置中添加以下内容: 正如@SotiriosDelimanolis所建议的那样,在JavaConfig中将以下内容重写为: @Bean public com.xyz.profiler.Profiler profiler() { com.xyz.profiler.Profiler profiler = com.xyz.profiler.Profiler.aspectOf(); profiler.setProfilingStrategy(jamonProfilingStrategy()); // assuming you have a corresponding @Bean method for that bean return profiler; } 但是,如果Profiler方面是用native aspectj .aj语法编写的,这似乎只能起作用。 如果它是用Java编写的并使用@Aspect注释,则会收到以下错误消息: 对于类型Profiler,方法aspectOf()未定义 对于使用@AspectJ语法编写的方面,是否有使用JavaConfig编写此方法的等效方法?

Spring-Jersey:如何返回静态内容?

题 : 如何公开我的css/ , images/ , js/和其他静态文件? 如何为我的索引视图返回控制器(而不是String方法)中的JSP页面? 问题 : 在努力解决问题#1时,其他项目使用filterjersey.config.servlet.filter.staticContentRegex (如此处所示) Stackoverflow问题我无法在项目设置中找到正常工作的依赖项。 在努力解决问题#2时,我试图引入依赖关系来使用Viewable 。 问题 – 传递依赖关系会对使用适当的Spring和Jersey类别(将雪球变成模糊错误的兔子洞)对webapp产生负面影响 完成项目 > Github项目 依赖关系 > 完整的POM文件 org.glassfish.jersey.containers jersey-container-servlet 2.15 org.glassfish.jersey.ext jersey-spring3 2.15 org.springframework spring-web 3.2.3.RELEASE commons-logging commons-logging 1.1 javax.servlet servlet-api Web.xml > Web.xml org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:applicationContext.xml Jersey org.glassfish.jersey.servlet.ServletContainer javax.ws.rs.Application com.component.ResourceRegister 1 Jersey /* Controller > SpringController.java @Path(“/”) […]

使用Spring restTemplate跟随302重定向?

RestTemplate restTemplate = new RestTemplate(); final MappingJackson2XmlHttpMessageConverter converter = new MappingJackson2XmlHttpMessageConverter(); final List supportedMediaTypes = new LinkedList(converter.getSupportedMediaTypes()); supportedMediaTypes.add(MediaType.ALL); converter.setSupportedMediaTypes(supportedMediaTypes); restTemplate.getMessageConverters().add(converter); ResponseEntity response = restTemplate.getForEntity(urlBase, MyDTO[].class); HttpHeaders headers = response.getHeaders(); URI location = headers.getLocation(); // Has my redirect URI response.getBody(); //Always null 我的印象是会自动跟踪302。 这个假设我不正确吗? 我现在需要选择这个位置并重新申请?