3.5+ Spring框架中常用注解的使用

常用注解:

1.@Controller 控制器
用于标注控制层,相当于struts中的action层

2.@Service 服务
用于标注服务层,主要用来进行业务的逻辑处理

3.@Repository
用于标注数据访问层,也可以说用于标注数据访问组件,即DAO组件

4.@Component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)
泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类。

用法:都是标注在类名上,用于注册一个bean到Spring上下文中。
区别:@Service 用于服务层;@Controller用于控制层;@Repository用于DAO层;不确定的用@Component

@Autowire和@Resource都是Spring支持的注解方式动态装配bean。

5.@Autowire默认按照类型(by-type)装配,默认情况下要求依赖对象必须存在。

-如果允许依赖对象为null,需设置required属性为false,即

@Autowire(required=false)
private InjectionBean beanName;

-如果使用按照名称(by-name)装配,需结合@Qualifier注解使用,即

@Autowire
@Qualifier("beanName")
 private InjectionBean beanName;

6.@Resource默认按照名称(by-name)装配,名称可以通过name属性指定。

-如果没有指定name
1.当注解在字段上时,默认取name=字段名称装配。
2.当注解在setter方法上时,默认取name=属性名称装配。

-当按照名称(by-name)装配未匹配时,按照类型(by-type)装配。
1.当显示指定name属性后,只能按照名称(by-name)装配。

-@Resoure装配顺序
如果同时指定name和type属性,则找到唯一匹配的bean装配,未找到则抛异常;
如果指定name属性,则按照名称(by-name)装配,未找到则抛异常;
如果指定type属性,则按照类型(by-type)装配,未找到或者找到多个则抛异常;
既未指定name属性,又未指定type属性,则按照名称(by-name)装配;如果未找到,则按照类型(by-type)装配。

下面通过一个实例讲解如何使用这些注解

目录结构

img

1.创建Dao层

在ch3应用的src中,创建annotation.Dao包,该包下创建TestDao接口和TestDaoImpl实现类,并将实现类TestDaoImpl使用@Repository注解标注为数据访问层。

TestDao.java

package annotation.dao;

public interface TestDao {
    public void save();
}

TestDaoImpl.java

package annotation.dao;

import org.springframework.stereotype.Repository;

@Repository("testDao")
public class TestDaoImpl implements TestDao {
    public void save() {
        System.out.println("testDao save");
    }
}

2.创建Service层

在ch3应用的src中,创建annotation.service包,该包下创建TestService接口和TestServiceImpl实现类,并将实现类TestServiceImpl使用@Service注解表注为业务逻辑层。

TestService.java

package annotation.service;

public interface TestService {
    public void save();
}

TestServiceImpl

package annotation.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import annotation.dao.TestDao;

@Service("testService")
public class TestServiceImpl implements TestService{
    @Resource(name="testDao")
    private TestDao testDao;
    public void save() {
        testDao.save();
        System.out.println("testService save");
    }
}

3.创建Controller层

在ch3应用的src中,创建annotation.controller包,该包下创建TestController类使用@Controller注解标注为控制器层。

TestController.java

package annotation.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import annotation.dao.TestDao;

@Service("testService")
public class TestServiceImpl implements TestService{
    @Resource(name="testDao")
    private TestDao testDao;
    public void save() {
        testDao.save();
        System.out.println("testService save");
    }
}

4.配置注解

由于annotation.dao、annotation.service和annotation.controller包都属于annotation包的子包,因此,不需要在配置annotationContext.xml中配置注解

annotationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    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命名空间,通过Spring扫描指定包下所有的Bean实现类,通过注释解析 -->
    <context:component-scan base-package="annotation"/>
     
</beans>

5.创建测试类

在ch3应用的test包中,创建测试类

TestMoreAnnotation.java

package test;

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

import annotation.AnnotationUser;
import annotation.controller.TestController;

public class TestMoreAnnotation {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext appCon = new ClassPathXmlApplicationContext("annotationContext.xml");
        TestController testcon = (TestController)appCon.getBean("testController");
        testcon.save();
    }

}

运行结果

img

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容