Spring学习之IOC控制反转(二.注解注入)

本文详细解析了Spring框架中各种注解的作用与用法,包括@Component、@Repository、@Service、@Controller等组件注解,以及@Autowired、@Resource、@Qualifier等依赖注入注解,并通过示例展示了它们在实际开发中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(一)各个注解作用
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

@Repository用于标注数据访问组件,即DAO组件。

@Service用于标注业务层组件、

@Controller用于标注控制层组件(如struts中的action)

@Scope用于指定scope作用域的(用在类上),有以下范围:
1.singleto:spring IOC容器中仅有一个实例
2.prototype:每次调用bean返回一个新的实例
3.request:每次HTTP请求都会创建一个新的bean
4.session:同一个HTTP Session共享一个bean
5.global session 同一个全局session共享一个bean
6.application:同一个application共享一个bean
(重点1和2)

@Autowired 默认按类型装配

@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。使用方法类似@Autowired,有:
@Resource(name=“zhangsan”),
@Resource(type=LiSi.class);

@Qualifier指定注解bean名称

@Autowired @Qualifier(“lisi”) 存在多个实例配合使用

1.@Autowired 配合@Qualifier实例指定bean(此处我们选择李四):

public interface People {
	public String getPeopleName();
}

public class ZhangSan implements People{
	@Override
	public String getPeopleName() {
		// TODO Auto-generated method stub
		return "我是张三";
	}
}

public class LiSi implements People{
	@Override
	public String getPeopleName() {
		// TODO Auto-generated method stub
		return "我是李四";
	}
}

public class PeopleMaker {
	@Autowired
	@Qualifier("lisi")
	private People people;
	@Override
	public String toString() {	
		return people.getPeopleName();		
	}
}

public class TestMethod {
	public static void main(String[] args) {	
		//创建spring容器对象
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		//获取spring创建的user对象
		PeopleMaker p=(PeopleMaker)ac.getBean("peoplemaker");
		System.out.println(p);
	}
}

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    ">
    <context:component-scan base-package="com.spring" />
	<bean id="peoplemaker" class="com.spring.model.PeopleMaker"></bean>
	<bean id="zhangsan" class="com.spring.model.ZhangSan"></bean>
	<bean id="lisi" class="com.spring.model.LiSi"></bean>
</beans>

2.Sevice配合Scope简化代码:

@Service
public class Monkey {
	private String monkeyName="monkeyKing";
	@Override
	public String toString() {
		return "Monkey [monkeyName=" + monkeyName + "]";
	}
}

@Service
public class Tiger {
	private String tigerName="tigerKing";
	@Override
	public String toString() {
		return "Tiger [tigerName=" + tigerName + "]";
	}
}

@Service("zoos")
@Scope("prototype")
public class Zoo {
	@Autowired
	private Tiger tiger;
	@Autowired
	private Monkey monkey;
	@Override
	public String toString() {
		return "Zoo [tiger=" + tiger + ", monkey=" + monkey + "]";
	}
}

public class TestMethod {
	public static void main(String[] args) {		
		//创建spring容器对象
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");		
		//获取spring创建的user对象
		Zoo z=(Zoo)ac.getBean("zoos");		
		System.out.println(z);		
	}
}

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    ">
  <!--省去了三行bean -->   
    <context:component-scan base-package="com.spring" />
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值