2020-04-02 spring学习------注解方式装配Bean

本文详细介绍了Spring框架的配置方式,包括XML配置文件的结构和注解配置的使用。探讨了如何通过<context:component-scan>进行包扫描,以及bean的作用域、命名规则和依赖注入的多种方式。

一、配置文件

<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:p="http://www.springframework.org/schema/p">

    <!-- 开启注解支持 -->
    <context:annotation-config/>
    <!--配置包扫描器-->
    <context:component-scan base-package="test.spring.*"/>  

    <!--
    1.bean作用域:scope="singleton"单例 scope="prototype"多例。
    2.bean命名:
        (1)id是唯一的,name较随意。同时指定都会生效。
        (2)命名的字符串中有空格逗号分号,id不会分割,name会分割成多个相当于别名。
        (3)都不指定且实例类都一样时,不带后缀(即默认)是第一个。通过getBean(“com.kkb.service.PersonService#1”)获取第二个。


    3.使用多个配置文件
       (1)方法一:<import resource="classpath:spring-conf.xml"></import>
       (2)方法二:ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans1.xml", "beans2.xml");
    -->
</beans>

`二、代码
1.UserImpl 

package test.spring.Dao.Impl;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import test.spring.Dao.UserDao;@Repository("udao")
public class UserImpl implements UserDao{
    public void getNum(){
        System.out.println(1234);
    }
}


2.AnnotationService 里注入UserDao 

package test.spring.Service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import test.spring.Dao.Impl.UserImpl;
import test.spring.Dao.UserDao;
import test.spring.Domain.StudentBean;

import javax.annotation.Resource;
//@Autowired:位置:1.成员变量上;2.setter方法上。默认:类型注入,若类型相同则根据名称注入。
//@Autowired(required = true):找不到会报错
//@Qualifier("udao")

//@Resource(name = "udao" ,type = UserDao.class):默认byname.
//两个都写会找唯一匹配的,找不到抛异常。
//name:会根据bean的名称(id)找不到抛异常
//type:根据bean的类型匹配,找不到或找到多个都会抛异常
//没有指定name和type:默认nyname,没有则bytype。
@Service("annotationService")
public class AnnotationService {
    private UserDao userImpl;
    public UserDao getUserImpl() {
        return userImpl;
    }
    @Autowired(required = true)
    @Qualifier("udao")
//    @Resource(name = "udao")
    public void setUserImpl(UserDao userImpl) {
        this.userImpl = userImpl;
    }    public void useAnnotation(){
        userImpl.getNum();
    }
}


3. UserImpl 

package test.spring.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import test.spring.Service.Impl.AnnotationService;
import javax.annotation.Resource;

public class AnnotationServiceController {
/*自动装载并没有把配置文件中配置的bean转载到内存中。
    Spring的自动装载是在applicationContext=new ClassPathXmlApplicationContext("");的时候自动装载。
    在实例化的时候,会去根据<context:component-scan base-package="test.spring.*"></context:component-scan>中配置的扫描范围,扫描所有的有@Autowired注解的bean,进行装载,转载完以后,所有地方都可以使用这里bean了。

简单来说,就是,注解省去了在xml中配置。要想使用还是要从applicationContext里取出来再用
*/
    public static void main(String [] args){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml","spring-conf.xml");
        AnnotationService annotationService = (AnnotationService)applicationContext.getBean("annotationService");
        annotationService.useAnnotation();
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值