我是靠谱客的博主 土豪白云,最近开发中收集的这篇文章主要介绍spring的基本注解使用demo,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

基本注解的使用,首先在eclipse 中新建一个spring + maven 的web工程,测试注解使用的文件包含applicationContent.xml,Service.java(接口),ServiceImpl.java(实现类1),ServiceImpl2.java(实现类2),TestAnnotation.java(测试类),这四个文件。

主要使用的注解@Autowired,@Service,@Qualifier

相关文件内容如下:

1、配置applicationContent.xml,主要就是添加使用注解的配置<context:annotation-config/>和扫描包文件<context:component-scan base-package="com.mdl.model" />,有一点需要注意的是,这里的包文件的路径,尽量能够精确就精确,不要使用com.mdl.*这样统称的扫描方式,如果其他包里面有错误,就会报一些奇怪的错,实际是跟这几个文件没关系的,之前经常碰到的错就是:

annotations:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

实际上是其他文件的注解有问题。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- <context:annotation-config/>的作用是式地向 Spring 容器注册
AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、
PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 这 4 个BeanPostProcessor。
注册这4个 BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。 -->
<!-- <context:annotation-config/> 是用来打开注解的,但是此处没有加入该注解也能使用,具体原因不知道为啥 -->
<!-- <context:annotation-config/> -->
<context:component-scan base-package="com.mdl.model" />
</beans>

2、Services.java(就是一个接口)

package com.mdl.model.annotation;
public interface Services {
void print(String str);
}

3、ServicesImpl.java(Services.java 的实现类1)

package com.mdl.model.annotation.impl;
import org.springframework.stereotype.Service;
import com.mdl.model.annotation.Services;
@Service("testServicesImpl")
public class ServicesImpl implements Services{
@Override
public void print(String str) {
System.out.println("打印:" + str);
}
}

4、ServicesImpl2.java(Services.java 的实现类2)

package com.mdl.model.annotation.impl;
import org.springframework.stereotype.Service;
import com.mdl.model.annotation.Services;
@Service("testServicesImpl2")
public class ServicesImpl2 implements Services{
@Override
public void print(String str) {
System.out.println("打印2:" + str);
}
}

注意:这里面Service.Java有两个实现类ServicesImpl.java 和ServicesImpl2.java,分别用@Service(“testServicesImpl”)和@Service(“testServicesImpl2”)在对应实现类上作标注,表示不同的实现类,然后在调用的时候通过@Qualifier(“testServicesImpl”)来表示使用的是哪个实现类,见下面:

5.1、TestAnnotation.java(测试方法一)

package com.mdl.model;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import com.mdl.model.annotation.Services;
@Component
public class TestAnnotation {
@Autowired
@Qualifier("testServicesImpl")
private Services testServices;
public static void main(String[] args) {
ClassPathXmlApplicationContext cat = new ClassPathXmlApplicationContext("classpath*:applicationContent.xml");
TestAnnotation bean = cat.getBean(TestAnnotation.class);
bean.printInfo();
}
@Test
public void printInfo() {
testServices.print("测试注解……");
}
}

结果:

打印:测试注解……

注:以上是通过调用spring的ClassPathXmlApplicationContext,来直接调用TestAnnotation.java 的bean的方法。

5.2、TestAnnotation2.java(测试方法二)

package com.mdl.model;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.mdl.model.annotation.Services;
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:applicationContent.xml" })
public class TestAnnotation2 {
@Autowired
@Qualifier("testServicesImpl2")
private Services testServices;
@Test
public void printInfo() {
testServices.print("测试注解……");
}
}

结果:

打印2:测试注解……

注:以上是通过Junit4直接调用相应类和方法执行

最后

以上就是土豪白云为你收集整理的spring的基本注解使用demo的全部内容,希望文章能够帮你解决spring的基本注解使用demo所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部