属性注入:创建对象的时候,向类里面的属性设置值。
Java属性注入有三种方法:
- 使用set方法注入
- 有参数构造注入
- 使用接口注入
Spring框架里面的属性注入方式
- 有参数构造属性注入
- set方法属性注入
有参数构造属性注入
编写User.java
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为属性赋值
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.java
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); } }
运行输出
Jacky is 22 years old, living in China
set方法属性注入
编写User.java
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