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直接调用相应类和方法执行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值