spring 属性注入

作者: DouDouZH | 来源:发表于2018-05-25 23:21 被阅读7次
    1、创建对象时,向类属性里面设置值
    2、java属性注入的三种方式
    • set方法


      image.png
    • 有参构造


      image.png
    • 接口注入


      image.png
    3、在spring中只支持两种方式
    (1)set方法注入

    PropertyUser.java

    package work.zhangdoudou.Property;
    
    public class PropertyUser {
        private String username;
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }   
    }
    

    applicationContext.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-3.0.xsd">
        <!-- set方法注入属性 -->
        <bean id="propertyUser" class="work.zhangdoudou.Property.PropertyUser">
            <!-- set方法注入属性 -->
            <property name="username" value="lisi"></property>
        </bean>
    </beans>
    

    测试类TestPropertyUser.java

    package work.zhangdoudou.test;
    
    import static org.junit.Assert.*;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import work.zhangdoudou.Property.PropertyUser;
    
    public class TestPropertyUser {
    
        @Test
        public void test() {
            //1加载配置文件,根据创建对象
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
            //2得到创建的对象
            PropertyUser propertyUser=(PropertyUser) context.getBean("propertyUser");
            System.out.println(propertyUser.getUsername());
        }
    
    }
    

    运行结果


    image.png
    (2)有参数构造注入

    PropertyUser1.java

    package work.zhangdoudou.Property;
    
    public class PropertyUser1 {
        private String username;
    
        public PropertyUser1(String username) {
            this.username = username;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }   
    }
    

    applicationContext.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-3.0.xsd"> 
        <!-- 有参数构造注入属性 -->
        <bean id="propertyUser1" class="work.zhangdoudou.Property.PropertyUser1">
            <!-- 有参数构造注入 -->
            <constructor-arg name="username" value="zhangsan"></constructor-arg>
        </bean>
    </beans>
    

    测试类TestPropertyUser1.java

    package work.zhangdoudou.test;
    
    import static org.junit.Assert.*;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import work.zhangdoudou.Property.PropertyUser1;
    
    public class TestPropertyUser1 {
    
        @Test
        public void test() {
            //1加载配置文件,根据创建对象
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
            //2得到创建的对象
            PropertyUser1 propertyUser1=(PropertyUser1) context.getBean("propertyUser1");
            System.out.println(propertyUser1.getUsername());
        }
    
    }
    

    运行结果


    image.png

    相关文章

      网友评论

        本文标题:spring 属性注入

        本文链接:https://www.haomeiwen.com/subject/lyhpjftx.html