Spring's applicationContext default constructor with no arguments to create objects

 1 public class User {
 2     private String name;
 3   
 4     public User() {
 5         System.out.println("User的无参构造");
 6     }
 7 
 8     public String getName() {
 9         return name;
10     }
11 
12     public void setName(String name) {
13         this.name = name;
14     }
15 
16 }
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         https://www.springframework.org/schema/beans/spring-beans.xsd">
 6     <bean id="user" class="com.rzk.pojo.User">
 . 7          <Property name = " name " value = " Bob " /> use of non-default parameter settings 
. 8      </ the bean>
 . 9  
10  
. 11 </ Beans>
1 public class Mytext {
2     public static void main(String[] args) {
3         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
4         User user = (User) context.getBean("user");
5         <!--System.out.println(user.getName());-->
6      application一注册就实例化
7     }
8 }

When we create ApplicationContext object while the object will be created along with bean configuration file configuration

(Starving mode) at this time no-argument constructor is also loaded

 The above parameter setting value is not used

-------------

The following parameters are set

 1 public class User {
 2     private String name;
 3 
 4     public User(String name) {
 5         this.name = name;
 6         System.out.println("User的有参构造");
 7     }
 8 
 9     public String getName() {
10         return name;
11     }
12 
13     public void setName(String name) {
14         this.name = name;
15     }
16 
17 }

By setting the value of direct parameter name

 1  <?xml version="1.0" encoding="UTF-8"?>
 2  <beans xmlns="http://www.springframework.org/schema/beans"
 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4         xsi:schemaLocation="http://www.springframework.org/schema/beans
 5          https://www.springframework.org/schema/beans/spring-beans.xsd">
 6      <bean id="user" class=""com.rzk.pojo.User>
 7  <!--        <property name="name" value="小明"/>-->
 8          <constructor-arg name="name"  value="小明" />使用有参注入值
 9      </bean>
10  </beans>

 

-------------

By subscript assignment

 1 public class User {
 2     private String name;
 3     private int age;
 4 
 5     public User(String name, int age) {
 6         this.name = name;
 7         this.age = age;
 8         System.out.println("User的有参构造");
 9     }
10 
11     public String getName() {
12         return name;
13     }
14 
15     public void setName(String name) {
16         this.name = name;
17     }
18 
19     public int getAge() {
20         return age;
21     }
22 
23     public void setAge(int age) {
24         this.age = age;
25     }
26 }
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         https://www.springframework.org/schema/beans/spring-beans.xsd">
 6     <bean id="user" class="com.rzk.pojo.User">
 7         <constructor-arg index="0"  value="小明" />
 8         <constructor-arg index="1" value="12"/>
 9     </bean>
10 </beans>

 

Guess you like

Origin www.cnblogs.com/rzkwz/p/12637284.html