博客
关于我
Spring05:使用注解开发
阅读量:793 次
发布时间:2019-03-25

本文共 2232 字,大约阅读时间需要 7 分钟。

Spring注解配置与使用指南

Bean的实现

在Spring中,通过注解可以实现对象的管理。以下将详细介绍Bean的配置和使用方法。

1. 配置注解扫描包

确保Spring能够扫描指定的包,找到带有注解的类。以下配置示例:

2. 编写组件类

在指定包下编写Spring管理的组件类,并添加注解。以下是一个典型的示例:

@Component("person") //组件ID可以自定义,建议使用 lowercasepublic class Person {    @Value("韦德") //可以直接注入值,不需要set方法    public String name; //字段直接注入}

3. 测试Bean的获取

编写测试代码,通过Spring的 ApplicationContext 获取Bean实例:

@Testpublic void test01(){    ApplicationContext context = new ClassPathXmlApplicationContext("springConfig.xml");    Person person = (Person) context.getBean("person");    System.out.println("姓名:" + person.name);}

属性注入

在Spring中,属性注入可以通过@Value标注实现,支持直接注入值或通过 setter方法注入。

1. Direct Value Injection

如果类属性已经有 setter方法,可以直接注入值:

@Component("person")public class Person {    private String name;    @Value("韦德")    public void setName(String name) {        this.name = name;    }    public String getName() {        return name;    }}

2. 测试属性注入

测试代码如下:

@Testpublic void test01() {    ApplicationContext context = new ClassPathXmlApplicationContext("springConfig.xml");    Person person = (Person) context.getBean("person");    System.out.println("姓名:" + person.getName());}

衍生注解

@Component是Spring中常用的注解,随着项目复杂,逐渐衍生出多种类型的注解,便于不同的层次管理。

常用衍生注解

  • @Controller: 用于控制器层
  • @Service: 用于服务层
  • @Repository: 用于数据访问层

XML与注解对比

两种配置方式各有优缺点:

优点对应

  • /XML: 结构清晰,适合复杂配置,维护方便
  • 注解: 开发简单直观,不需要自己写配置文件

XML与注解的结合使用

推荐实践

  • 用XML管理Bean定义
  • 用注解完成属性注入
  • 在实际项目中,可以灵活选择混合配置
  • 注解驱动配置

    了解<context:annotation-config>的作用:

    < زیرкут:annotation-config />
    • 用于激活注解驱动注册的Bean
    • 例如,在已经定义的Bean上添加注解,可以通过这个标签显式生效
    • 如果不使用注解驱动,注入的值可能为null

    基于Java类的配置

    1. 定义User实体类

    @Componentpublic class User {    public String name = "用户";}

    2. 创建配置类

    @Configurationpublic class MyConfig {    @Bean    public User user() {        return new User();    }}

    3. 测试基于类的配置

    @Testpublic void test02() {    ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);    User user = (User) context.getBean("user");    System.out.println("姓名:" + user.name);}

    导入其他配置类

    1. 定义额外配置类

    @Configurationpublic class MyConfig2 {}

    2. 导入到主配置类

    @Configuration@Import(MyConfig2.class)public class MyConfig {    @Bean    public User user() {        return new User();    }}

    通过以上配置方法,可以方便地管理Spring中的Bean IllegalStateException,完成对象的创建和注入。注解配置简化了传统XML配置的复杂性,是现代Spring应用的主要选择。

    转载地址:http://qmsuk.baihongyu.com/

    你可能感兴趣的文章
    MySQL万字总结!超详细!
    查看>>
    Mysql下载以及安装(新手入门,超详细)
    查看>>
    MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
    查看>>
    MySQL不同字符集及排序规则详解:业务场景下的最佳选
    查看>>
    Mysql不同官方版本对比
    查看>>
    MySQL与Informix数据库中的同义表创建:深入解析与比较
    查看>>
    mysql与mem_细说 MySQL 之 MEM_ROOT
    查看>>
    MySQL与Oracle的数据迁移注意事项,另附转换工具链接
    查看>>
    MySQL两千万数据优化&迁移
    查看>>
    MySql中 delimiter 详解
    查看>>
    MYSQL中 find_in_set() 函数用法详解
    查看>>
    MySQL中auto_increment有什么作用?(IT枫斗者)
    查看>>
    MySQL中B+Tree索引原理
    查看>>
    mysql中cast() 和convert()的用法讲解
    查看>>
    mysql中datetime与timestamp类型有什么区别
    查看>>
    MySQL中DQL语言的执行顺序
    查看>>
    mysql中floor函数的作用是什么?
    查看>>
    MySQL中group by 与 order by 一起使用排序问题
    查看>>
    mysql中having的用法
    查看>>
    MySQL中interactive_timeout和wait_timeout的区别
    查看>>