Tag: spring

为什么使用基于java的配置的Spring应用程序无法正常工作

我最近开始使用Spring框架开发一个带有ojective的项目来开发它,没有XML配置文件,只有Java代码。 在当前时刻,我将以下文件添加到我的项目中: WebAppConfig.java @EnableWebMvc @ComponentScan(value=”org.webapp”) @Configuration public class WebAppConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(“/css/**”).addResourceLocations(“/css/”).setCachePeriod(31556926); registry.addResourceHandler(“/css/**”).addResourceLocations(“/fonts/”).setCachePeriod(31556926); registry.addResourceHandler(“/img/**”).addResourceLocations(“/image/”).setCachePeriod(31556926); registry.addResourceHandler(“/js/**”).addResourceLocations(“/js/”).setCachePeriod(31556926); } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Bean public InternalResourceViewResolver getInternalResourceViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix(“/WEB-INF/jsp/”); resolver.setSuffix(“.jsp”); return resolver; } } SecurityConfig.java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter […]

在Spring中运行时注册bean(原型)

只需要社区评估的东西。 以下是一段代码,它是一个创建特定类型实例的简单工厂。 该方法将在上下文中将bean注册为原型并返回实例。 这是我第一次在运行时配置bean。 你能评价并提供反馈意见吗? 先谢谢你。 package au.com.flexcontacts.flexoperations; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConstructorArgumentValues; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.support.AbstractApplicationContext; import au.com.flexcontacts.exceptions.SyncClassCreactionError; /** * @author khushroo.mistry * Class purpose: Simple Factory to create an * instance of SynchroniseContactsService and register it in the Spring IoC. */ public final class FLEXSyncFactory implements […]

Multipart文件上传Spring Boot

我使用Spring Boot并希望使用Controller来接收多部分文件上传。 发送文件时,我不断收到错误415不支持的内容类型响应,并且永远不会到达控制器 There was an unexpected error (type=Unsupported Media Type, status=415). Content type ‘multipart/form-data;boundary=—-WebKitFormBoundary1KvzQ1rt2V1BBbb8’ not supported 我尝试使用form:action在html / jsp页面中以及在使用RestTemplate的独立客户端应用程序中发送。 所有尝试都给出了相同的结果 multipart/form-data;boundary=XXXXX not supported. 从多部分文档看来,边界参数必须添加到分段上传,但这似乎与接收”multipart/form-data”的控制器不匹配 我的控制器方法设置如下 @RequestMapping(value = “/things”, method = RequestMethod.POST, consumes = “multipart/form-data” , produces = { “application/json”, “application/xml” }) public ResponseEntity submitThing(HttpServletRequest request, @PathVariable(“domain”) String domainParam, @RequestParam(value = “type”) String thingTypeParam, @RequestBody […]

具有Spring Security和Java配置的自定义validation管理器

我正在使用Spring Security与SpringMVC创建一个Web应用程序(为了清楚起见,我将其称为WebApp),它与现有应用程序相对应(我将其称为BackendApp)。 我想将身份validation职责委派给BackendApp(这样我就不需要同步这两个应用程序)。 为了实现这一点,我希望WebApp(运行Spring安全性)通过REST与用户在表单中提供的用户名和密码与BackendApp进行通信,并根据BackendApp的响应是200 OK还是401 Unauthorized进行身份validation。 我知道我需要编写一个自定义的身份validation管理器来执行此操作,但我是一个非常新的spring,无法找到有关如何实现它的任何信息。 我相信我需要做这样的事情: public class CustomAuthenticationManager implements AuthenticationManager{ @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String username = authentication.getName(); String pw = authentication.getCredentials().toString(); // Code to make rest call here and check for OK or Unauthorised. // What do I return? } } 如果成功,我是否设置authentication.setAuthenticated(true),否则设置为false,那就是它? 编写完成后,如何使用java配置文件配置spring security以使用此身份validation管理器? 在此先感谢您的任何帮助。

如何在Spring XML配置中收集和注入给定类型的所有bean

Spring框架最强大的一个重点是dependency injection概念。 我理解其中的一个建议是将一般的高级机制与低级细节分开(如依赖性倒置原则所宣布的那样)。 从技术上讲,这可以归结为让bean实现尽可能少地了解作为依赖项注入的bean,例如 public class PrintOutBean { private LogicBean logicBean; public void action() { System.out.println(logicBean.humanReadableDetails()); } //… } 但是如果我想要一个在多个依赖bean上运行的高级机制呢? public class MenuManagementBean { private Collection options; public void printOut() { for (Option option:options) { // do something for option } //… } } 我知道一个解决方案是在单例bean中使用@Autowired注释,即…… @Autowired private Collection options; 但它不违反分离原则吗? 为什么我必须在我使用它们的同一个地方指定依赖者(即我的例子中的MenuManagementBean类)? 有没有办法像这样在XML配置中注入bean集合(在MMB类中没有任何注释)?

为什么不建议使用HibernateTemplate?

我习惯于在hibernate 3中使用getHibernateTemplate(),现在我正在转向Hibernate 4,在这里我没有找到以下类: org.springframework.orm.hibernate4.support.HibernateDaoSupport; 在这里我读到了它不再推荐使用 http://forum.springsource.org/showthread.php?117227-Missing-Hibernate-Classes-Interfaces-in-spring-orm-3.1.0.RC1 有人能解释我为什么吗? 现在我需要在hibernate 4中完成所有任务,如提交,关闭,刷新由getHibernateTemplate()方法自动管理的事务?

如何从BindingResult获取控制器中的错误文本

我有一个返回JSON的控制器。 它采用一种forms,通过弹簧注释validation自己。 我可以从BindingResult获取FieldError列表,但它们不包含JSP将在标记中显示的文本。 如何通过JSON发送错误文本? @RequestMapping(method = RequestMethod.POST) public @ResponseBody JSONResponse submit(@Valid AnswerForm answerForm, BindingResult result, Model model, HttpServletRequest request, HttpServletResponse response) { if (result.hasErrors()) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); JSONResponse r = new JSONResponse(); r.setStatus(JSONResponseStatus.ERROR); //HOW DO I GET ERROR MESSAGES OUT OF BindingResult??? } else { JSONResponse r = new JSONResponse(); r.setStatus(JSONResponseStatus.OK); return r; } } JSONREsponse类只是一个POJO […]

Spring安全CORSfilter

我们在现有项目中添加了Spring Security 。 从这一刻起,我们得到一个401 No ‘Access-Control-Allow-Origin’ header is present on the requested resource我们服务器No ‘Access-Control-Allow-Origin’ header is present on the requested resource错误上。 那是因为没有Access-Control-Allow-Origin标头附加到响应。 为了解决这个问题,我们在注销过滤Filter之前添加了我们自己的filter,它在Filter链中,但filter不适用于我们的请求。 我们的错误: XMLHttpRequest无法加载http://localhost:8080/getKunden 。 请求的资源上不存在“Access-Control-Allow-Origin”标头。 原因http://localhost:3000因此不允许访问。 响应具有HTTP状态代码401。 我们的安全配置: @EnableWebSecurity @Configuration @ComponentScan(“com.company.praktikant”) public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Autowired private MyFilter filter; @Override public void configure(HttpSecurity http) throws Exception { final UrlBasedCorsConfigurationSource source = […]

向前推进添加参数?

有没有办法将请求转发给另一个控制器,同时向其添加一些参数数据? 我尝试添加到ModelMap,但它似乎没有挂起。 我做的事情如下: return “forward:/my-other-controller”; 只有我能想到的其他方法是将参数放在会话上,然后在目标控制器中弹出它们。

Spring-Batch没有将元数据保存到数据库?

我想创建一个spring-batch作业,但我想在没有任何数据库持久性的情况下运行它。 不幸的是,spring-batch需要以某种方式将作业循环中的metadata写入数据库,从而促使我至少提供某种db与transactionmanager和entitymanager。 是否可以阻止元数据并独立于txmanagers和数据库运行? 更新: ERROR org.springframework.batch.core.job.AbstractJob: Encountered fatal error executing job java.lang.NullPointerException at org.springframework.batch.core.repository.dao.MapJobExecutionDao.synchronizeStatus(MapJobExecutionDao.java:158) ~[spring-batch-core-3.0.1.RELEASE.jar:3.0.1.RELEASE] at org.springframework.batch.core.repository.support.SimpleJobRepository.update(SimpleJobRepository.java:161) ~[spring-batch-core-3.0.1.RELEASE.jar:3.0.1.RELEASE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_51] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_51] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_51] at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_51] at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) ~[spring-aop-4.0.6.RELEASE.jar:4.0.6.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.0.6.RELEASE.jar:4.0.6.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.0.6.RELEASE.jar:4.0.6.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) ~[spring-tx-4.0.6.RELEASE.jar:4.0.6.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262) ~[spring-tx-4.0.6.RELEASE.jar:4.0.6.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) ~[spring-tx-4.0.6.RELEASE.jar:4.0.6.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.0.6.RELEASE.jar:4.0.6.RELEASE] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) ~[spring-aop-4.0.6.RELEASE.jar:4.0.6.RELEASE] […]