关于spring:Spring认证-Bean-范围教程

6次阅读

共计 3251 个字符,预计需要花费 9 分钟才能阅读完成。

定义 <bean> 时,您能够抉择申明该 bean 的作用域。例如,要强制 Spring 在每次须要时生成一个新的 bean 实例,您应该将 bean 的 scope 属性申明为 prototype。相似地,如果您心愿 Spring 在每次须要时返回雷同的 bean 实例,您应该将 bean 的 scope 属性申明为 singleton。

Spring Framework 反对以下五个范畴,其中三个仅在您应用 web-aware ApplicationContext 时可用。

在本章中,咱们将探讨前两个范畴,其余三个范畴将在探讨 Web 感知 Spring ApplicationContext 时探讨。

单例范畴
如果范畴设置为单例,则 Spring IoC 容器将创立该 bean 定义定义的对象的一个​​实例。该单个实例存储在此类单例 bean 的缓存中,并且对该命名 bean 的所有后续申请和援用都返回缓存对象。

默认范畴始终是单例。然而,当您只须要一个 bean 实例时,您能够在 bean 配置文件中将 scope 属性设置为 singleton,如上面的代码片段所示 –

<!– A bean definition with singleton scope –>
<bean id = “…” class = “…” scope = “singleton”>
<!– collaborators and configuration for this bean go here –>
</bean>
示例
让咱们有一个工作的 Eclipse IDE 并采取以下步骤来创立一个 Spring 应用程序 –

这是 HelloWorld.java 文件的内容 -

package com.tutorialspoint;

public class HelloWorld {
private String message;

public void setMessage(String message){

  this.message  = message;

}
public void getMessage(){

  System.out.println("Your Message :" + message);

}
}
以下是 MainApp.java 文件的内容 -

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
public static void main(String[] args) {

  ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
  HelloWorld objA = (HelloWorld) context.getBean("helloWorld");

  objA.setMessage("I'm object A");
  objA.getMessage();

  HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
  objB.getMessage();

}
}
以下是单例范畴所需的配置文件 Beans.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”
xsi:schemaLocation = “http://www.springframework.org/schema/beans
http://www.springframework.or…d”>

<bean id = “helloWorld” class = “com.tutorialspoint.HelloWorld” scope = “singleton”>
</bean>

</beans>
实现源文件和 bean 配置文件的创立后,让咱们运行应用程序。如果您的应用程序一切正常,它将打印以下音讯 –

Your Message : I’m object A
Your Message : I’m object A
原型范畴
如果范畴设置为原型,则每次收回对该特定 bean 的申请时,Spring IoC 容器都会创立该对象的一个​​新 bean 实例。通常,对所有有状态 bean 应用原型作用域,对无状态 bean 应用单例作用域。

要定义原型范畴,您能够在 bean 配置文件中将范畴属性设置为原型,如以下代码片段所示 –

<!– A bean definition with prototype scope –>
<bean id = “…” class = “…” scope = “prototype”>
<!– collaborators and configuration for this bean go here –>
</bean>
示例
让咱们应用 Eclipse IDE 并依照以下步骤创立一个 Spring 应用程序 –

这是 HelloWorld.java 文件的内容

package com.tutorialspoint;

public class HelloWorld {
private String message;

public void setMessage(String message){

  this.message  = message;

}
public void getMessage(){

  System.out.println("Your Message :" + message);

}
}
以下是 MainApp.java 文件的内容 -

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
public static void main(String[] args) {

  ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
  HelloWorld objA = (HelloWorld) context.getBean("helloWorld");

  objA.setMessage("I'm object A");
  objA.getMessage();

  HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
  objB.getMessage();

}
}
以下是原型范畴所需的配置文件 Beans.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”
xsi:schemaLocation = “http://www.springframework.org/schema/beans
http://www.springframework.or…d”>

<bean id = “helloWorld” class = “com.tutorialspoint.HelloWorld” scope = “prototype”>
</bean>

</beans>
实现源文件和 bean 配置文件的创立后,让咱们运行应用程序。如果您的应用程序一切正常,它将打印以下音讯 –

Your Message : I’m object A
Your Message : null

正文完
 0