Spring(一)Bean

目录

1.设值注入

1).创建两个类

 2.创建Bean文件

 3.创建context来获取Bean对象

2.构造注入

3.集合对象注入

1)创建对象

2)加入对应的Bean

4.注解自动扫描

4.1创建配置类

1)在配置类中引入xml文件

 2)在xml文件中引入注解配置

 3)自动扫描

4.2获取bean

5.bean的作用域

6.装配方式

7.依赖检查


相比于使用new构造对象,只用bean更加清晰简便,便于修改,尤其当被构造的对象形态有限,或者在多出构造完全相同的对象时。相当于能直接生成一个带有默认值的对象。

1.设值注入

最常用的方法,依赖于setter方法,必须有setter方法。所有Bean类都要实现无参构造方法。

1).创建两个类

一个含普通字段

@NoArgsConstructor
@AllArgsConstructor
@Data
public class Pair {
    private String father;
    private String mother;
}

一个含对象字段

@NoArgsConstructor
@AllArgsConstructor
@Data
public class Person {
    private String name;
    private Pair pair;
}

 2.创建Bean文件

Bean的配置文件,在idea中新建文件,XML Configuration File,Spring Config,产生带有基本配置的Bean文件。普通字段用value赋值,对象字段用ref

<?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="pair" class="com.example.springdemo.domain.Pair">
        <property name="father">
            <value>ming</value>
        </property>
        <property name="mother">
            <value>hong</value>
        </property>
    </bean>

    <bean id="person" class="com.example.springdemo.domain.Person">
        <property name="name">
            <value>peter</value>
        </property>
        <property name="pair">
            <ref bean="pair"/>
        </property>
    </bean>

</beans>

 3.创建context来获取Bean对象

注意路径是斜杠不是点

    @Test
    void contextLoads() {
        ApplicationContext context=new FileSystemXmlApplicationContext("src/main/resources/myContext.xml");
        Person person=(Person) context.getBean("person");
        System.out.println(person);
    }
//Person(name=peter, pair=Pair(father=ming, mother=hong))

2.构造注入

依赖于含参构造方法,要设置autowire,property改为constructor-arg,其他不变

    <bean id="pair" class="com.example.springdemo.domain.Pair" autowire="constructor">
        <constructor-arg name="father">
            <value>ming</value>
        </constructor-arg>
        <constructor-arg name="mother">
            <value>hong</value>
        </constructor-arg>
    </bean>

3.集合对象注入

1)创建对象

@Data
public class MyCollections {
    private String[] array;
    private List<String> list;
    private Map<String,String> map;
}

2)加入对应的Bean

数组和list相同,增加list标签;map增加map标签

    <bean id="myCollections" class="com.example.springdemo.domain.MyCollections">
        <property name="array">
            <list>
                <value>array1</value>
                <value>array2</value>
            </list>
        </property>
        <property name="list">
            <list>
                <value>list1</value>
                <value>list2</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="myKey">
                    <value>myValue</value>
                </entry>
            </map>
        </property>
    </bean>

4.注解自动扫描

4.1创建配置类

@Configuration来代表这相当于一个xml文件

@Bean相当与<bean>

使用@Import导入其他类;使用ImportResource导入其他xml文件

1)在配置类中引入xml文件

importResource时不需要写出完整路径,当文件不在resources下时,需要在Project Structure, Module, Resource Folders 下添加新的路径。

@Configuration
@Import({SecondConfig.class})
@ImportResource(value = "classpath:/myContext.xml")
public class MainConfig {
    @Bean(name = "pair2")
    public Pair pair(){
        return new Pair("f2","m2");
    }
}
@Configuration
public class SecondConfig {
    @Bean(name = "pair3")
    public Pair pair(){
        return new Pair("f3","m3");
    }
}

 2)在xml文件中引入注解配置

之后以xml的方式获取bean

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

<context:annotation-config/>
    <bean class="com.example.springdemo.config.SecondConfig"/>

<beans/>

 3)自动扫描

也可以直接在类上标记@Component, @Controller, @Service, @Repository 等,在xml文件中加入扫描语句后可以自动扫描,用resource同样能实现对另一个bean的引用

<context:component-scan base-package="com.example.springdemo.domain"/>
@Component
@Data
public class Book {

    private String bookName="begin";

    @Resource(name = "person")
    private Person person;
}

 java注释的自动扫描

@Configuration
@ComponentScan(value = {"com.example.springdemo.domain"})
public class MainConfig {

}

4.2获取bean

与xml文件方式类似

    @Test
    void anno(){
        var context=new AnnotationConfigApplicationContext(MainConfig.class);
        Pair pair2=context.getBean("pair2", Pair.class);
        System.out.println(pair2);
        Pair pair3=context.getBean("pair3",Pair.class);
        System.out.println(pair3);
        Pair pair=context.getBean("pair", Pair.class);
        System.out.println(pair);
    }

//Pair(father=f1, mother=m1)
//Pair(father=f3, mother=m3)
//Pair(father=ming, mother=hong)

5.bean的作用域

定义在bean标签内

singleton:默认值,只有唯一的实例

prototype:一个bean有多个实例

request:每个http请求有一个实例

session:每个session一个实例

global session:在一个全局session中,一个bean一个实例

6.装配方式

在bean标签中定义autowire

byType:默认

no:手动装配

byName:根据id名字

constructor:根据构造方法

autodetect:按先Constructor后byType的顺序装配

7.依赖检查

<bean>中dependency-check属性可以确保各字段都已赋值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值