spring的基本注解使用demo

来源:互联网 发布:织梦模板html源码 编辑:程序博客网 时间:2024/05/17 18:03

基本注解的使用,首先在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;@Componentpublic 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直接调用相应类和方法执行

原创粉丝点击