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