Spring中属性注入的方式

什么是属性注入?
属性注入就是在实例化对象时,同时向对象中的属性进行相应的赋值。即,通俗点说,属性注入就是给类中的属性赋值。

在Java中(非Spring、非框架)的属性注入方式:
1.通过set方法注入

public class Student{
  private String name;
  public void setName(String name) {
    this.name = name;
  }
}
Student student = new Student();
student.setName("棒冰冰");

2.通过有参构造方式注入

public class Student{
   private String name;
   public Student(String name){
     this.name = name;
   }
}
Student student = new Student("棒棒冰");

3.通过接口的方式实现

public interface UserDao{
  public void delete(String id);
}

public class UserDaoImplUserDao{
  private Integer id;
  public void delete (Integer id) {
    this.id = id;
  }
}

Spring中属性注入的方式
1.set方式注入(常用)
pom.xml的配置文件


    4.0.0

    com.zh
    property-test
    1.0.0-SNAPSHOT

    
        
            org.springframework
            spring-context
            4.3.17.RELEASE
        
    



spring-context.xml的配置文件



   
    
        
        
    

User.java

public class User {
    private String username;

    public void setUsername(String username){
        this.username = username;
    }
    public void getUsername(){
        System.out.println("User---------------"+username);
    }
}

UserTest.java

public class UserTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
       User user= (User) context.getBean("user");
        user.getUsername();

    }
}
  1. 有参构造的方式注入
    pom.xml的配置文件

    4.0.0

    com.zh
    property-test
    1.0.0-SNAPSHOT

    
        
            org.springframework
            spring-context
            4.3.17.RELEASE
        
    

spring-context.xml的配置文件



    
    
        
    


PropertiesTest.java

public class PropertyTest {
    private String name;

    public PropertyTest(String name){
        this.name = name;
    }

    public void sayHi(){
        System.out.println("PropertyTest----"+name);
    }
}

Test.java测试类

public class Test{
    public static void main(String[] args){
       ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
        PropertyTest propertyTest = (PropertyTest) context.getBean("propertyTest");
        propertyTest.sayHi();
    }
}

输出结果: PropertyTest—-冰冰棒 ,则说名赋值成功

注入对象类型的属性(重要)
代码如下 :
pom.xml的配置文件


    4.0.0

    com.zh
    object-injection
    1.0-SNAPSHOT

    
        
            org.springframework
            spring-context
            4.3.17.RELEASE
        
    

spring-context.xml的配置文件



    

    
        
    

UserDao.java

public class UserDao {
    public void add(){
        System.out.println("UserDao-----add");
    }
}
UserService.java

public class UserService {
    private UserDao userDao;

    public void setUserDao(UserDao userDao){
        this.userDao = userDao;
    }

    public void add(){
        System.out.println("UserService-----add");
        userDao.add();
    }
}

UserTest.java测试类

public class UserTest {
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}

作者:onnoA

链接: https://www.jianshu.com/p/e05f3c2ff606

来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。