spring-4

how to get multiple instances of same bean in spring?

怎甘沉沦 提交于 2019-11-27 07:43:37
问题 By default, spring beans are singletons. I am wondering if there is a way to get multiple instances of same bean for processing. Here is what I do currently @Configuration public class ApplicationMain { @Value("${service.num: not configured}") private int num; //more code @PostConstruct public void run(){ for (int i = 0; i < num ; i++) { MyService ser = new MyService(i); Future<?> tasks = executor.submit(ser); } } } Here is the Service class public class MyService implements Runnable {

How to turn on annotation driven validation in Spring 4?

五迷三道 提交于 2019-11-27 06:31:24
问题 I am using the annotation validation as below: public String processRegistration(@Valid Spitter spitter, Errors errors, Model model) { if (errors.hasErrors()) { return "registerForm"; } ... } But the errors.hasErrors() always return false. I guess I didn't turn on some kind of switch for the annotation driven validation. But how can I do it with JavaConfig in Spring 4? I tried to apply the @AnnotationDrivenConfig to the configuration class, but that type cannot even be resolved. ADD 1 I

Multiple scenarios @RequestMapping produces JSON/XML together with Accept or ResponseEntity

*爱你&永不变心* 提交于 2019-11-27 04:21:51
问题 I am working with Spring 4.0.7 About Spring MVC, for research purposes, I have the following: @RequestMapping(value="/getjsonperson", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE) public @ResponseBody Person getJSONPerson(){ logger.info("getJSONPerson - getjsonperson"); return PersonFactory.createPerson(); } @RequestMapping(value="/getperson.json", method=RequestMethod.GET) public @ResponseBody Person getPersonJSON(){ logger.info("getPerson - getpersonJSON"); return

WebSocket with Sockjs & Spring 4 but without Stomp

梦想与她 提交于 2019-11-27 02:51:41
Is there a way to use WebSockets with SockJS client and Spring 4 server but not using STOMP? Based on this tutorial from Spring's website, I know how to set up a WebSocket based application using Stomp and Spring 4. On the client side, we have: var socket = new SockJS('/hello'); stompClient = Stomp.over(socket); stompClient.connect({}, function(frame) { setConnected(true); console.log('Connected: ' + frame); stompClient.subscribe('/topic/greetings', function(greeting){ showGreeting(JSON.parse(greeting.body).content); }); }); And on the server side, we have the following in the controller:

When use ResponseEntity<T> and @RestController for Spring RESTful applications

这一生的挚爱 提交于 2019-11-27 02:25:46
I am working with Spring Framework 4.0.7, together with MVC and Rest I can work in peace with: @Controller ResponseEntity<T> For example: @Controller @RequestMapping("/person") @Profile("responseentity") public class PersonRestResponseEntityController { With the method (just to create) @RequestMapping(value="/", method=RequestMethod.POST) public ResponseEntity<Void> createPerson(@RequestBody Person person, UriComponentsBuilder ucb){ logger.info("PersonRestResponseEntityController - createPerson"); if(person==null) logger.error("person is null!!!"); else logger.info("{}", person.toString());

How to use JasperReports with Spring MVC?

岁酱吖の 提交于 2019-11-26 23:54:31
问题 I've been investigating the use of JasperReports (6.0.0) with Spring MVC (4.1.3) to generate PDF reports. Spring is rife with "Spring specific" ways to integrate with JasperReports to generate PDFs: Use JasperReportsPdfView relies on now deprecated JasperReport features Use JasperReportsMultiFormatView Use JasperReportsViewResolver I struggled to find good, complete examples online and wanted to share my findings (see my answer below). Feel free to add additional methods and/or improvements

Difference between @EntityScan and @ComponentScan

大城市里の小女人 提交于 2019-11-26 22:44:43
问题 I am trying to understand the difference here. I see that a class has been annotated with both of them with same package example : @Configuration @EntityScan("some.known.persistence") @ComponentScan({ "some.known.persistence"}) public class ApiConfig { } I understand the differences from API docs but want to understand in detail. Also does it mean that anything that is scanned by @ComponentScan is having wider visibility wrt Spring context and @EntityScan does not. If so using some attribute

Spring 4 - addResourceHandlers not resolving the static resources

安稳与你 提交于 2019-11-26 18:52:12
My maven spring project directory structure is shown below. I am using Spring-4 annotation based configuration. I configure the resources like below. I tried many ways that are suggested in many Stackoverflow questions and other websites Spring 4 loading static resources http://imwill.com/spring-mvc-4-add-static-resources-by-annotation/#.U5GZlXKs9i4 But the jsp files could not load the resources, all the static content requests returns 404 error. I tried these things in jsp, <link href="resources/css/bootstrap.css" rel="stylesheet" media="screen"> <link href="/resources/css/bootstrap.css" rel=

How to return JSON data from spring Controller using @ResponseBody

血红的双手。 提交于 2019-11-26 18:51:41
Spring version 4.2.0, Hibernate 4.1.4 Here is my Controller function: @RequestMapping(value = "/mobile/getcomp", method = RequestMethod.GET) @ResponseBody public List<Company> listforCompanies() { List<Company> listOfCompanies= new ArrayList<Company>(); listOfCompanies = companyManager.getAllCompanies(); return listOfCompanies; } Jackson JSON mapper dependency in Pom.xml : <!-- Jackson JSON Mapper --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>${jackson.version}</version> </dependency> Getting the list in my ArrayList , but when

AbstractMethodError on deploying Spring 4.0 in Tomcat 6

大兔子大兔子 提交于 2019-11-26 17:41:57
问题 I am getting below exception on deploying Spring 4.0.1 app in Tomcat 6.0.37: SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean#0': Invocation of init method failed; nested exception is java.lang.AbstractMethodError: org.hibernate.validator.internal