spring注解自动装配
发布日期:2021-05-07 09:14:17 浏览次数:24 分类:原创文章

本文共 3709 字,大约阅读时间需要 12 分钟。

注解自动装配

  1. 创建bean对象(具体实践在servlet中)

    package com.atguigu.dao;import org.springframework.stereotype.Repository;@Repositorypublic class BookDao {     	 public void sava(){     		 System.out.println("图书已经保存");	 }	}
    package com.atguigu.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.atguigu.dao.BookDao;@Servicepublic class BookService {     	@Autowired	private BookDao bookDao;	public void savaBook() {     		System.out.println("BookService正在调用dao保存图书");		bookDao.sava();	}}
    package com.atguigu.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.atguigu.dao.BookDao;@Servicepublic class BookServiceExt extends BookService{     		@Autowired	private BookDao bookDao;	@Override	public void savaBook() {     		System.out.println("BookServiceExt调用bookDao进行保存");		bookDao.sava();	}	}
    package com.atguigu.servlet;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Controller;import com.atguigu.service.BookService;@Controllerpublic class BookServlet {     	/**	 * 属性的自动赋值	 * @Autowired:Spring会自动的为这个属性赋值,它会去容器中找到这个属性对应的组件	 * 		(1)先按照类型去容器中找到组件	 * 			(1)找到一个直接复制	 * 			(2)没有找到则抛异常	 * 			(3)找到多个时再按照变量名(bookServiceExt)作为id继续匹配	 * 				(1)匹配上了将装配	 * 				(2)没有匹配上则报异常	 * 			(3)或直接去找@Qualifier指定的id名找bean,如果找不到则报异常	 * 	 * @Autowired 自动装配的谁能够在默认是一定装配上的,没有找到则报错。	 * @Autowired(required=false) 自动装配的谁能够在默认是一定装配上的,没有找到就装配null	 */	@Qualifier("bookServiceExt")	//指定一个名作为id,让spring别拿变量名去匹配bean	@Autowired(required=false)	private BookService bookServiceExt2;		public void doGet() {     		System.out.println("BookServlet调用BookService保存图书");		bookServiceExt2.savaBook();	}}
  2. 开启组件扫描

    <?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 http://www.springframework.org/schema/context/spring-context-4.0.xsd">	 	 <!-- 开启组件扫描	 base-package:指定扫描的基础包,把基础包及它下面的包的所有类,自动扫描进ioc容器中 -->	<context:component-scan base-package="com.atguigu"></context:component-scan></beans>
  3. 测试

    package com.atguigu.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.atguigu.dao.BookDao;import com.atguigu.servlet.BookServlet;public class AnnotationTest {     	ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");		@Test	public void annotationAutowired(){     		BookServlet bookServlet = context.getBean("bookServlet",BookServlet.class);		bookServlet.doGet();        /*        BookServlet调用BookService保存图书        BookServiceExt调用bookDao进行保存        图书已经保存        */	}	}

    拓展

    1. @Autowired使用在方法上

      package com.atguigu.servlet;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import com.atguigu.dao.BookDao;import com.atguigu.service.BookService;@Controllerpublic class BookServlet {       	/**	 * 方法上有@Autowired:	 * 1、这个方法会在bean创建的时候自动运行	 * 2、这方法上的每一个参数都会自动注入值	 * 	 * @Qualifier 指定一个名字作为id,spring将不会拿变量名去寻找bean	 */	@Autowired	public void test(BookDao bookDao,@Qualifier("bookService")BookService book){       		System.out.println("spring运行了这个方法"+bookDao+"、"+book);        //启动容器后:spring运行了这个方法com.atguigu.dao.BookDao@4e41089d、com.atguigu.service.BookService@32a068d1	}	}
上一篇:@Resource与@Autowired的区别
下一篇:context:include-filter与exclude-filte控制扫描组件

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2025年04月11日 04时41分23秒