Spring講解(三)

依賴注入Bean屬性,使用xml配置

1、構造方法注入

案例代碼演示

public class User {
    
    private String username;
    private String password;
    private Integer age;

    public User() {}
 
    public User(String username, String password, Integer age) {
        this.username = username;
        this.password = password;
        this.age = age;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", 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"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!---->
    <bean id="user" class="com.example.demo.testservice.User">
        <!--相當於調用了有參構造
            public User(String username, String password, Integer age) {...}
        -->
        <constructor-arg name="username" value="zhangsan"/>
        <constructor-arg name="password" value="123456"/>
        <constructor-arg name="age" value="99"/>
    </bean>
</beans>

========================================================================================
測試函數   
public class ServiceTest {

    public static void main(String[] args) {

        ApplicationContext context1 = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) context1.getBean("user");
        System.out.println(user);
    }
}        
執行測試函數得到如下結果:

User{username='zhangsan', password='123456', age=99}

上面的bean.xml還可以通過索引注入參數,其他不變,修改 bean.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.xsd">

    <bean id="user" class="com.example.demo.testservice.User">
        <!--相當於調用了有參構造
            public User(String username, String password, Integer age) {...}
        -->
        <constructor-arg index="0" value="李四" type="java.lang.String"/>
        <constructor-arg index="1" value="112233" type="java.lang.String"/>
        <constructor-arg index="2" value="44" type="java.lang.Integer"/>
    </bean>
</beans>
執行測試函數得到如下結果:

User{username='李四', password='112233', age=44}

2、屬性setter方法注入(有兩種)

上面的 User 和 測試函數 ServiceTest 不變,只需要修改 bean.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.xsd">

    <bean id="user" class="com.example.demo.testservice.User">
        <property name="age" value="11"/>
        <property name="password" value="666"/>
        <property name="username" value="王麻子"/>
    </bean>
</beans>

=========================================================================================

<?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 id="user" class="com.example.demo.testservice.User">
        <property name="age">
            <value>11</value>
        </property>
        <property name="password">
            <value>666</value>
        </property>
        <property name="username">
            <value>王麻子</value>
        </property>
    </bean>
</beans>
上面 兩種配置 bean.xml 都可以,採用第一種配置的比較只管方便。

測試結果都如下:

User{username='王麻子', password='666', age=11}

3、p命名空間注入

上面的 User 和 測試函數 ServiceTest 不變,只需要修改 bean.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"
       xmlns:p ="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user" class="com.example.demo.testservice.User" p:age="111" p:password="888" p:username="秦始皇" />
</beans>
這種方法不常用,有興趣的朋友可以對比下 p 命名空間的 bean.xml 和構造方法注入、setter 方法注入的 bean.xml 區別就是加了一個===》xmlns:p ="http://www.springframework.org/schema/p",就可以使用 p 命名空間了。

運行結果如下:

User{username='秦始皇', password='888', age=111}

4、集合注入

List、Set、Map、Properties
public class Coder {

    private List<String> cars;  // 車

    private Set<String> pats;   // 寵物

    private Map<String,String> information; // 信息

    private Properties mysqlInfo;   // Mysql 連接信息

    public Properties getMysqlInfo() {
        return mysqlInfo;
    }

    public void setMysqlInfo(Properties mysqlInfo) {
        this.mysqlInfo = mysqlInfo;
    }

    public Map<String, String> getInformation() {
        return information;
    }

    public void setInformation(Map<String, String> information) {
        this.information = information;
    }

    public Set<String> getPats() {
        return pats;
    }

    public void setPats(Set<String> pats) {
        this.pats = pats;
    }

    public List<String> getCars() {
        return cars;
    }

    public void setCars(List<String> cars) {
        this.cars = cars;
    }

    @Override
    public String toString() {
        return "Coder{" +
                "cars=" + cars +
                ", pats=" + pats +
                ", information=" + information +
                ", mysqlInfo=" + mysqlInfo +
                '}';
    }
}

=========================================================================================
<?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 id="coder" class="com.example.demo.testservice.Coder">
        <!--List 數據注入-->
        <property name="cars">
            <list>
                <value>ofo</value>
                <value>寶馬</value>
                <value>奔馳</value>
            </list>
        </property>

        <!--Set數據注入-->
        <property name="pats">
            <set>
                <value>豬</value>
                <value>馬</value>
                <value>牛</value>
            </set>
        </property>

        <!--Map 數據注入-->
        <property name="information">
            <map>
                <entry key="name" value="王八蛋"/>
                <entry key="age" value="99"/>
                <entry key="password" value="8888"/>
            </map>
        </property>

        <!--Properties 數據注入-->
        <property name="mysqlInfo">
            <props>
                <prop key="url">mysql:jdbc://localhost:3306/sample</prop>
                <prop key="username">root</prop>
                <prop key="password">root</prop>
            </props>
        </property>
    </bean>
</beans>
=========================================================================================測試函數
public class ServiceTest {

    public static void main(String[] args) {

        ApplicationContext context1 = new ClassPathXmlApplicationContext("beans.xml");
        Coder coder = (Coder) context1.getBean("coder");
        System.out.println("車:"+coder.getCars());
        System.out.println("寵物:"+coder.getPats());
        System.out.println("個人信息:"+coder.getInformation());
        System.out.println("數據庫信息信息:"+coder.getMysqlInfo());
    }
}                    
運行結果如下:

車:[ofo, 寶馬, 奔馳]

寵物:[豬, 馬, 牛]

個人信息:{name=王八蛋, age=99, password=8888}

數據庫信息信息:{password=root, url=mysql:jdbc://localhost:3306/sample, username=root}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章