Spring框架 (7) —— 使用注解配置Spring

使用注解配置Spring

前言:

学习基于注解的IoC配置,大家脑海里首先得有一个认知,即注解配置和xml配置要实现的功能都是一样的,都是要降低程序间的耦合。只是配置的形式不一样。
关于实际的开发中到底使用xml还是注解,每家公司有着不同的使用习惯。所以这两种配置方式我们都需要掌握。

基于注解配置的方式也已经逐渐代替xml。所以我们必须要掌握使用注解的方式配置Spring。

配置步骤

注意:Eclipse需要先安装了STS插件,或者使用STS开发工具创建项目。

第一步:拷贝必备jar包到工程的lib目录。

注意:在基于注解的配置中,我们还要多拷贝一个aop的jar包。如下图:
在这里插入图片描述

第二步:在类的根路径下创建一个任意名称的xml文件(不能是中文)

在这里插入图片描述
注意:基于注解整合时,Spring配置文件导入约束时需要多导入一个context名称空间下的约束

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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
">
</beans>

第三步:创建一个服务类

创建一个测试的服务类,并且加入使用@Component注解,声明该类允许注入到Spring容器

package com.xc.spring.service;

import org.springframework.stereotype.Component;

//1.使用注解配置,需要将启动是就创建对象的类表示为组件类
@Component
public class CustomerService {

	public void save(){
		System.out.println("-保存数据-");
	}

}

第四步在spring的配置文件加入扫描注解

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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="cn.xc.spring"></context:component-scan>
</beans>

第五步:测试调用代码

package com.xc.spring.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xc.spring.service.CustomerService;

public class CustomerServiceTest {
	
	public static void main(String[] args) {
		//CustomerService cs=new CustomerService();
		ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		
		CustomerService customerService = context.getBean("customerService",CustomerService.class);
		customerService.save();
		context.close();
	}
}

测试结果,如果可以调用服务方法,测试成功

在这里插入图片描述

设置注解扫描的组件的名称

默认情况下, 被注解@Component 扫描的类的名称就是当前类名的首字母小写名称,开发者可以自定义组件的名称

/*	使用注解方式配置IOC
	@Component 说明当前类被Spring管理,Spring框架启动的时候就会创建此类的对象
	设置当前Bean的名称
		默认当前Bean的名称就是简单类名的 首字母小写 customerService
		value 属性可以自定义组件的名称 等价于 <bean id/name="bean名称">
		@Component(value="service")
		简写,可以省略value
		@Component("service")
	
*/
@Component("service")
public class CustomerService {
	public void save() {
		System.out.println("-保存数据-");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值