6.DI依赖注入

1.构造器注入

  前面已经使用过

2.set注入【重点】

  依赖注入:set注入  

    依赖:bean对象的创建依赖于spring容器

    注入:bean对象中的所有属性由spring容器注入

  环境搭建:

    (1)导入依赖

<dependencies>
   <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.24</version>
    </dependency>
</dependencies>

   (2)编写实例

Address.java

public class Address {
    private String address;
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}

Student .java

public class Student {

    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> cards;
    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> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

    public Map<String, String> getCards() {
        return cards;
    }

    public void setCards(Map<String, String> cards) {
        this.cards = cards;
    }

    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) +
                ", hobbys=" + hobbys +
                ", cards=" + cards +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

3.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    
    
</beans>

4. 注入的实现

<bean id="address" class="com.zuo.dao.Address">
    <property name="address" value="左家旁峪村"/>
</bean>
<bean id="studentName" class="com.zuo.dao.Student">
    <!--普通值的注入 value -->
    <property name="name" value="zuozhikun"/>
    <!--bean的注入 ref-->
    <property name="address" ref="address"/>
    <!--数组-->
    <property name="books">
        <array>
            <value>水浒传</value>
            <value>三国演义</value>
            <value>西游记</value>
            <value>红楼梦</value>
        </array>
    </property>
    <!--list-->
    <property name="hobbys">
        <list>
            <value></value>
            <value></value>
            <value>篮球</value>
            <value>rap</value>
        </list>
    </property>
    <!--map-->
    <property name="cards">
        <map>
            <entry key="身份证" value="37032319910200817"/>
            <entry key="姓名" value="左志锟"/>
        </map>
    </property>
    <!--集合-->
    <property name="games">
        <set>
            <value>LOL</value>
            <value>CF</value>
            <value>PUBG</value>
        </set>
    </property>
    <!--null-->
    <property name="wife" value=""/>
    <!--property-->
    <property name="info">
        <props>
            <prop key="性别"></prop>
            <prop key="电话号码">19558986127</prop>
            <prop key="身高">178</prop>
        </props>
    </property>
</bean>

3.拓展方式注入

(1)p命名空间注入

引入配置信息

xmlns:p="http://www.springframework.org/schema/p"

编写bean

<!--p命名空间注入,可以直接注入属性值:property-->
<bean id="user" class="com.zuo.dao.User" p:name="zuo" p:pwd="123456" />

测试

@Test
public void test2(){
    //获取spring容器
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    User user = (User) context.getBean("user");
    System.out.println(user.toString());
}

 官方简介

  

(2)c命名空间注入

引入配置信息

xmlns:c="http://www.springframework.org/schema/c"

编写bean

<!--c命名空间注入,通过构造器注入:constructor-arg-->
<bean id="user" class="com.zuo.dao.User" c:name="song" c:pwd="123456" />

测试

@Test
public void test2(){
    //获取spring容器
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    User user = (User) context.getBean("user");
    System.out.println(user.toString());
}

 官方简介

 

 4.Bean作用域

(1)singleton

  单例模式(spring默认机制,每次从容器中get的对象都是同一个对象)

  显示设置单例模式:

<bean id="user2" class="com.zuo.dao.User" c:name="song" c:pwd="123456" scope="singleton"/>

(2)prototype

  原型模式(每次从容器中get 的对象都为一个新的对象)

  设置原型模式:

<!--c命名空间注入,通过构造器注入:constructor-arg-->
<bean id="user2" class="com.zuo.dao.User" c:name="song" c:pwd="123456" scope="prototype"/>

 

测试:

@Test
public void test3(){

    //获取spring容器
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");

    User user1 = (User) context.getBean("user2");
    User user2 = (User) context.getBean("user2");

    System.out.println(user1==user2);
}

 

(3)其他

  request,session,application,websocket只能在web开发中使用!!

 

posted on   人无远虑必有近忧  阅读(20)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示