Spring-02-配置,注入,作用域

165 阅读3分钟

单例模式

在加载Beans.xml文件时会自动创建所有bean的实例

在Spring中,getBean()获取的是同一个实例对象

配置

**alias **别名

<alias id="" alias="">

name 也是别名,更高级,可以同时取多个

<bean id="uers" class="" name="user,u1,u2;u3"></bean>

为id为users的实体类配置别名为user,u1,u2,u3 分隔符可以用空格,逗号,分号等

import 导入配置文件,将多个配置文件合并

<import resource="bean.xml"></import>

注入

普通注入

<bean id="testHello" class="pojo.Test">
        <property name="str" value="d"/>
</bean>

set注入

<bean id="Test2" class="pojo.Test2">
    <!--bean注入-->
    <property name="test" ref="testHello"/>
    
    
    <!--数组-->
    <property name="books">
        <array>
            <value>红楼梦</value>
            <value>西游记</value>
            <value>水浒传</value>
        </array>
    </property>
    
    
    <!--set-->
    <property name="games">
        <set>
            <value>coc</value>
            <value>lol</value>
        </set>
    </property>
    
    
    <!--list-->
    <property name="hobby">
        <list>
            <value>swing</value>
            <value>run</value>
            <value>basketball</value>
        </list>
    </property>
    
    
    <!--map-->
    <property name="cards">
        <map>
            <entry key="身份证" value="123456"></entry>
            <entry key="银行卡" value="13456"></entry>
        </map>
    </property>
    
    
    <!--空值设置-->
    <property name="name">
        <null/>
    </property>
    
    
    <!--properties注入-->
    <property name="pop">
        <props>
            <prop key="键"></prop>
            <prop key="学号">134656</prop>
        </props>
    </property>
    
    
</bean>

拓展注入

p注入:直接注入

 <bean id="testHello" class="pojo.Test" p:str="d">
</bean>

c注入:通过构造器注入

<bean id="testHello" class="pojo.Test" c:str="c">
</bean>

作用域

scope

单例模式(默认)

只有一个实例

<bean id="testHello" class="pojo.Test" c:str="c" p:str="d" scope="singleton">

原型模式

(重新创建实例对象)

<bean id="testHello" class="pojo.Test" c:str="c" p:str="d" scope="prototype">

Bean自动装配

public class people {
    private Dog dog;
    private Cat cat;

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "people{" +
                "dog=" + dog +
                ", cat=" + cat +
                '}';
    }

    @Test
    public void test(){
        ApplicationContext context= new ClassPathXmlApplicationContext("beans.xml");
        people p=context.getBean("people",people.class);
        p.dog.shout();
        p.cat.shout();
    }


}

autowrite 需要定义对应对象的set方法

<bean id="dog" class="Dog"></bean>
<bean id="cat" class="Cat"></bean>
<bean id="people" class="people" autowire="byType"></bean>
autowrite=""
		   byName 通过id进行匹配,通过set方法注入,要保证该bean所需和属性的set值一样(需要保证beanid唯一)
		   byType 通过class匹配,通过set注入,要保证该bean所需和属性类型一样(保证bean的class唯一)
		   constructor
		   default
		   no

通过注解自动装配

通过注解 自动@Autowired装配

  1. 添加约束

    <?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:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
    </beans>
    
  2. 配置注解支持

    <context:annotation-config/>
    
  3. 使用

可直接在属性上使用,无需定义set方法

也可在set方法上使用

通过byType方式匹配,如果有多个则通过byName匹配,根据变量名与beanid,可通过@Qualifier(value=“xxx”)辅助

public class people {
    @Autowired
    private Dog dog;
    @Autowired(required = false)
    private Cat cat;
    @Override
    public String toString() {
        return "people{" +
                "dog=" + dog +
                ", cat=" + cat +
                '}';
    }

    @Test
    public void test(){
        ApplicationContext context= new ClassPathXmlApplicationContext("beans.xml");
        people p=context.getBean("people",people.class);
        p.dog.shout();
        p.cat.shout();
    }


}
  1. 补充
@Nullable 标记字段可以为null,即下方构造方法中的name可以为空
public class test{
    public test(@Nullable String name){
        
    }
}
public @interface Autowired {
	boolean required() default true;
    默认为true,即标记对象不允许为null
}
@Autowired(required = false) 标记对象允许为null

@Qualifier(value="xxx")去配置@ Autowired的使用,指定一个唯一的bean对象注入

当有多个id的不同,class为Dog的bean时通过Qualifier来指定唯一的bean

public class people {
    @Autowired
    @Qualifier(value="dog222")
    private Dog dog;


}

@Resource和@Autowired都是做bean的注入时使用

@Resource不需要spring5是由J2EE提供

public class TestServiceImpl {
 	// 下面两种@Resource只要使用一种即可
 @Resource(name= "userDao")
 private UserDao userDao; // 用于字段上
 
 @Resource(name="userDao")
 public void setUserDao(UserDao userDao) { // 用于属性的setter方法上
  this.userDao = userDao;
 }
}

@Resource装配顺序:

①如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常。

②如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常。

③如果指定了type,则从上下文中找到类似匹配的唯一bean进行装配,找不到或是找到多个,都会抛出异常。

④如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配。

@Resource的作用相当于@Autowired,只不过@Autowired按照byType自动注入。