spring IOC 注解

注解:就是一个类,使用@注解名称
开发中:使用注解 取代 xml配置文件。

一下是注解的作用

  1. @Component取代 不写id默认为类的首字母小写
    @Component(“id”) 取代
    2.web开发,提供3个@Component注解衍生注解(功能一样)取代
    @Repository :dao层
    @Service:service层
    @Controller:web层 只是名称有所变化功能一样
    3.依赖注入,给私有字段设置,也可以给setter方法设置
    普通值:@Value("")
    引用值:
    方式1:按照【类型】注入
    @Autowired
    方式2:按照【名称】注入1
    @Autowired
    @Qualifier(“名称”)
    方式3:按照【名称】注入2
    @Resource(“名称”)
    4.生命周期
    初始化:@PostConstruct
    销毁:@PreDestroy
    5.作用域
    @Scope(“prototype”) 多例
    使用前提需要添加命名空间 context

配置文件<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.xsd"> <!-- 组件扫描,扫描含有注解的类 --> <context:component-scan base-package="com.itheima.g_annotation.a_ioc"></context:component-scan> </beans>
下面是为@Autowired的使用其他注释基本一样

package com.qst.annotation_web;

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

@Controller("StudentActionId")
public class StudentAction {
	
	@Autowired
	private StudentService studenService;
	
	public void execute() {
		studenService.addStudent();
		
	}

}

每一个注释注释替代了xml的配置功能

	<!--

	
		@Controller("StudentActionId")
		public class StudentAction 
			如次注释替代了
				<bean id="personId" class="com.qst.annotation_web.StudentAction">
-->
@Autowired
	private StudentService studenService;代替了一下xml配置
	
	<bean id="personId" class="com.qst.annotation_web.StudentAction">
			<<property name="StudentService"  ref="StudentService这个类的bean对象ID名称"></property>
			</bean>
		
		
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值