问题描述
我想在会话范围内使用bean,但收到错误消息:
I would like to use a bean in session scope, but I receive an error:
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:
The AppConfig:
@Configuration
@EnableWebMvc
@ComponentScan("test.server")
public class AppConfig extends WebMvcConfigurerAdapter {
@Bean(scope = DefaultScopes.SESSION)
@ScopedProxy
public Person getPerson() {
return new Person();
}
}
服务的人
@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:
The 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不在会话范围内.
I tried to use @Scope("session")
on the Person bean, in this case I did not use the @ScopedProxy
annotation in appconfig and used the @org.springframework.context.annotation.Bean("person")
instead of org.springframework.config.java.annotation.Bean(scope = DefaultScopes.SESSION)
. In this case I didn't receive an error, but the Person bean wasn't in the session scope when I tested it.
感谢您的帮助.
推荐答案
JDK动态代理支持会话级Bean.您的代码中需要进行以下修改:
Session level beans are supported through JDK dynamic proxies. The following modifications are required in your code:
- 从AppConfig类中删除getPerson()方法
- 不要自动将您的bean连接到PersonService中,只需使用其默认构造函数实例化它即可.
- 使用以下注释批注PersonService:
- @Scope(值=会话",proxyMode = ScopedProxyMode.INTERFACES)
- remove the getPerson() method from the AppConfig class
- do not autowire your bean in PersonService, just instantiate it with it's default constructor
- annotate the PersonService with the following:
- @Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)
这篇关于Spring使用java config在会话范围内定义一个bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!