Spring中的Bean的管理-基于注释的装配

基于注释的装配

在Spring中,使用xml配置文件实现Bean的装配工作,但是实际开发中如果Bean的数量较多,会导致XML配置文件过于臃肿,给后期维护升级带来一定的困难。为解决此问题,Spring提供了注解,通过注解也可以实现Bean的装配


语言: java
技术: spring
运行环境: eclipse
测试工具: JUnit


前言

这里提供一些Spring常用注解

注解描述
@Component指定一个普通的Bean,可以作用在任何层次
@Controller指定一个控制器组件Bean,用于将控制层的类标识为Spring中的Bean,功能上等同于@Component
@Service指定一个业务逻辑组件Bean,用于将业务逻辑层的类标识为Spring中的Bean,功能上等@Component
@Repository指定一个数据访问组件Bean,用于将数据访问层的类标识为Spring中的Bean,功能上@Component
@Scope指定Bean实例的作用域
@Value指定Bean实例的注入值
@Autowired指定要自动装配的对象
@Resource指定要注入的对象
@Qualifier指定要自动装配的对象名称,通常与@Autowired联合使用
@PostConstruct指定Bean实例完成初始化后调用的方法
@PreDestroy指定Bean实例销毁前调用的方法

一、导入依赖

在webapp下的WEB-INF下的lib包中导入spring-aop-4.3.6RELEASE.jar依赖包
总体结构如下:在这里插入图片描述

二、创建XML配置文件

在src目录下创建applicationContext.xml
注意:是放在src目录下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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-4.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
http://www.springframework.org/schema/jdbc 
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<!-- 设置开启注解 --> 
<context:annotation-config />
<!-- 设置扫描哪个包下的注解 --> 
<context:component-scan base-package="controller" />
<context:component-scan base-package="dao" />
<context:component-scan base-package="service" />
<context:component-scan base-package="test" />
<!-- 以上是主要要扫描的四个包下的注解,也可将四个包都放在同一个包下面,这样只需写一行代码 -->

</beans>

三、定义、实现

1.定义DAO层

package dao;
/**
 *在dao包下创建UserDao接口作为数据访问层接口,
 *并在UserDao接口中声明say()方法,
 */
public interface UserDao {
	public void say();
}
}

2.实现DAO层

package dao;
/**
 * 在dao包下创建UserDaoImpl作为UserDao的实现类,
 * 并在UserDaoImpl类中实现UserDao接口中的say()方法
 */
import org.springframework.stereotype.Repository;

@Repository(value="userDao")	//@Repository注解将UserDaoImpl类标识为Spring中的Bean
public class UserDaoImpl implements UserDao{
	
	@Override
	public void say() {
		System.out.println("userDao say hello world!");		//打印提示信息
	}
}

3.定义Service层

package service;
/**
 *在service包下创建UserService接口作为业务逻辑层接口,
 *并在UserService接口中定义say()方法
 */
public interface UserService {
	public void say();
}

4.实现Service层

package service;
/**
 * 在service包下创建UserServiceImpl作为UserService的实现类,
 * 并在UserServiceImpl类中实现UserService接口中的Say()方法。
 */
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import dao.UserDao;

@Service(value="userService")	//使用@Service注解将UserServiceImpl类标识为Spring中的Bean
public class UserServiceImpl implements UserService{

	@Resource(name="userDao")	//使用@Resource注解注入UserDao
	private UserDao userDao;
	
	@Override
	public void say() {
		this.userDao.say();
		System.out.println("UserService say hello world");
	}
}

5.定义Controller层

package controller;


import javax.annotation.Resource;
import org.springframework.stereotype.Controller;

import service.UserService;

@Controller		//使用@Controller注解将UserController类标识为Spring中的Bean
public class UserController {
	
	@Resource(name="userService")	使用@Resource注解注入UserService
	private UserService userService;
	
	public void say() {
		this.userService.say();
		System.out.println("userController say Hello world");
	}
}


6.定义测试类

package test;

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

import controller.UserController;

public class test {
	
	@Test	//@Test:测试方法,在这里可以测试期望异常和超时时间
	 public void test(){
		//通过容器获取对象而非new 
		ApplicationContext applicationContext=new  ClassPathXmlApplicationContext("applicationContext.xml");
		UserController userController = (UserController)applicationContext.getBean("userController");
		userController.say(); 
	 }
}


7.添加JUnit

这里可能Test会报错,原因可能是没有添加JUnit
以下是添加方法:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

7.测试结果

测试步骤1
在这里插入图片描述
测试步骤2
在这里插入图片描述
测试步骤3
在这里插入图片描述
测试步骤4
在这里插入图片描述


总结

这里对文章进行总结:

Spring容器已经成功获取了UserController实例,并通过调用实例中的方法执行了各层中的输出语句,这说明Spring容器基于注解完成了Bean的装配。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阋木

我很穷

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值