day14_spring文档学习

1.bean的自动装配
  • 自动装配是spring满足bean依赖的一种方式

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

  • spring中有三种装配的方式

    • 在xml中显示的配置,上一节day13中所有的配置方式都是显示装配方式
    • 在Java中显示配置(后续补充)
    • 隐式的自动装配bean【重要
    <bean id="cat" class="com.yang.pojo.Cat"/>
    <bean id="dog" class="com.yang.pojo.Dog"/>
    
1.1 byName自动装配
<bean id="pet" class="com.yang.pojo.Pet" autowire="byName"/>
1.2 byType自动装配
<bean id="pet" class="com.yang.pojo.Pet" autowire="byType"/>

byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid;
byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean;可以省略id

<bean id="pet" class="com.yang.pojo.Pet">
    <property name="cat" ref="cat"/>
    <property name="dog" ref="dog"/>
 </bean>
等同于上面的方式
1.3使用注解实现自动装配

​ 1.3.1导入约束

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

​ 1.3.2配置注解的支持

<!--    开启context注解的支持,必须要有!!-->
    <context:annotation-config/>

​ 1.3.3属性的配置

<bean id="cat" class="com.yang.pojo.Cat"/>
<bean id="cat1" class="com.yang.pojo.Cat"/>

​ 1.@Autowired直接用在属性上,也可以在set方法上使用;

​ 使用Autowired可以不用编写set方法,前提是这个自动装配的属性在IOC容器中存在,且符合名字byname

public class Pet {
    @Autowired
    @Qualifier("cat1")
    private Cat cat;
    @Autowired(required = false)
    private Dog dog;
    }
//省略getter setter、、、
* 如果查询结果刚好为一个,就将该bean装配给@Autowired指定的数据,如果查询结果不止一个,@Autowired会根据名称来查找,如果上述结果为空,就会抛出异常,解决方式就是设置required=false。如上* 如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解@Autowired完成时,可以使用@Qualifier("xxx")去配合@Autowired使用,指定一个唯一的bean对象注入!

​ 2.@Resource

public class Pet {    @Resource(name="cat1")    private Cat cat;    @Resource    private Dog dog;    }
  • 是Java中的注解,等同于@Autowired,也可以通过name=”xxx“实现bean的注入

@Autowired和@Resource的异同

​ 都是用来自动装配的,Autowired先按照byType注入,对象必须存在

​ Resource通过byName方式实现,找不到则通过byType

2.衍生注解

@Component

//Component :组件 等价于 <bean id="user" class="com.yang.pojo.User"/>@Componentpublic class User {     //给name赋值 等价于 <property name="name" value="kuanghsen"/>    public String name;    @Value("kuanghsen")    public void setName(String name) {        this.name = name;    }}

Component有几个衍生注解,在web开发中会按照mvc三层架构分层!

  • dao层【@Repository】
  • service层【@Service】
  • controller层【@Controller】

这四个注解功能相同,都是表示将某个类注册到spring中,装配bean!只是用到不同的层中。

3.使用Java的方式配置spring

JavaConfig是spring4的新功能,必须导入aop包

Java配置类

package com.yang.config;import com.yang.pojo.User;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Import;//这个注解也会被spring容器托管,注册到容器中,因为他本身就是一个@Component--可进源码中查看//有这个注解就代表这是一个配置类,等同于applicationContext.xml@Configuration@Import(MyConfig2.class) //等同于配置文件中的<import resource="beans.xml"/>public class MyConfig {    /*注册一个bean,相当于之前配置文件里写的bean标签    * 这个方法的名字,就相当于bean标签中的id属性    * 方法的返回值,就想到于bean标签中的class属性    * */    @Bean    public User user(){        return new User(); //就是返回的要注入的bean的对象    }}

实体类

package com.yang.pojo;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import org.springframework.stereotype.Controller;import org.springframework.stereotype.Repository;import org.springframework.stereotype.Service;//Component :组件 等价于 <bean id="user" class="com.yang.pojo.User"/>//意思就是这个类被spring接管了,注册到了容器中@Componentpublic class User {     //给name赋值 等价于 <property name="name" value="kuanghsen"/>    public String name;    @Value("kuanghsen")    public void setName(String name) {        this.name = name;    }    public String getName() {        return name;    }}

测试方式

@Testpublic void annotation(){    //如果使用了Java类方式配置。就只能通过AnnotationConfig上下文来回去容器,通过配置类的class对象加载!    ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);    User user = context.getBean("user", User.class);    System.out.println(user.getName());}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值