Spring?????????@Scope-??????-????????????-?????????
??????spring4.0??????
scope???????????????
When you create a bean definition, you create a recipe for creating actual instances of the class defined by that bean definition. The idea that a bean definition is a recipe is important, because it means that, as with a class, you can create many object instances from a single recipe.
?????????
1???singleton??????? ??
???? ??(Default) Scopes a single bean definition to a single object instance per Spring IoC container.
2???prototype??????? ??
???? ??Scopes a single bean definition to any number of object instances.
3???request???
???? ??Scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
4???session???
???? ??Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
5???globalSession???
???? ??Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a Portlet context. Only valid in the context of a web-aware Spring ApplicationContext.
6???application???
???? ??Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
7???websocket???
???? ??Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
????????????singleton???prototype??????????????????????????????
1???spring ??????scope ??????????????????
2???@Scope("prototype")??????spring?????????controller?????????????????????????????????
3???Spring???Action????????????@Scope("prototype")?????????????????????????????????????????????Action?????????
??????????????????@Scope("singleton"),??????????????????,??????????????????????????????Action???
?? ?? ??eg????????????????????????,???????????????????????????,???????????????,??????????????????????????????,Action???????????????????????????????????????
?????????
1. singleton???????????????
???? ??singleton?????????bean?????????????????????????????????????????????????????????????????????bean????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????singleton???bean???
???? ???????????singleton?????????bean?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????bean????????????????????????????????????????????????????????????????????????bean???scope???singleton?????????????????????scope???
2. prototype???????????????
???? ??scope???prototype???bean?????????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??????????????????????????????????????????
???? ????????????????????????????????????????????????????????????????????????????????bean???scope??????????????? prototype????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?????????????????????????????????????????????????????????????????????????????????????????????????????????scope??????prototype?????????????????????prototype??? bean???????????????????????????????????????????????????????????????????????????
??
???????????????
package com.yiibai.customer.services; public class CustomerService { String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
??
<beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean /> </beans>
???????????????
package com.yiibai.common; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yiibai.customer.services.CustomerService; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"}); CustomerService custA = (CustomerService)context.getBean("customerService"); custA.setMessage("Message by custA"); System.out.println("Message : " + custA.getMessage()); //retrieve it again CustomerService custB = (CustomerService)context.getBean("customerService"); System.out.println("Message : " + custB.getMessage()); } }
????????????
Message : Message by custA Message : Message by custA??
????????bean??????????CustomerService'???????????????????????????????????????????????custB??????????????????????????custA'????????????????????????????????????????getBean()???????????????????????????????????????Spring??IoC???????????????????????????????????????????????? getBean()???????????????????????????????????????????????????
??
<beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean scope="prototype"/> </beans>
??????-??????
Message : Message by custA Message : null
最后更新:2017-09-25 16:03:58