Spring Scope

时间:2015-11-23 22:18:32

标签: java spring spring-boot

使用下面的代码我试图设置请求属性的值" name"对象文本。当请求属性名称= Test2时,我引入了10秒的延迟。我发出一个名为= Test2的请求。当请求进行时,我将使用name = Test激活另一个请求。对于第二个请求,我看到name = Test得到打印而不是Test1,因为我猜bean文本是单例。当我在Text类中更新范围(" prototype")时,我看到DemoRestController.java中的Text.getData()的值为null。如何将Text对象的范围定义为原型并在DemoRestController中自动装配?

DemoApplication.java

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ComponentScan({"com.example"})
@ImportResource("application-context.xml")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

DemoRestController.java

package com.example;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoRestController {

    @Autowired
    Text text;
    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {

        if(text.getData().equalsIgnoreCase("Test2")){
            System.out.println("Matching!");
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        System.out.println(name + " = " + text.getData());
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }
}

Greeting.java

package com.example;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

GreetingInterceptor.java

package com.example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class GreetingInterceptor extends HandlerInterceptorAdapter{
    @Autowired
    Text text;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        // TODO Auto-generated method stub
        System.out.println(" Intecepted :: " + request.getParameter("name"));
        text.setData(request.getParameter("name"));
        return super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        // TODO Auto-generated method stub
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        // TODO Auto-generated method stub
        super.afterCompletion(request, response, handler, ex);
    }

    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        // TODO Auto-generated method stub
        super.afterConcurrentHandlingStarted(request, response, handler);
    }





}

Text.java

package com.example;

import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;

@Component
public class Text {

    private String data;

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }



}

应用context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- <import resource = "classpath:config.xml"/>  -->
    <context:annotation-config/>
    <context:property-placeholder/>
    <!-- <context:component-scan base-package="com.comcast.customer.db" /> -->
    <!-- <context:property-placeholder location="classpath:customer-config.properties" />-->

    <!-- <bean id="billingConnector"
        class="com.comcast.billing.connector.app.BillingConnector"/> -->
    <!-- <bean id="LocationServiceConnector"
        class="com.comcast.cx.LocationServiceConnector.LocationServiceConnector"/> -->
        <mvc:interceptors>
   <mvc:interceptor>
     <mvc:mapping path="/greeting/**" />
     <bean class="com.example.GreetingInterceptor" />
   </mvc:interceptor>
</mvc:interceptors>
</beans>

0 个答案:

没有答案