spring--bean的装配方式

1.在xml中配置

实体类

 

public class Student implements Serializable {
    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;

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">

    <import resource="classpath:userbean.xml"/>
<!--di依赖注入-->
    <bean id="address" class="com.kuang.pojo.Address">
        <property name="address" value="西安"></property>
     </bean>
    <bean id="student" class="com.kuang.pojo.Student">
<!--        1.普通值,使用value注入即可-->
        <property name="name" value="小何"></property>
<!--        2.引用类型,使用bean注入,ref-->
        <property name="address" ref="address"></property>
<!--        3.数组注入,-->
        <property name="books">
            <array>
                <value>水浒传</value>
                <value>三国演义</value>
                <value>红楼梦</value>
            </array>
        </property>
<!--        4.list-->
        <property name="hobbys">
            <list>
                <value>学习</value>
                <value>玩游戏</value>
            </list>
        </property>
<!--        5.map-->
        <property name="card">
            <map>
                <entry key="身份证" value="132654"></entry>
                <entry key="银行卡" value="0213231"></entry>
            </map>
        </property>
<!--        6.set-->
        <property name="games">
            <set>
                <value>lol</value>
                <value>bob</value>
            </set>
        </property>
<!--        7.null-->
        <property name="wife">
            <null></null>
        </property>
<!--        8.Properties-->
        <property name="info">
            <props>
                <prop key="学号">123</prop>
                <prop key="姓名">134545</prop>
            </props>
        </property>
    </bean>
</beans>

 

 

 

2.在java中配置

 实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component//表明这个类被spring托管
public class User {
    @Value("小马")
    private String name;
}

 

配置类

package com.kuang.config;

import com.kuang.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

/**
 * @author Administrator
 * @description: TODO
 * @date 2021/11/25 16:19
 */

//@Component//表示这个类被spring托管,注册到容器中
@Configuration//和component作用一样
@ComponentScan("com.kuang.pojo")//包扫描
@Import(JConfig.class)//导入另外一个配置文件
public class JavaConfig {

    /**
     * 注册一个bean,相当于我们的bean标签
     * 方法名:相当于bean的id属性
     * 返回值:相当于bean的class属性
     * @return
     */
    @Bean
    public User getUser(){
        return new User();//就是返回要注入bean的对象
    }

}

测试类

@Test
    public void tes(){
        ApplicationContext context = new
                AnnotationConfigApplicationContext(JavaConfig.class);
        //这个bean就是方法名
        User user = (User) context.getBean("getUser");
        System.out.println(user);
    }

 

3.隐式自动装配bean

byName

 

 

    <!--
        byName:会自动在容器上下文查找,和自己对象set方法后面相似的bean的id
    -->
    <bean id="people" class="com.kuang.pojo.People" autowire="byName">
        <property name="name" value="小李"></property>
    </bean>

 

 

 

byType

 

 

   <!--
        byType:会自动在容器上下文查找,和自己对象属性类型相同的bean
    -->
    <bean id="people" class="com.kuang.pojo.People" autowire="byType">
        <property name="name" value="小李"></property>
    </bean>

 

 

 

使用注解装配bean,@Autowired(byType)

1.导入约束:context

 

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

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd

 

 完整版

 

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:annotation-config/>

</beans>

 

 

2.配合注解的支持

 

<context:annotation-config/>

 

3.在实体类属性上面@Autowired使用即可,也可以在set方法上使用(spring中的注解)

 

    private String name;
    @Autowired
    private Dog dog;
    @Autowired
    private Cat cat;

 

使用技巧:属性可以为空的注解

方法一:required的值为false,那么这个属性可以为空

@Autowired(required = false)

方法二:使用@Nullable这个注解,属性也可以为空

    @Nullable

装配指定bean名字的类,注意Qualifier不能单独使用

    @Autowired()
    @Qualifier(value = "dog2")
    private Dog dog;

4.@resource(先找名字相同的,找不到,再找类型相同的)

    @Resource()
    private String name;

也可以找指定名字的bean的id

    @Resource(name = "dog")
    private String name;

 

posted @ 2021-11-25 14:38  江南0o0  阅读(52)  评论(0编辑  收藏  举报