什么是属性注入?
属性注入就是在实例化对象时,同时向对象中的属性进行相应的赋值。即,通俗点说,属性注入就是给类中的属性赋值。
在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 UserDaoImpl UserDao{
  private Integer id;
  public void delete (Integer id) {
    this.id = id;
  }
}
Spring中属性注入的方式
1.set方式注入(常用)
pom.xml的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.zh</groupId>
    <artifactId>property-test</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.17.RELEASE</version>
        </dependency>
    </dependencies>
</project>
spring-context.xml的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">
   <!--通过set方法注入-->
    <bean id="user" class="com.zh.propperty.test.User">
        <!--注入对象的属性值-->
        <property name="username" value="小明"></property>
    </bean>
</beans>
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();
    }
}
2. 有参构造的方式注入
pom.xml的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.zh</groupId>
    <artifactId>property-test</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.17.RELEASE</version>
        </dependency>
    </dependencies>
</project>
spring-context.xml的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">
    <!--通过有参构造赋值-->
    <bean id="propertyTest" class="com.zh.propperty.test.PropertyTest">
        <constructor-arg name="name" value="冰冰棒"></constructor-arg>
    </bean>
</beans>
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的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.zh</groupId>
    <artifactId>object-injection</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.17.RELEASE</version>
        </dependency>
    </dependencies>
</project>
spring-context.xml的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">
    <bean id="userDao" class="com.zh.object.injection.UserDao"></bean>
    <bean id="userService" class="com.zh.object.injection.UserService">
        <property name="userDao" ref="userDao"></property>
    </bean>
</beans>
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();
    }
}