Spring 3 ways of instantiating Bean

 

To use the Spring Bean, you need to create an instance of the Bean.

Examples of Bean There are three ways:

  • Constructor way
  • Static Factory mode
  • Examples factory mode

 

 

Constructor way

The constructor method is the most commonly used. Write Bean constructor, and then pass arguments in the configuration file.

 Example:

1, write a Bean, with a constructor to initialize the Bean

. 1  class Student {
 2      Private String name;
 . 3      Private  int Score;
 . 4  
. 5      // by the constructor initializes the object 
. 6      public Student (String name, int Score) {
 . 7          the this .name = name;
 . 8          the this .score = Score;
 . 9      }
 10  
. 11      public  void OUT () {
 12 is          System.out.println (name + "score is" + score);
 13 is      }
 14 }

 

 

2, disposed in the Bean Spring configuration file, passed arguments

1 <bean name="student" class="my_package.Student">
2         <constructor-arg value="张三" />
3         <constructor-arg value="90" />
4     </bean>

 

 

3, using the Bean

1 public class Test {
2     public static void main(String[] args) {
3         ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
4         Student student=applicationContext.getBean("student",Student.class);
5         student.out();   
6     }
7 }

 

 You can see the console output "John's score is 90."

 

 

 

 

Static Factory mode

You need to create a factory class, write a static method in the factory class, it returns an instance of the Bean.

Example:

1, write a Bean, still need the constructor

 1 class Student{
 2     private String name;
 3     private int score;
 4 
 5     //构造器
 6     public Student(String name,int score){
 7         this.name=name;
 8         this.score=score;
 9     }
10 
11     public void out(){
12         System.out.println(name+"的成绩是"+score);
13     }
14 }

 

 

2, writing a factory class, the static instance of the class production method

. 1  class Factory's {
 2      // is a static method 
. 3      public  static Student createStudent (String name, int Age) {
 . 4          return  new new Student (name, Age);
 . 5      }
 . 6 }

 Because the plant is used in the method for producing a static instance of the Bean, so called static factory method.

A factory can have multiple static methods, producing multiple instances of Bean.

 

 

3, configure Bean in the xml file, only need to configure the Bean, no need to configure factory.

<bean name="student" class="my_package.Factory" factory-method="createStudent">
        <constructor-arg value="张三" />
        <constructor-arg value="90" />
    </bean>

 Specifies the class factory class, factory-method for producing the static method specified Bean instance which, thus determining this Bean (class). We need to pass parameters to pass parameters.

Because it is a static method, directly through the factory class name called, so does not require factory class instance, there is no need to configure factory classes in the xml file.

 

 

4, using the example of Bea

1 public class Test {
2     public static void main(String[] args) {
3         ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
4         Student student=applicationContext.getBean("student",Student.class);
5         student.out();
6     }
7 }

 Console print "John's score is 90."

 

 

 

 

 

Examples factory mode

Bean production in the factory method is not static, you need to configure factory in the xml file, the Bean.

1, write a Bean, the constructor needs

. 1  class Student {
 2      Private String name;
 . 3      Private  int Score;
 . 4  
. 5      // by the constructor initializes the object 
. 6      public Student (String name, int Score) {
 . 7          the this .name = name;
 . 8          the this .score = Score;
 . 9      }
 10  
. 11      public  void OUT () {
 12 is          System.out.println (name + "score is" + score);
 13 is      }
 14 }

 

 

2, writing a factory class, Bean Examples of the method of production

1 class Factory{
2     //实例方法
3     public Student createStudent(String name,int age){
4         return new Student(name,age);
5     }
6 }

 

 

3, in the xml file configure factory class, the Bean

  <bean name="factory" class="my_package.Factory" />

    <bean name="student" factory-bean="factory" factory-method="createStudent">
        <constructor-arg value="张三" />
        <constructor-arg value="90" />
    </bean>

Because it is to create an instance by instance method Bean factory class, so you need to create an instance of the class factory, so we need to configure engineering in xml. Factory class is an ordinary Bean, configuration and general configuration of the same.

This Bean plant is by way of example, it is necessary to specify by factory-bean bean plants where, by the designated production factory-method Examples of the method of the bean, thus determining this Bean (class).

 

 

4, using the example of Bean

1 public class Test {
2     public static void main(String[] args) {
3         ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
4         Student student=applicationContext.getBean("student",Student.class);
5         student.out();
6     }
7 }

 

Guess you like

Origin www.cnblogs.com/chy18883701161/p/11117723.html