Spring中属性注入的方式

栏目: IT技术 · 发布时间: 5年前

内容简介:什么是属性注入?属性注入就是在实例化对象时,同时向对象中的属性进行相应的赋值。即,通俗点说,属性注入就是给类中的属性赋值。在Java中(非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

来源:简书

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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Python for Everyone

Python for Everyone

Cay S. Horstmann、Rance D. Necaise / John Wiley & Sons / 2013-4-26 / GBP 181.99

Cay Horstmann's" Python for Everyone "provides readers with step-by-step guidance, a feature that is immensely helpful for building confidence and providing an outline for the task at hand. "Problem S......一起来看看 《Python for Everyone》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具