天心堂

spring学习笔记

spring学习笔记

Bean Scopes (Bean的作用域)

Scope Description
singleton (Default) Scopes a single bean definition to a single object instance per Spring IoC container.
prototype Scopes a single bean definition to any number of object instances.
request Scopes 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.
session Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
application Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
websocket Scopes 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="hello" class="com.kuang.pojo.Hello" c:age="18" c:name="青云" scopes="singleton">	
    
  2. 原型模式(每一次从容器中get的时候,都会产生一个新的对象)

    <bean id="hello" class="com.kuang.pojo.Hello" c:age="18" c:name="云" scopes="prototype">
    
  3. 其他模式request,session,application这些是在web 开发中使用

Bean的自动装配

  • 自动装配是Spring满足bean依赖一种方式!

  • Spring会在上下文中自动寻找,并自动给bean装配属性!

    在Spring中有三种装配的方式

    1. 在xml中显示的配置
    2. 在java中显示配置
    3. 隐式的自动装配bean【重要】
  • ByName自动装配
    byName:会自动在容器上下文中查找,和自己对象set方法后面的名称值对应的bean的id!

    <bean id="person" class="cn.kuang.pojo.Person" autowire="byName">
    <property name="name" value="程铭"/></bean>
    
  • ByType自动装配
    会自动在容器上下文中查找,和自己对象属性类型相同的bean!

    <bean class="cn.kuang.pojo.Dog"/>
    <bean class="cn.kuang.pojo.Cat"/>
    <bean id="person" class="cn.kuang.pojo.Person" autowire="byType">
    <property name="name" value="程铭"/>
    </bean>
    

    小结

  • byname的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法名一致!

  • bytype的时候,需要保证所有bean的class唯一,并且这个备案需要和自动注入的属性的类型一致!【同个类型的bean有多个是则不能用】

使用注解实现自动装配

The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML.

要使用注解须知:

  1. 导入约束

  2. 配置注解的支持 开启注解的支持context:annotation-config/ 【重要】

    • @Autowired可以配置在set方法上

    • 在属性上配置@Autowired注解后可以连set方法都不需要,前提是自动装配的属性在这个IoC容器中,并且符合ByName

   <?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
           https://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/>
   </beans>

@Autowired

  • @Autowired是按类型自动转配的,不支持id匹配。
  • 需要导入 spring-aop的包!

测试:

1、将User类中的set方法去掉,使用@Autowired注解

public class User {   
	@Autowired   
	private Cat cat;   
	@Autowired   
	private Dog dog;   
	private String str;   
public Cat getCat() {       
	return cat;  
}   
public Dog getDog() {       
	return dog;  
}   
public String getStr() {       
	return str;  
}
}

2、此时配置文件内容

<context:annotation-config/>
<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="user" class="com.kuang.pojo.User"/>

3、测试,成功输出结果!

【小狂神科普时间】

@Autowired(required=false) 说明:false,对象可以为null;true,对象必须存对象,不能为null。

//如果允许对象为null,设置required = false,默认为true
@Autowired(required = false)
private Cat cat;

@Qualifier

  • @Autowired是根据类型自动装配的,加上@Qualifier则可以根据byName的方式自动装配
  • @Qualifier不能单独使用。

测试实验步骤:

1、配置文件修改内容,保证类型存在对象。且名字不为类的默认名字!

<bean id="dog1" class="com.kuang.pojo.Dog"/>
<bean id="dog2" class="com.kuang.pojo.Dog"/>
<bean id="cat1" class="com.kuang.pojo.Cat"/>
<bean id="cat2" class="com.kuang.pojo.Cat"/>

2、没有加Qualifier测试,直接报错

3、在属性上添加Qualifier注解

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

测试,成功输出!

@Resource

  • @Resource如有指定的name属性,先按该属性进行byName方式查找装配;
  • 其次再进行默认的byName方式进行装配;
  • 如果以上都不成功,则按byType的方式自动装配。
  • 都不成功,则报异常。

实体类:

public class User {   //如果允许对象为null,设置required = false,默认为true 
@Resource(name = "cat2")   
private Cat cat;   
@Resource   
private Dog dog;   
private String str;}

beans.xml

<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="cat1" class="com.kuang.pojo.Cat"/>
<bean id="cat2" class="com.kuang.pojo.Cat"/>
<bean id="user" class="com.kuang.pojo.User"/>

测试:结果OK

配置文件2:beans.xml , 删掉cat2

<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="cat1" class="com.kuang.pojo.Cat"/>

实体类上只保留注解

@Resource
private Cat cat;
@Resource
private Dog dog;

结果:OK

结论:先进行byName查找,失败;再进行byType查找,成功。

小结

@Autowired与@Resource异同:

1、@Autowired与@Resource都可以用来装配bean。都可以写在字段上,或写在setter方法上。

2、@Autowired默认按类型装配(属于spring规范),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用

3、@Resource(属于J2EE复返),默认按照名称进行装配,名称可以通过name属性进行指定。如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。

它们的作用相同都是用注解方式注入对象,但执行顺序不同。@Autowired先byType,@Resource先byName。

XML与注解比较

  • XML可以适用任何场景 ,结构清晰,维护方便
  • 注解不是自己提供的类使用不了,开发简单方便

xml与注解整合开发 :推荐最佳实践

  • xml管理Bean
  • 注解完成属性注入
  • 使用过程中, 可以不用扫描,扫描是为了类上的注解
<context:annotation-config/>  

作用:

  • 进行注解驱动注册,从而使注解生效
  • 用于激活那些已经在spring容器里注册过的bean上面的注解,也就是显示的向Spring注册
  • 如果不扫描包,就需要手动配置bean
  • 如果不加注解驱动,则注入的值为null!

posted on 2020-07-09 19:44  天心堂  阅读(100)  评论(0编辑  收藏  举报

导航