Spring使用java config在会话范围内定义bean

时间:2014-09-06 08:10:27

标签: java spring rest javabeans

我想在会话范围内使用bean,但收到错误:

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();
    }
}

The 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不在会话范围内。

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

通过JDK动态代理支持会话级bean。您的代码需要进行以下修改:

  • 从AppConfig类中删除getPerson()方法
  • 不要在PersonService中自动装配你的bean,只需用它的默认构造函数实例化它
  • 使用以下内容注释PersonService:
    • @Scope(值=&#34;会话&#34;,proxyMode = ScopedProxyMode.INTERFACES)

答案 1 :(得分:0)

注释具有bean作用域的类,如下所示:

@Scope(value = [scope],proxyMode = ScopedProxyMode.TARGET_CLASS)

  

代理模式TARGET_CLASS:用于创建bean的AOP代理。   当注入短期的bean作用域(请求,   会话…)到长期存在的bean作用域(单例,原型)或注入   原型到单例范围。

@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
@Component
public class Address {
    private String address = "US";

    public Address() {
        System.out.println("Create new Address: " + this.address);
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}


@Component
public class Customer {

    @Autowired
    private Address address;

    public String getCustomerAddress(){
        return address.getAddress();
    }

    public void setCustomerAddress(String address){
        this.address.setAddress(address);
    }


}