您当前的位置: 首页 >  spring

小志的博客

暂无认证

  • 0浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

spring自动装配——@Autowired&@Qualifier&@Autowired

小志的博客 发布时间:2019-11-20 12:09:43 ,浏览量:0

一、spring自动装配——@Autowired(默认优先按照类型去容器中找对应的组件)示例

1、创建一个maven项目(创建maven项目过程省略),pom.xml文件引入如下依赖:


	org.springframework
	spring-context
	5.2.0.RELEASE


	junit
	junit
	4.12
	test

2、创建dao层、service层、controller层,代码如下:

1)dao层代码:

package com.rf.dao;

import org.springframework.stereotype.Repository;

@Repository
public class BookDao {

}

2)service层代码:

package com.rf.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rf.dao.BookDao;

@Service
public class BookService {
	
	@Autowired
	BookDao bookDao;
	
	public void print(){
		System.out.println(bookDao);
	}

	@Override
	public String toString() {
		return "BookService [bookDao=" + bookDao + "]";
	}
}

3)controller层代码:

package com.rf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.rf.service.BookService;

@Controller
public class BookController {
	
	@Autowired
	BookService bookService;
}

3、编写配置类,代码如下:

package com.rf.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
* 自动装配:
*  Spring利用依赖注入(DI),完成对IOC容器中中各个组件的依赖关系赋值;
*    1)、默认优先按照类型去容器中找对应的组件:applicationContext.getBean(BookDao.class);找到就赋值
*/
@Configuration
//通过扫描的方式注册一个bookDao组件
@ComponentScan({"com.rf.controller","com.rf.service","com.rf.dao"})
public class MyConfigOfAutowired {
	
}

4、编写测试类,代码如下:

package com.rf.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.rf.config.MyConfigOfAutowired;
import com.rf.dao.BookDao;
import com.rf.service.BookService;

public class Test2 {
	
	@org.junit.Test	
	public void test(){
		
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfigOfAutowired.class);
		System.out.println("ioc容器创建完成了。。。。");
		
		BookService bookService = ctx.getBean(BookService.class);
		System.out.println(bookService);
		BookDao bookDao = ctx.getBean(BookDao.class);
		System.out.println(bookDao);
	}
}

5、启动测试类,运行效果如下: 在这里插入图片描述

二、spring自动装配——@Autowired(如果找到多个相同类型的组件,BookService 层的组件id为bookDao,@Bean中指定BookService 层的组件id为bookDao2)示例1

1、创建一个maven项目(创建maven项目过程省略),pom.xml文件引入如下依赖:


	org.springframework
	spring-context
	5.2.0.RELEASE


	junit
	junit
	4.12
	test

2、创建dao层、service层、controller层,代码如下:

1)dao层代码:

package com.rf.dao;

import org.springframework.stereotype.Repository;

@Repository
public class BookDao {

	private String lable="1";
	
	public String getLable() {
		return lable;
	}

	public void setLable(String lable) {
		this.lable = lable;
	}

	@Override
	public String toString() {
		return "BookDao [lable=" + lable + "]";
	}
}

2)service层代码:

package com.rf.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rf.dao.BookDao;

@Service
public class BookService {
	
	@Autowired
	BookDao bookDao;
	
	public void print(){
		System.out.println(bookDao);
	}

	@Override
	public String toString() {
		return "BookService [bookDao=" + bookDao + "]";
	}
}

3)controller层代码:

package com.rf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.rf.service.BookService;

@Controller
public class BookController {
	
	@Autowired
	BookService bookService;
}

3、编写配置类,代码如下:

package com.rf.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
* 自动装配:
*  Spring利用依赖注入(DI),完成对IOC容器中中各个组件的依赖关系赋值;
*    1)、默认优先按照类型去容器中找对应的组件:applicationContext.getBean(BookDao.class);找到就赋值
*/
@Configuration
//通过扫描的方式注册一个bookDao组件
@ComponentScan({"com.rf.controller","com.rf.service","com.rf.dao"})
public class MyConfigOfAutowired {
	
	//通过@Bean的方式在注册一个bookDao组件
	@Bean("bookDao2")
	public BookDao bookDao(){
		BookDao bookDao = new BookDao();
		bookDao.setLable("2");
		return bookDao;
	}
}

4、编写测试类,代码如下:

package com.rf.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.rf.config.MyConfigOfAutowired;
import com.rf.dao.BookDao;
import com.rf.service.BookService;

public class Test2 {
	
	@org.junit.Test	
	public void test(){
		
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfigOfAutowired.class);
		System.out.println("ioc容器创建完成了。。。。");
		
		BookService bookService = ctx.getBean(BookService.class);
		System.out.println(bookService);
	}
}

5、启动测试类,运行效果如下: 在这里插入图片描述

三、spring自动装配——@Autowired(如果找到多个相同类型的组件,BookService 层的组件id为bookDao2,@Bean中指定BookService 层的组件id为bookDao2)示例2

1、创建一个maven项目(创建maven项目过程省略),pom.xml文件引入如下依赖:


	org.springframework
	spring-context
	5.2.0.RELEASE


	junit
	junit
	4.12
	test

2、创建dao层、service层、controller层,代码如下:

1)dao层代码:

package com.rf.dao;

import org.springframework.stereotype.Repository;

@Repository
public class BookDao {

	private String lable="1";
	
	public String getLable() {
		return lable;
	}

	public void setLable(String lable) {
		this.lable = lable;
	}

	@Override
	public String toString() {
		return "BookDao [lable=" + lable + "]";
	}
}

2)service层代码:

package com.rf.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rf.dao.BookDao;

@Service
public class BookService {
	
	@Autowired
	BookDao bookDao2;
	
	public void print(){
		System.out.println(bookDao2);
	}

	@Override
	public String toString() {
		return "BookService [bookDao2=" + bookDao2 + "]";
	}
}

3)controller层代码:

package com.rf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.rf.service.BookService;

@Controller
public class BookController {
	
	@Autowired
	BookService bookService;
}

3、编写配置类,代码如下:

package com.rf.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
* 自动装配:
*  Spring利用依赖注入(DI),完成对IOC容器中中各个组件的依赖关系赋值;
*    1)、默认优先按照类型去容器中找对应的组件:applicationContext.getBean(BookDao.class);找到就赋值
*/
@Configuration
//通过扫描的方式注册一个bookDao组件
@ComponentScan({"com.rf.controller","com.rf.service","com.rf.dao"})
public class MyConfigOfAutowired {
	
	//通过@Bean的方式在注册一个bookDao组件
	@Bean("bookDao2")
	public BookDao bookDao(){
		BookDao bookDao = new BookDao();
		bookDao.setLable("2");
		return bookDao;
	}
}

4、编写测试类,代码如下:

package com.rf.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.rf.config.MyConfigOfAutowired;
import com.rf.dao.BookDao;
import com.rf.service.BookService;

public class Test2 {
	
	@org.junit.Test	
	public void test(){
		
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfigOfAutowired.class);
		System.out.println("ioc容器创建完成了。。。。");
		
		BookService bookService = ctx.getBean(BookService.class);
		System.out.println(bookService);
	}
}

5、启动测试类,运行效果如下: 在这里插入图片描述

四、spring自动装配——@Autowired(如果找到多个相同类型的组件,使用@Qualifier指定需要装配的组件的id,而不是使用属性名)示例

1、创建一个maven项目(创建maven项目过程省略),pom.xml文件引入如下依赖:


	org.springframework
	spring-context
	5.2.0.RELEASE


	junit
	junit
	4.12
	test

2、创建dao层、service层、controller层,代码如下:

1)dao层代码:

package com.rf.dao;

import org.springframework.stereotype.Repository;

@Repository
public class BookDao {

	private String lable="1";
	
	public String getLable() {
		return lable;
	}

	public void setLable(String lable) {
		this.lable = lable;
	}

	@Override
	public String toString() {
		return "BookDao [lable=" + lable + "]";
	}
}

2)service层代码:

package com.rf.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rf.dao.BookDao;

@Service
public class BookService {

	@Qualifier("bookDao")//使用@Qualifier指定需要装配的组件的id,而不是使用属性名
	@Autowired
	BookDao bookDao2;
	
	public void print(){
		System.out.println(bookDao2);
	}

	@Override
	public String toString() {
		return "BookService [bookDao2=" + bookDao2 + "]";
	}
}

3)controller层代码:

package com.rf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.rf.service.BookService;

@Controller
public class BookController {
	
	@Autowired
	BookService bookService;
}

3、编写配置类,代码如下:

package com.rf.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
* 自动装配:
*  Spring利用依赖注入(DI),完成对IOC容器中中各个组件的依赖关系赋值;
*    1)、默认优先按照类型去容器中找对应的组件:applicationContext.getBean(BookDao.class);找到就赋值
*/
@Configuration
//通过扫描的方式注册一个bookDao组件
@ComponentScan({"com.rf.controller","com.rf.service","com.rf.dao"})
public class MyConfigOfAutowired {
	
	//通过@Bean的方式在注册一个bookDao组件
	@Bean("bookDao2")
	public BookDao bookDao(){
		BookDao bookDao = new BookDao();
		bookDao.setLable("2");
		return bookDao;
	}
}

4、编写测试类,代码如下:

package com.rf.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.rf.config.MyConfigOfAutowired;
import com.rf.dao.BookDao;
import com.rf.service.BookService;

public class Test2 {
	
	@org.junit.Test	
	public void test(){
		
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfigOfAutowired.class);
		System.out.println("ioc容器创建完成了。。。。");
		
		BookService bookService = ctx.getBean(BookService.class);
		System.out.println(bookService);
	}
}

5、启动测试类,运行效果如下: 在这里插入图片描述

五、spring自动装配——@Autowired(让Spring进行自动装配的时候,默认使用首选的bean;)示例

1、创建一个maven项目(创建maven项目过程省略),pom.xml文件引入如下依赖:


	org.springframework
	spring-context
	5.2.0.RELEASE


	junit
	junit
	4.12
	test

2、创建dao层、service层、controller层,代码如下:

1)dao层代码:

package com.rf.dao;

import org.springframework.stereotype.Repository;

@Repository
public class BookDao {

	private String lable="1";
	
	public String getLable() {
		return lable;
	}

	public void setLable(String lable) {
		this.lable = lable;
	}

	@Override
	public String toString() {
		return "BookDao [lable=" + lable + "]";
	}
}

2)service层代码:

package com.rf.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rf.dao.BookDao;

@Service
public class BookService {

	
	@Autowired
	BookDao bookDao;
	
	public void print(){
		System.out.println(bookDao);
	}

	@Override
	public String toString() {
		return "BookService [bookDao=" + bookDao + "]";
	}
}

3)controller层代码:

package com.rf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.rf.service.BookService;

@Controller
public class BookController {
	
	@Autowired
	BookService bookService;
}

3、编写配置类,代码如下:

package com.rf.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
* 自动装配:
*  Spring利用依赖注入(DI),完成对IOC容器中中各个组件的依赖关系赋值;
*    1)、默认优先按照类型去容器中找对应的组件:applicationContext.getBean(BookDao.class);找到就赋值
*/
@Configuration
//通过扫描的方式注册一个bookDao组件
@ComponentScan({"com.rf.controller","com.rf.service","com.rf.dao"})
public class MyConfigOfAutowired {
	
	//通过@Bean的方式在注册一个bookDao组件
	@Primary //让Spring进行自动装配的时候,默认使用首选的bean;
	@Bean("bookDao2")
	public BookDao bookDao(){
		BookDao bookDao = new BookDao();
		bookDao.setLable("2");
		return bookDao;
	}
}

4、编写测试类,代码如下:

package com.rf.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.rf.config.MyConfigOfAutowired;
import com.rf.dao.BookDao;
import com.rf.service.BookService;

public class Test2 {
	
	@org.junit.Test	
	public void test(){
		
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfigOfAutowired.class);
		System.out.println("ioc容器创建完成了。。。。");
		
		BookService bookService = ctx.getBean(BookService.class);
		System.out.println(bookService);
	}
}

5、启动测试类,运行效果如下: 在这里插入图片描述

关注
打赏
1661269038
查看更多评论
立即登录/注册

微信扫码登录

0.0446s