0
点赞
收藏
分享

微信扫一扫

spring中di注入

诗远 2022-04-05 阅读 21
javaspring

DI注入

public class Address {
    private String adress;

    @Override
    public String toString() {
        return "Address{" +
                "adress='" + adress + '\'' +
                '}';
    }

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }
}
public class Student {

    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String house;
    private Properties info;
    }
<?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 name="adress" class="com.liu.pojo.Address">
        <property name="adress" value="河南"/>
    </bean>

    <bean name="student" class="com.liu.pojo.Student">
        <!--普通注入-->
        <property name="name" value="张三" />
        <!--bean注入-->
        <property name="address" ref="adress"/>
        <!--数组注入-->
        <property name="books">
            <array>
                <value>红与黑</value>
                <value>Java</value>
                <value>Python</value>
                <value>数据结构</value>
            </array>
        </property>
        <!--List注入-->
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>吃好吃的</value>
                <value>打游戏</value>
                <value>女孩</value>
            </list>
        </property>
        <!--Map注入-->
        <property name="card">
            <map>
                <entry key="会员卡" value="1001"/>
                <entry key="学生卡" value="1001"/>
                <entry key="超市卡" value="1001"/>
            </map>
        </property>
        <!--set注入-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>原神</value>
                <value>永劫无间</value>
            </set>
        </property>
        <!--null注入-->
        <property name="house">
            <null />
        </property>
        <!--配置注入-->
        <property name="info">
            <props>
                <prop key="driver">driver</prop>
                <prop key="url">url</prop>
            </props>
        </property>
    </bean>



</beans>

测试

public class MyTest {
   @Test
    public void test1(){
       ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
       Student student = context.getBean("student", Student.class);
      System.out.println(student.toString());
   }
}

总结

除了基本类型和String外,其余引用类型赋值需要用到其特有的标签进行传值

CP命名空间

public class User {
    private String name;
    private int age;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public User() {
    }

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

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean id="user" class="com.liu.pojo.User" p:age="18" p:name="张三"/>
        <bean id="user2" class="com.liu.pojo.User" c:age="20" c:name="李四"/>
</beans>
@Test
public void test2(){
    ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
    User user1 = context.getBean("user", User.class);
    User user2 = context.getBean("user2", User.class);
    System.out.println(user1);
    System.out.println(user2);
    System.out.println(user1==user2);
}

总结

c命名空间需要无参构造,p命名空间需要有参构造

同一ID下生成的对象是同一个反之不同

举报

相关推荐

0 条评论