spring--bean的装配方式


1.在xml中配置

实体类

public class Student implements Serializable {
    private String name;
    private Address address;
    private String[] books;
    private List hobbys;
    private Map card;
    private Set games;
    private String wife;
    private Properties info;

xml

<?xml version="1.0" encoding="UTF-8"?>
"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">

    "classpath:userbean.xml"/>

    "address" class="com.kuang.pojo.Address">
        "address" value="西安">
     
    "student" class="com.kuang.pojo.Student">

        "name" value="小何">

        "address" ref="address">

        "books">
            
                水浒传
                三国演义
                红楼梦
            
        

        "hobbys">
            
                学习
                玩游戏
            
        

        "card">
            
                "身份证" value="132654">
                "银行卡" value="0213231">
            
        

        "games">
            <set>
                lol
                bob
            set>
        

        "wife">
            <null>null>
        

        "info">
            
                "学号">123
                "姓名">134545
            
        
    

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

    
    "people" class="com.kuang.pojo.People" autowire="byName">
        "name" value="小李">
    

byType

    
    "people" class="com.kuang.pojo.People" autowire="byType">
        "name" value="小李">
    

使用注解装配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"?>
"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">
    

2.配合注解的支持

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;