Spring中基于注解的IOC配置

说明

并不是说使用了注解之后,就可以不用使用xml文件,使用注解也需要配置xml文件,但是xml文件配置的不再是bean,而是配置扫描,扫描的配置指的是,Spring能够从配置的指定的classpath中自动扫描,侦测和实例化具有特定的Spring注解的组件【类】。

环境搭建

  • 加入jar包

    commons-logging-1.1.3.jar
    spring-aop-4.0.0.RELEASE.jar
    spring-beans-4.0.0.RELEASE.jar
    spring-context-4.0.0.RELEASE.jar
    spring-core-4.0.0.RELEASE.jar
    spring-expression-4.0.0.RELEASE.jar

  • 编写需要加入到SpringIOC容器中管理的组件

package com.wanee.annotation;

import org.springframework.stereotype.Component;
<!--
		注解Component 把资源让 spring 来管理。相当于在 xml 中配置一个 bean
		相当于:<bean id="" class="">
-->
@Component  
public class Car {
	private String brand;
	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	@Override
	public String toString() {
		return "Car [brand=" + brand  + "]";
	}
	
}
  • 编写SPring的配置文件
<?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">
	
	<!-- 
		配置自动扫描的包:表示这个包【或者子包】中带有Spring注解的组件会被IOC容器管理
		context:component-scan : 用来配置自动扫描的包
			base-package : 表示配置扫描的基础包,则表示会扫描此包及其子包中所有的Spring的注解
	 -->
	<context:component-scan base-package="com.wanee.annotation"></context:component-scan>

</beans>
  • 编写测试类
public class Main {

	public static void main(String[] args) {
		
		ApplicationContext act = new ClassPathXmlApplicationContext("annotation.xml");
		
		Car car = act.getBean(Car.class);
		
		System.out.println(car);
	}
}

常用注解

  • 用与创建对象的

相当于:< bean id="" class="">

@Component

作用:把资源让 spring 来管理。相当于在 xml 中配置一个 bean。
属性:value:指定 bean 的 id。如果不指定 value 属性,默认 bean 的 id 是当前类的类名,首字母小写。

//@Component(value = "car")  
@Component("car")  //如果注解中有且只有一个属性要赋值时,且名称是 value,value 在赋值是可以不写。  
	public class Car {
		....
	}

@Controller @Service @Repository
这三个注解都是针对Component的衍生注解,他们的作用及属性都是一模一样的。只是提供了更加明确的语义化。
@Controller:一般用于表现层的注解。
@Service:一般用于业务层的注解。
@Repository:一般用于持久层的注解。

  • 注入数据

相当于:< property name="" ref="">

@Autowired
自动按照类型注入。当使用注解注入属性时,set 方法可以省略。它只能注入其他 bean 类型。当有多个类型匹配时,使用要注入的对象变量名称作为 bean 的 id,在 spring 容器查找,找到了也可以注入成功。找不到就报错。

/**
	 * 	在注解中,可以在
	 * 		- 属性声明上面使用@AutoWired注解,
	 * 		- 或者在对应的Setter方法上使用@AutoWired注解,
	 * 	注意点:
	 * 		①:如果在属性声明方法上使用@AutoWire注解,那么我们的Setter和Getter方法可以省略
	 * 		②:如果IOC容器中没有对应的Car的bean存在,则程序报错
	 * 			- 默认情况下@Autowired自动装配的bean是必须能够自动装配上的,如果装配不上,则报错
	 * 			- 这个时候我们可以改变@Autowired属性,设置为非必须装配,表示有则装配,没有则为null 使用required=false配置
	 * 		③:如果IOC容器中存在两个相同类型的bean,那么装配的时候到底使用哪个呢?
	 * 			- 在属性的setter方法上使用@Autowired,并在入参中使用@Qualifier注解指定装配哪个bean
	 * 				@Autowired
					public void setCar(@Qualifier("car1")Car car) {
						this.car = car;
					}
				- 但是以上的方式略显复杂,还有更好的方式,我们依然在属性声明上使用@AutoWired注解,但是属性的名称我们可以使用指定的bean的id
					@Autowired
					private Car car2;
	 */
	@Autowired
	private CartService cartService;

@Qualifier
作用:在自动按照类型注入的基础之上,再按照 Bean 的 id 注入。它在给字段注入时不能独立使用,必须@Autowire 一起使用;但是给方法参数注入时,可以独立使用。

	@Autowired
	@Qualifier("car2")
	private Car car;

@Resource
作用:直接按照 Bean 的 id 注入。它也只能注入其他 bean 类型。
属性:name:指定 bean 的 id。

    @Resource(name=car1")
    private Car car;

@Value
作用:注入基本数据类型和 String 类型数据的
属性:value:用于指定值

@Value("小仙女")
private String name;
  • 用于改变作用范围的
    @Scope
    作用:指定 bean 的作用范围。
    属性:value:指定范围的值。
    取值:singleton prototype request session globalsession
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值