自动注入与组件扫描

自动注入与组件扫描

1.自动注入

​ 1.spring为了简化,给我们提供了自动注入的功能:
​ 2.当指定了autowire="byName"属性的时候,spring会根据Bean的属性(id)(对象的setXXX)来注入对象。
​ 3.当指定了autowire="byType"属性的时候,spring会根据类型(class)来查找对应的对象;
​ 以上都是set注入

1.1XML配置文件
	<bean id="cat" class="fyjz.com.Spring.Cat"/>
	<bean id="dog" class="fyjz.com.Spring.Dog"/>
	<bean id="dog1" class="fyjz.com.Spring.Dog"/>
	<bean id="p" class="fyjz.com.Spring.Person"/>
	<!--注解驱动-->
	<context:annotation-config/>

注意:注解驱动不可省略,没有注解驱动,我们无法在类中识别javaBean对象

1.2Dog和Cat类
package fyjz.com.Spring;

public class Dog {
	
	public void show(){
		System.out.println("汪汪");
	}
}
package fyjz.com.Spring;

public class Cat {
	public void show(){
		System.out.println("喵喵");
	}
}
1.3Person类

我们现在需要将猫和狗的对象注入到Person对象中。

几种自动注入的注解如下:

@Autowire是一个spring组件,默认以autowire="byType"类型,来查找相关的对象。注意:该注解可以写在set方法上面,建议写在属性上面。

@Qualifier当类型一样时,需要@Autowired和@Qualifier配合使用,指定具体注入的对象。

@Resource先用byName查找,如果没有该bean属性的名字,则用getType查找,当类型一样时需要@Resource(name=“XX”),指定具体注入的对象。

package fyjz.com.Spring;

public class Person {
	//@Autowired//默认以autowire="byType"
	//@Qualifier(value="dog1")
	@Resource(name="dog")
	private Dog dog;
	//@Autowired
	@Resource
	private Cat cat;
	
	
	public Dog getDog() {
		
		return dog;
	}
	public void setDog(Dog dog) {
		System.out.println("测试1");
		this.dog = dog;
	}
	public Cat getCat() {
		return cat;
	}
	public void setCat(Cat cat) {
		System.out.println("测试2");
		this.cat = cat;
	}
	
	
	@Override
	public String toString() {
		return "Person [dog=" + dog + ", cat=" + cat + "]";
	}
}

相当于当Spring容器中有多个同种类型的对象时:@Autowired+@Qualifier(value=“dog1”)=@Resource(name=“dog1”)

这两种方式我们在一般的工作使用的频率时1:1

1.4Test测试
package springTest;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import fyjz.com.Spring.Cat;
import fyjz.com.Spring.Dog;
import fyjz.com.Spring.Person;

public class Test01 {
ClassPathXmlApplicationContext ac;
	
	@Before
	@Test
	public void init(){
		ac=new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	
	@Test
	public void test01(){
		Person p=ac.getBean("p",Person.class);
		Dog dog=p.getDog();
		dog.show();
		Cat cat=p.getCat();
		cat.show();
	}
}
1.5执行结果

在这里插入图片描述

2.组件扫描

2.1概念

​ Spring容器会扫描指定的包及其包下面的所有类,该包必须有特有的注解,
​ 则容器会将该类纳入spring容器管理
​ 特有的注解:
​ @Component 通用注解
​ @Service 业务层注解
​ @Repository 持久层注解
​ @Controller 控制层注解

2.2XML配置文件
	<!--注解驱动-->
	<context:annotation-config/>

	<!--在xml配置文件中配置组件扫描-->
	<context:component-scan base-package="fyjz.com.Spring02"/>
2.3Cat和Dog类
package fyjz.com.Spring02;

import org.springframework.stereotype.Component;

@Component(value="c")
public class Cat {
	public void show(){
		System.out.println("miaomiao");
	}
}
package fyjz.com.Spring02;

import org.springframework.stereotype.Component;

@Component
public class Dog {
	
	public void show(){
		System.out.println("wangwang");
	}
}

注意:Cat和Dog类上都有@Component,为的是让Spring扫描到该类,并管理该对象。

2.4Person2类

@Component(value=“XX”)xx是修饰bean的id,也可以直接@Component(“p”)如果不修改bean的id,spring容器管理该对象后,以该对象名的首写小写开始为bean的id,例如该对象名Person那么被spring容器管理后bean的默认id为person。

@Scope(“prototype”)//多例模式 @Scope(“Singleton”)单例模式

@Lazy(true)//默认为饿汉式@Lazy(false)容器启动后创建对象,懒汉式@Lazy(true):访问bean对象时,创建对象

package fyjz.com.Spring02;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import fyjz.com.Spring.Teacher;

@Component(value="p")
@Scope("prototype")
@Lazy(true)
public class Pserson02 {
	@Value("超哥")
	private String name;
	@Value("21")
	private int age;
	
	@Autowired
	private Dog dog;
	@Autowired
	private Cat cat;
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	
	public Dog getDog() {
		return dog;
	}
	public void setDog(Dog dog) {
		this.dog = dog;
	}
	public Cat getCat() {
		return cat;
	}
	public void setCat(Cat cat) {
		this.cat = cat;
	}
	@Override
	public String toString() {
		return "Pserson02 [name=" + name + ", age=" + age + "]";
	}
	
}

2.5Test测试类
package springTest;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import fyjz.com.Spring.Teacher;
import fyjz.com.Spring02.Cat;
import fyjz.com.Spring02.Dog;
import fyjz.com.Spring02.Pserson02;


public class Test02 {
ClassPathXmlApplicationContext ac;
	
	@Before
	@Test
	public void init(){
		ac=new ClassPathXmlApplicationContext("applicationContext2.xml");
	}
	
	@Test
	public void test01(){
		Pserson02 p=ac.getBean("p",Pserson02.class);
		System.out.println(p);
	}
}

2.6测试结果

在这里插入图片描述

总结:自动注入方式用于处理对象与对象之间的关系,比如说,将一个对象注入到另一个对象中去,而组件扫描用于处理对象与spring容器之间的关系,这直接影响一个类所生成的对象是否能归容器去管理。

3.配置文件类

3.1配置文件类
package fyjz.kunge.Spring03;

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

/*
 * @Configuration相当于我们的xml文件的<beans>
 * 
 * @ComponentScan("fyjz.kunge.Spring03")相当于xml中的<context:component-scan base-package="fyjz.com.*"/>
 */
@Configuration
@ComponentScan("fyjz.kunge.Spring03")
public class Config {
	/*
	 * @Bean相当于xml中的<bean>
	 * Bean的id就是方法的名字getCat
	 * Bean的class就是返回的对象new Cat();
	 */
	@Bean
	public Cat getCat(){
		return new Cat();
	}
}

该类代替了我们在xml中所写的配置文件

3.2Cat类
package fyjz.kunge.Spring03;

import org.springframework.stereotype.Component;

@Component
public class Cat {
	
	public Cat(){
		System.out.println("猫的构造方法");
	}
	public void show(){
		System.out.println("喵喵。。。。");
	}
}
3.3Person类
package fyjz.kunge.Spring03;

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

@Component
public class Person {
	@Autowired
	private Cat cat;

	public Cat getCat() {
		return cat;
	}

	public void setCat(Cat cat) {
		this.cat = cat;
	}
	
}
3.4Test测试类
package springTest;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import fyjz.kunge.Spring03.Config;
import fyjz.kunge.Spring03.Person;

public class Test03 {
	
	
	@Test
	public void getCats(){
		ApplicationContext ac=new AnnotationConfigApplicationContext(Config.class);
		Person c=ac.getBean("person",Person.class);
		c.getCat().show();
	}
	
}
3.5测试结果

在这里插入图片描述
从上可以看出在加载config这个类时,给我们生成了猫这个对象,我们用的时候,只需使用即可,无需再次创建。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值