Java框架spring 學習筆記(六):屬性注入

屬性注入:建立對象的時候,向類裏面的屬性設置值。java

Java屬性注入有三種方法:spring

  1. 使用set方法注入
  2. 有參數構造注入
  3. 使用接口注入

 

Spring框架裏面的屬性注入方式框架

  1. 有參數構造屬性注入
  2. set方法屬性注入

 

有參數構造屬性注入函數

編寫User.javathis

 1 package com.example.spring;
 2 
 3 public class User {
 4     private String name;
 5     private Integer age;
 6     private String country;
 7 
 8     //基於構造函數的屬性注入
 9     public User(String name, Integer age, String country) {
10         this.name = name;
11         this.age = age;
12         this.country = country;
13     }
14 
15     public String toString() {
16         return name + " is " + age + " years old, living in " + country;
17     }
18 }

編寫Beans.xml,使用constructor-arg爲屬性賦值spa

 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 http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 
 6     <bean id="user" class="com.example.spring.User" >
 7         <constructor-arg name="name" type="java.lang.String" value="Jacky" />
 8         <constructor-arg name="age" type="java.lang.Integer" value="22"/>
 9         <constructor-arg name="country" type="java.lang.String" value="China"/>
10     </bean>
11 
12 </beans>

編寫Application.javacode

package com.example.spring;

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

public class Application {
    public static void main(String[] args) {
        //bean配置文件所在位置 D:\\IdeaProjects\\spring\\src\\Beans.xml
        //使用AbstractApplicationContext容器
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:\\IdeaProjects\\spring\\src\\Beans.xml");
        User user = (User)context.getBean("user");
        System.out.println(user);
    }
}

運行輸出xml

Jacky is 22 years old, living in China

 

set方法屬性注入對象

編寫User.javablog

 1 package com.example.spring;
 2 
 3 public class User {
 4     private String name;
 5     private Integer age;
 6     private String country;
 7 
 8     //基於set方法的屬性注入
 9     public void setName(String name) {
10         this.name = name;
11     }
12     public void setAge(Integer age) {
13         this.age = age;
14     }
15     public void setCountry(String country) {
16         this.country = country;
17     }
18     
19     public String toString() {
20         return name + " is " + age + " years old, living in " + country;
21     }
22 }

編寫Beans.xml,使用property關鍵字爲屬性賦值

 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 http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 
 6     <bean id="user" class="com.example.spring.User" >
 7         <property name="name" value="Jacky"></property>
 8         <property name="age" value="22"></property>
 9         <property name="country" value="China"></property>
10     </bean>
11 
12 </beans>

編寫Application.java

 1 package com.example.spring;
 2 
 3 import org.springframework.context.support.AbstractApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Application {
 7     public static void main(String[] args) {
 8         //bean配置文件所在位置 D:\\IdeaProjects\\spring\\src\\Beans.xml
 9         //使用AbstractApplicationContext容器
10         AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:\\IdeaProjects\\spring\\src\\Beans.xml");
11         User user = (User)context.getBean("user");
12         System.out.println(user);
13     }
14 }

運行輸出

Jacky is 22 years old, living in China
相關文章
相關標籤/搜索