0
点赞
收藏
分享

微信扫一扫

Spring依赖注入

黎轩的闲暇时光 2022-02-18 阅读 25

依赖注入

Set方式注入

  • 依赖:bean对象的创建依赖于容器
  • 注入:bean对象中的所有属性由容器来注入

【环境搭建】

  1. 复杂类型

    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 + '\'' +
                    '}';
        }
    }
    
  2. 真实测试对象

    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 wife;
        private Properties info;
    
  3. 完善注入信息(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.lzj.entity.Address">
        	<property name="address" value="武汉"></property>
        </bean>
        <bean id="student" class="com.lzj.entity.Student">
    <!--        第一种,普通值注入,value-->
            <property name="name" value="lzj"></property>
    <!--        第二种,Bean注入,ref-->
            <property name="address" ref="address"></property>
    <!--        数组-->
            <property name="books">
                <array>
                    <value>红楼梦</value>
                    <value>西游记</value>
                    <value>水浒传</value>
                    <value>三国演义</value>
                </array>
            </property>
    <!--        List-->
            <property name="hobbies">
                <list>
                    <value>听歌</value>
                    <value>敲代码</value>
                    <value>看电影</value>
                </list>
            </property>
    <!--       Map -->
            <property name="card">
                <map>
                    <entry key="身份证" value="2132132465456"></entry>
                    <entry key="银行卡" value="546565432134"></entry>
                </map>
            </property>
    <!--        Set-->
            <property name="games">
                <set>
                    <value>LOL</value>
                    <value>COC</value>
                    <value>BOB</value>
                </set>
            </property>
    <!--        null-->
            <property name="wife">
                <null></null>
            </property>
    <!--        properties-->
            <property name="info">
                <props>
                    <prop key="学号">20191025302</prop>
                    <prop key="性别"></prop>
                    <prop key="姓名">lzj</prop>
                </props>
            </property>
        </bean>
    </beans>
    
  4. 测试

    public class MyTest {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            Student student = (Student) context.getBean("student");
            System.out.println(student);
        }
    }
    

p命名空间和c命名空间注入

官方文档位置:

在这里插入图片描述

使用:

实体类:

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

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

    public User() {
    }
}

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">
<!--    p命名空间注入,可以直接注入属性的值:property-->
    <bean id="user" class="com.lzj.entity.User" p:name="lzj" p:age="20"></bean>
<!--    c命名空间注入,通过构造器注入:construct-args-->
    <bean id="user2" class="com.lzj.entity.User" c:name="lzj" c:age="20"></bean>
</beans>

测试:

@Test
public void test2(){
    ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
    User user = context.getBean("user", User.class);
    System.out.println(user);

}

bean的作用域

ScopeDescription
singleton(Default) Scopes a single bean definition to a single object instance for each Spring IoC container.
prototypeScopes a single bean definition to any number of object instances.
requestScopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
sessionScopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
applicationScopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
websocketScopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.
  1. 单例模式(Spring默认机制)

    <bean id="user2" class="com.lzj.entity.User" c:name="lzj" c:age="20" scope="singleton"></bean>
    
  2. 原型模式:每次从容器中get的时候,都会产生一个新对象

    <bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
    
  3. 其余的request、session、application、这些个只能在web开发中使用

的时候,都会产生一个新对象

<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
  1. 其余的request、session、application、这些个只能在web开发中使用
举报

相关推荐

0 条评论