正在加载中...

spring的DI注入和set注入

Spring的DI注入和set注入

注:此观点只是个人理解

前沿:spring就是控制反转,在上一章已经说,这次理解Spring的DI注入和Set注入,因为这样注入会提高很大效率,下章发一篇C标签和P标签,其实他和Jsp中的C标签用法差不多,他更方便和简介了。

 

1.装Spring头部,这是必须的

上一章已经讲过,但是这个是千万不能忘记的。

<?xml version="1.0" encoding="UTF-8"?>
<!--
注:此头部禁止修改,可以去spring官网中文文档进行查找
https://www.docs4dev.com/docs/zh/spring-framework/5.1.3.RELEASE/reference/
-->
<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">
    
    </beans>

 

 

2.写各种方式的实体类

例如:list,map,引入类型和set,还有数组类型

注:记得给set、get、有参、无参和tostring的方法

public class Test2 {
    private String name;
    private String[] pwd;
    private List<String> sex;
    private Map<String,String> age;
    private Set<String> c;
​
    public Test2() {
    }
​
    public Test2(String name, String[] pwd, List<String> sex, Map<String, String> age, Set<String> c) {
        this.name = name;
        this.pwd = pwd;
        this.sex = sex;
        this.age = age;
        this.c = c;
    }
​
    public String getName() {
        return name;
    }
​
    public void setName(String name) {
        this.name = name;
    }
​
    public String[] getPwd() {
        return pwd;
    }
​
    public void setPwd(String[] pwd) {
        this.pwd = pwd;
    }
​
    public List<String> getSex() {
        return sex;
    }
​
    public void setSex(List<String> sex) {
        this.sex = sex;
    }
​
    public Map<String, String> getAge() {
        return age;
    }
​
    public void setAge(Map<String, String> age) {
        this.age = age;
    }
​
    public Set<String> getC() {
        return c;
    }
​
    public void setC(Set<String> c) {
        this.c = c;
    }
​
    @Override
    public String toString() {
        return "Test2{" +
                "name='" + name + '\'' +
                ", pwd=" + Arrays.toString(pwd) +
                ", sex=" + sex +
                ", age=" + age +
                ", c=" + c +
                '}';
    }

 

3.写Spring的xml

注:每个方法写的都差不多,都是可以进行赋值value的值,这里只是做一个简单的赋值。map的话需要用entry。

<?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:如果这样写话必须得把所有的property进行包裹
     -->
    <bean id="ss" class="com.zzy.pojo.Test2">
​
        <property name="name" value="name的字段"></property>
​
        <!-- 这个是String[]的数组,进行array方法注入-->
        <property name="pwd">
            <array>
                <value>吃饭</value>
                <value>睡觉</value>
                <value>打豆豆</value>
            </array>
        </property>
​
        <!-- 这个是list,进行list方法注入-->
        <property name="sex">
            <list>
                <value>姓名</value>
                <value>年龄</value>
                <value>身高</value>
            </list>
        </property>
        <!-- 这个是map,他是键值对的形式,所以进行用map的entry进行方法注入-->
        <property name="age">
            <map>
                <entry key="年龄" value="20"></entry>
                <entry key="身高" value="180"></entry>
            </map>
        </property>
        <!-- 这个是set,进行set的value进行注入-->
        <property name="c">
            <set>
                <value>1</value>
                <value>2</value>
                <value>3</value>
​
            </set>
        </property>
​
    </bean>
</beans>

 

4.写一个test类

 

public static void main(String[] args) {
        //必须要写的,可以写多个xml
       ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
       //进行实体类转化,如果不想转换可以在后面写一个实体类.class例如:
        // Test2 ss = context.getBean("ss", Test2.class);
​
        Test2 test = (Test2) context.getBean("ss");
​
        System.out.println("IOC为:"+test);
    }

 

posted @ 2021-11-23 10:31  笑太苦  阅读(105)  评论(0)    收藏  举报