2
我想用在會話範圍豆,但是我收到一個錯誤:春季定義的Java配置會話範圍豆
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\testRestService\WEB-INF\classes\test\server\config\AppConfig.class];
nested exception is java.lang.annotation.AnnotationFormatError: Invalid default: public abstract org.springframework.beans.factory.annotation.Autowire org.springframework.config.java.annotation.Bean.autowire()
的人豆:
public class Person {
//This is a Pojo
//...
}
了AppConfig :
@Configuration
@EnableWebMvc
@ComponentScan("test.server")
public class AppConfig extends WebMvcConfigurerAdapter {
@Bean(scope = DefaultScopes.SESSION)
@ScopedProxy
public Person getPerson() {
return new Person();
}
}
的人Sercive:
@Component
public class PersonService implements IPersonService {
@Autowired
protected Person person;
@Override
public void setPerson(Person person) {
this.person = person;
}
@Override
public Person getPerson() {
return this.person;
}
}
的PersonController:
@RestController()
@RequestMapping("/person")
public class PersonController {
@Autowired
private IPersonService personService;
@InitBinder
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
binder.registerCustomEditor(Person.class, new GenericEditor<Person>(Person.class));
}
@RequestMapping(value = "/setPerson", method = RequestMethod.GET)
public String setPerson(@RequestParam(value = "person") Person person) {
this.personService.setPerson(person);
return "Person: " + person + " saved.";
}
@RequestMapping(value = "/getPerson", method = RequestMethod.GET)
public Person getPerson() {
return this.personService.getPerson();
}
}
我試圖Person Bean上使用@Scope("session")
,在這種情況下,我沒有在AppConfig中使用@ScopedProxy
註釋和使用的@org.springframework.context.annotation.Bean("person")
代替org.springframework.config.java.annotation.Bean(scope = DefaultScopes.SESSION)
。在這種情況下,我沒有收到錯誤,但是當我測試它時,Person bean不在會話範圍內。
感謝您的幫助。
如果你想能像你一樣設置人('setPerson'),你需要'PersonHolder'對象。當你做'this.person = person;'你用其他實例替換最初的自動裝配的作用域代理。 – 2014-09-06 08:32:36
謝謝,我將該代碼更改爲 @Override public void setPerson(Person person){this.person.setAge(person.getAge()); this.person.setName(person.getName()); } – laci0725 2014-09-06 08:50:14