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的配置文件

<?xml version="1.0"encoding="UTF-8"?>
<projectxmlns="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"?>
<beansxmlns="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方法注入-->
    <beanid="user"class="com.zh.propperty.test.User">
        <!--注入对象的属性值-->
        <propertyname="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();

    }
}
  1. 有参构造的方式注入

    pom.xml的配置文件

<?xml version="1.0"encoding="UTF-8"?>
<projectxmlns="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"?>
<beansxmlns="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 ">

    <!--通过有参构造赋值-->
    <beanid="propertyTest"class="com.zh.propperty.test.PropertyTest">
        <constructor-argname="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"?>
<projectxmlns="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"?>
<beansxmlns="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 ">

    <beanid="userDao"class="com.zh.object.injection.UserDao"></bean>

    <beanid="userService"class="com.zh.object.injection.UserService">
        <propertyname="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();
    }
}

作者:onnoA

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

来源:简书

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

原文 

http://www.anool.net/2020/07/11/spring-property-injection/

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » Spring中属性注入的方式

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址