1.Three ways for spring to create objects

1.Spring Framework

Framework (Framework): In the field of Java development, the specific performance is a series of jar files, which usually stipulate some special data processing procedures or programming methods to solve specific problems.

The main problem solved by the Spring framework is: creating objects and managing objects . Simply put, in the traditional development mode, when an object is needed, you can write such User user = new User();code in the project to create the object, or you can user.setName("Alex");assign values ​​to the properties in this way, or declare it public static User user;to add staticModifiers make it live longer in memory, etc. When you use the Spring framework, you do n’t have to write the above code, and the related work can be handed over to the Spring framework, mainly through configuration files or annotations. solve these problems.

2. Create a project based on the Spring framework

Create a Maven Project . During the creation process, check Create a simple Project , Group Id is  ***, Artifact Id is ***, and Packaging is selected jar(or war).

After the creation is completed, the Spring project needs to add the dependency in pom.xml . For spring-contextthe code of https://mvnrepository.com/the dependency , you can search for the name of the dependency to determine the dependency and version used. Then, manually add the node in pom.xml<dependencies> and paste from Dependency code found on the website:

<dependencies>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.1.5.RELEASE</version>
  </dependency>
</dependencies>

3. Get objects through the Spring framework

First create an XML file under src / main / resources to empty, and add the following code (this XML file is the spring configuration file)

<?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:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util " 
    xmlns: jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    
</beans>

3.1. Create an object through a parameterless construction method (the created class must have a parameterless construction method )

Assuming that you need to create a date type object through Spring, you can add it in the configuration file:

<!-Create a Date object through the Spring framework-> 
<!-Id attribute: custom name, which means Bean Id, and the object will be obtained through this value in the future. It is usually recommended to use the class name to change the first letter to lower case-> 
< !-class attribute: the object of which class needs to be created, the value is the full name of the class-> 
<bean id = "date" class = "java.util.Date"> </ bean>

After the addition is complete, if you start Spring, the Spring framework will automatically create java.util.Datean object of the class, and the object name is date, the developer can dateget the object by this name!

test:

  Create a main()class with methods, load the above configuration file, you can get the Spring container, and get the required objects from the container:

public  class the Test { 
  public  static  void main (String [] args) {
     // Load Spring profile Spring container acquire 
    the ClassPathXmlApplicationContext AC
       = new new the ClassPathXmlApplicationContext ( "the applicationContext.xml" ); 
    
    // get the object from the container Spring, invoking getBean () Method, the parameter is the Bean Id 
    Date date = ac.getBean ("date", Date.class ) 
    
    in the configuration file ; // Test 
    System.out.println (date); 
    
    // Close 
    ac.close (); 
  } 
}

3.2. Creating objects through static factory methods

Static factory method: A method exists in a class, the method is staticdecorated, and the return type of the method is the type of the current class, for example, the Calendarclass has a static factory method, which exists in the class:

public static Calendar getInstance() {
  // ...
}

Because it Calendaris an abstract class, it is impossible to new Calendar()create objects through this syntax. Therefore, if you need to create an object, use the above static factory method in the Spring configuration file:

<!-- factory-method:工厂方法的名称 -->
<bean id="calendar" class="java.util.Calendar" factory-method="getInstance"></bean>

test:

class public the Test { 
  public static void main (String [] args) { 
    // Load Spring profile Spring container acquire     the ClassPathXmlApplicationContext AC 
      = the ClassPathXmlApplicationContext new new ( "the applicationContext.xml" );  // get the object from the container Spring, invoking getBean () Method, the parameter is the Bean Id  calendar Calendar = ac.getBean ("calendar", Calendar.class );  // Test System.out.println (Calendar); // close ac.close (); }}

    
    

3.3. Creating objects through instance factory methods

If a class cannot create an object through a parameterless constructor (there is no parameterless constructor at all, or an abstract class), and there is no static factory method, but as long as there are instance factory methods, objects can also be created by Spring!

Instance factory method: There is another method in the class, you can create the object of the required class! Such as the need to create a UserDaoclass, however, UserDaothe premise does not meet the no-argument constructor and static factory methods, but there is UserDaoFactoryclass that has a return value of a method type is UserDao, you can, return to this UserDaotype of method is an instance factory method In actual operation, you need to create UserDaoFactorythe object (instance) first, and then call the method of the created object to get the UserDaotype of object.

as follows:

public class UserDao {
​
  public UserDao(Object object) {
  }
  
}
public class UserDaoFactory {
  
  public UserDao newInstance() {
    return new UserDao(null);
  }
​
}

When configuring, you need to configure first UserDaoFactoryso that Spring can create an UserDaoFactoryobject of the factory class , and then, during configuration UserDao, factory-beanspecify the factory by:

<!-factory-bean: Which bean's factory method to call to create the object of the current class, the value is Bean 
Id- > <bean id = "userDaoFactory" class = "cn.tedu.spring.UserDaoFactory"> < / bean> 
<bean id = "userDao" class = "cn.tedu.spring.UserDao" factory -bean = "userDaoFactory" factory -method = "newInstance"> </ bean>

 test:

public  class the Test { 
  public  static  void main (String [] args) {
     // Load Spring profile Spring container acquire 
    the ClassPathXmlApplicationContext AC
       = new new the ClassPathXmlApplicationContext ( "the applicationContext.xml" ); 
    
    // get the object from the container Spring, invoking getBean () Method, the parameter is the Bean Id in the configuration file 
    UserDao userDao = ac.getBean ("userDao", UserDao. Class ); 
    
    // Test 
    System.out.println (userDao); 
    
    // Close 
    ac.close (); 
  } 
}

 

Guess you like

Origin www.cnblogs.com/caoxingchun/p/12716991.html