Spring to create an object of the java

First, provide the directory structure is as follows:

Each file code is provided below for reference:

UserDynamicFactory.java

 1 package cn.sxt.factory;
 2 
 3 import cn.sxt.vo.User;
 4 
 5 /**
 6  *     ��̬��������������
 7  *
 8  */
 9 public class UserDynamicFactory {
10     public User createUser(String name,int age){
11         return new User(name,age);
12     }
13 }
View Code

UserFactory.java

 1 package cn.sxt.factory;
 2 
 3 import cn.sxt.vo.User;
 4 /**
 5  * 
 6  */
 7 public class UserFactory {
 8     public static User createUser(String name,int age){
 9         return new User(name,age);
10     }
11 }
View Code

User.java

 1 package cn.sxt.vo;
 2 
 3 
 4 public class User {
 5     private String name;
 6     private int age;
 7     public User() {
 8     }
 9     public User(String name, int age) {
10         super();
11         this.name = name;
12         this.age = age;
13     }
14     public String getName() {
15         return name;
16     }
17     public void setName(String name) {
18         this.name = name;
19     }
20     public int getAge() {
21         return age;
22     }
23     public void setAge(int age) {
24         this.age = age;
25     }
26     @Override
27     public String toString() {
28         return "User [name=" + name + ", age=" + age + "]";
29     }
30 }
View Code

beans.xml

. 1 <XML Version = "1.0" encoding = "UTF-. 8"??>
 2 <Beans xmlns = "http://www.springframework.org/schema/beans"
 . 3      xmlns: the xsi = "HTTP: // WWW. w3.org/2001/XMLSchema-instance "
 4      xsi: schemaLocation =" http://www.springframework.org/schema/beans
 5          HTTP: // www.springframework.org/schema/beans/spring-beans.xsd " > 
6      <! - create an object with a reference constructor by ->
 . 7      <the bean ID = "U1" class = "cn.sxt.vo.User">
 . 8          <! - the parameters set by the parameter index scale starts at 0 ->
 . 9          <constructor Arg-index = "0" value = "Joe Smith crazy" />
10         <constructor-arg index="1" value="33"/>
11     </ the bean>
 12 is      <the bean ID = "U2" class = "cn.sxt.vo.User">
 13 is          <-! name to set the parameters represented by the parameter name of the parameter name ->
 14          <constructor Arg-name = "name" value = "Joe Smith crazy" />
 15          <constructor Arg-name = "Age" value = "33 is" />
 16      </ the bean>
 . 17      <the bean ID = "U3" class = "cn.sxt.vo the .user ">
 18 is          <-! parameters set by the type parameter indicating the type of parameter types ->
 . 19          <constructor Arg-type =" java.lang.String "value =" Joe Smith crazy "/>
 20 is          <constructor- arg type = "int"
value="33"/>
21     </bean>
22     <bean id="u4" class= "cn.sxt.vo.User">
 23 is          <Property name = "name" value = "zhangwuji" />
 24          <Property name = "Age" value = "23 is" />
 25      </ the bean>
 26 is      <! - - create a static factory object ->
 27      <bean the above mentioned id = "U5" class = "cn.sxt.factory.UserFactory" = factory's-Method, "createUser">
 28          <constructor-Arg of the type = "java.lang.String" value = "Joe Smith crazy" />
 29          <constructor Arg-type = "int" value = "33 is" />
 30      </ the bean>
 31 is      <-! create objects by dynamic plant ->
 32      <bean id="factory" class="cn.sxt.factory.UserDynamicFactory"/>
33     <bean id="u6" factory-bean="factory" factory-method="createUser">
34         <constructor-arg type="java.lang.String" value="张三疯"/>
35         <constructor-arg type="int" value="33"/>
36     </bean>
37 </beans>
View Code

SpringTest.java

 1 package cn.sxt.spring;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import cn.sxt.vo.User;
 8 
 9 public class SpringTest {
10     @Test
11     public void testHello(){
12         //��������
13         ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
14         User u=(User)ac.getBean("u6");
15         System.out.println(u);
16     }
17 }
View Code

 

A. To create objects via the constructor

1. Set the parameters by the parameter index value

<! - by constructor to create an object with a reference -> 
    <the bean ID = "U1" class = "cn.sxt.vo.User"> 
        - starting from index 0 to set the parameters by the index parameter <! -> 
        <constructor Arg-index = "0" value = "Joe Smith crazy" /> 
        <constructor Arg-index = ". 1" value = "33 is" /> 
    </ the bean>

2. Set the parameter name

<the bean ID = "U2" class = "cn.sxt.vo.User"> 
        <-! name to set the parameters by the parameter name indicates the name of the parameter -> 
        <constructor Arg-name = "name" value = "Zhang three crazy "/> 
        <constructor Arg-name =" Age "value =" 33 is "/> 
    </ the bean>

 3. The parameter type is set by

<the bean ID = "U3" class = "cn.sxt.vo.User"> 
        <-! parameters set by the type parameter indicating the type of parameter types -> 
        <constructor Arg-type = "java.lang.String" value = "Joe Smith crazy" /> 
        <constructor Arg-type = "int" value = "33 is" /> 
    </ the bean>

 

II. To create objects by no constructor parameters

<bean id="u4" class="cn.sxt.vo.User">
        <property name="name" value="张无忌"/>
        <property name="age" value="23"/>
    </bean>

Note: no need to provide the configuration parameters, a set method and have corresponding properties

III. To create objects through static factory

Static factory class:

/ ** 
 * static factory method 
 * created by static factory object 
 * / 
public  class UserFactory {
     public  static the User the createUser (String name, int Age) {
         return  new new the User (name, Age); 
    } 
}

Profiles

<! - create objects through static factory -> 
    <bean the above mentioned id = "U5" class = "cn.sxt.factory.UserFactory" = Factory's-Method, "createUser"> 
        <constructor-Arg of the type = "java.lang.String "value =" Joe Smith crazy "/> 
        <constructor Arg-type =" int "value =" 33 is "/> 
    </ the bean>

IV. To create dynamic objects by factory

Dynamic factory class

/ ** 
 * Dynamic factory to create an object 
 * 
 * / 
public  class UserDynamicFactory {
     public the User the createUser (String name, int Age) {
         return  new new the User (name, Age); 
    } 
}

Profiles

<!-- 通过动态工厂来创建对象 -->
    <bean id="factory" class="cn.sxt.factory.UserDynamicFactory"/>
    <bean id="u6" factory-bean="factory" factory-method="createUser">
        <constructor-arg type="java.lang.String" value="张三疯"/>
        <constructor-arg type="int" value="33"/>
    </bean>

 

Guess you like

Origin www.cnblogs.com/Vincent-yuan/p/11247789.html