Java教程

Spring配置及依赖注入

本文主要是介绍Spring配置及依赖注入,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

入门

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.6</version>
</dependency>
  • 实体类
package com.pojo;

public class Hello {
    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;
    }
}
  • 配置beans.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="hello" class="com.pojo.Hello">
        <property name="name" value="张三"></property>
        <property name="age" value="18"></property>
    </bean>
</beans>
  • 测试
@Test
public void test1() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    Hello hello = (Hello) context.getBean("hello");
    System.out.println(hello);
}

Set方式注入

  • Student,Address
public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbies;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

    public String getName() {
        return name;
    }

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

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbies=" + hobbies +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}
  • beans.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="address" class="com.pojo.Address"/>
    <bean id="student" class="com.pojo.Student">

        <property name="name" value="张三"/>

        <property name="address" ref="address"/>

        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>水浒传</value>
            </array>
        </property>

        <property name="hobbies">
            <list>
                <value>唱歌</value>
                <value>编程</value>
            </list>
        </property>

        <property name="card">
            <map>
                <entry key="身份证" value="123"/>
                <entry key="大学" value="哔哩哔哩"/>
            </map>
        </property>

        <property name="games">
            <set>
                <value>lol</value>
                <value>王者</value>
            </set>
        </property>

        <property name="wife">
            <null>/</null>
        </property>

        <property name="info">
            <props>
                <prop key="学号">123</prop>
                <prop key="性别">男</prop>
            </props>
        </property>
    </bean>

</beans>

拓展方式注入

  • User.java
package com.pojo;

public class User {
    private String name;
    private int age;

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

    public User() {
    }

    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;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
  • userbeans.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"
       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.pojo.User" p:name="张三" p:age="18"></bean>

    <bean id="users" class="com.pojo.User" c:name="里斯" c:age="19"></bean>
</beans>
  • 注意点;p命名和c命名空间不能直接使用,需要导入xml约束!
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"

bean的作用域scope

<bean id="users" class="com.pojo.User" c:name="里斯" c:age="19" scope="prototype"></bean>
  • 单例模式singleton
  • 原型模式prototype
  • 其余的request、session、application、这些个只能在web开发中使用到!
这篇关于Spring配置及依赖注入的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!