課程
/后端開發
/Java
/Spring入門篇
若果需要應用作用域怎么辦
2017-10-14
源自:Spring入門篇 3-1
正在回答
//定義javaBean
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("prototype")
public class Person {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
public String getName() {
return name;
public void setName(String name) {
this.name = name;
//測試類
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
?* Hello world!
?*
?*/
public class App?
{
? ? public static void main( String[] args )
? ? {
? ? ? ? ApplicationContext appContext = new ClassPathXmlApplicationContext("/spring.xml");
? ? ? ? Person p1 = appContext.getBean(Person.class);
? ? ? ? System.out.println("p1's identityHashCode is " + System.identityHashCode(p1));
? ? ? ??
? ? ? ? Person p2 = appContext.getBean(Person.class);
? ? ? ? System.out.println("p2's identityHashCode is " + System.identityHashCode(p2));
? ? }
@Scope("")類型不同,作用域范圍不同
舉報
為您帶來IOC和AOP的基本概念及用法,為后續高級課程學習打下基礎
2 回答Bean的作用域
1 回答?Spring bean的默認作用域是什么?即不寫scope屬性的話它的作用域怎么理解呢?
2 回答proxyMode是啥意思,怎么用?加了這個后,作用域變了
2 回答關于作用域問題
3 回答bean作用域的類型
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2017-10-14
//定義javaBean
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("prototype")
public class Person {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//測試類
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
?* Hello world!
?*
?*/
public class App?
{
? ? public static void main( String[] args )
? ? {
? ? ? ? ApplicationContext appContext = new ClassPathXmlApplicationContext("/spring.xml");
? ? ? ? Person p1 = appContext.getBean(Person.class);
? ? ? ? System.out.println("p1's identityHashCode is " + System.identityHashCode(p1));
? ? ? ??
? ? ? ? Person p2 = appContext.getBean(Person.class);
? ? ? ? System.out.println("p2's identityHashCode is " + System.identityHashCode(p2));
? ? ? ??
? ? }
}
@Scope("")類型不同,作用域范圍不同