Spring之依赖注入(DI)

Dependency Injection简称DI,依赖注入的作用就是在Spring框架创建对象的时候,将对象的属性注入Bean中。依赖注入一般有三种实现方式,一种是提供构造方法注入;另一种是通过属性Setter方法进行注入;还有P命名空间进行注入。

 

1)使用构造方法进行注入:

 

实体类代码:

 1 public class User {
 2     private String userName;
 3     private String userAge;
 4     private String userPsw;
 5     //无参构造方法
 6     public User() {
 7     }
 8     //有参构造
 9     public User(String userName, String userAge) {
10         this.userName = userName;
11         this.userAge = userAge;
12     }
13     //set与get方法
14     public String getUserName() {
15         return userName;
16     }
17 
18     public void setUserName(String userName) {
19         this.userName = userName;
20     }
21 
22     public String getUserAge() {
23         return userAge;
24     }
25 
26     public void setUserAge(String userAge) {
27         this.userAge = userAge;
28     }
29 
30     public String getUserPsw() {
31         return userPsw;
32     }
33 
34     public void setUserPsw(String userPsw) {
35         this.userPsw = userPsw;
36     }
37     @Override
38     public String toString() {
39         return "User{" +
40                 "userName='" + userName + '\'' +
41                 ", userAge='" + userAge + '\'' +
42                 ", userPsw='" + userPsw + '\'' +
43                 '}';
44     }
45 }

Beansxml文件的书写:

 

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     <bean id="user" class="com.yhd.pojo.User">
6         <constructor-arg index="0" value="不好喝的柚子茶"></constructor-arg>
7         <constructor-arg index="1" value="22"></constructor-arg>
8     </bean>
9 </beans>

 

测试代码:

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class userText {
    @Test
    public void text(){
        ApplicationContext context =new  ClassPathXmlApplicationContext("res/beans.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user);
    }
}

运行结果:

注:因为我们没有对userPsw属性注入值,所有输出的值为默认值null

 

2)使用setter方法注入

 

Bean的Xml代码:

 

1 </bean>
2     <bean id="user" class="com.yhd.pojo.User">
3         <property name="userName" value="不好喝的柚子茶"></property>
4         <property name="userAge" value="18"></property>
5         <property name="userPsw" value="123456"></property>
6     </bean>

 

测试代码:

 1 import org.junit.Test;
 2 import org.springframework.context.ApplicationContext;
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 
 5 public class userText {
 6     @Test
 7     public void text(){
 8         ApplicationContext context =new  ClassPathXmlApplicationContext("res/beans.xml");
 9         User user = context.getBean("user", User.class);
10         System.out.println(user);
11     }
12 }

运行截图:

1)使用P命名空间进行注入(底层实现是setter方法注入)

 

添加命名空间:

 1 xmlns:p="http://www.springframework.org/schema/p" 

Bean的xml代码:

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        xmlns:p="http://www.springframework.org/schema/p"
5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
6     <bean id="user" class="com.yhd.pojo.User" p:userName="不好喝的柚子茶" p:userAge="23" p:userPsw="666666"></bean>
7 </beans>

测试代码:

 1 import org.junit.Test;
 2 import org.springframework.context.ApplicationContext;
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 
 5 public class userText {
 6     @Test
 7     public void text(){
 8         ApplicationContext context =new  ClassPathXmlApplicationContext("res/beans.xml");
 9         User user = context.getBean("user", User.class);
10         System.out.println(user);
11     }
12 }

运行截图:

 

 

 

 

 

 

 

posted @ 2020-07-16 21:16  不好喝的柚子茶  阅读(213)  评论(0编辑  收藏  举报