博客
关于我
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/

    你可能感兴趣的文章
    MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
    查看>>
    MySQL8修改密码的方法
    查看>>
    Mysql8在Centos上安装后忘记root密码如何重新设置
    查看>>
    Mysql8在Windows上离线安装时忘记root密码
    查看>>
    MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
    查看>>
    mysql8的安装与卸载
    查看>>
    MySQL8,体验不一样的安装方式!
    查看>>
    MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
    查看>>
    Mysql: 对换(替换)两条记录的同一个字段值
    查看>>
    mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
    查看>>
    MYSQL:基础——3N范式的表结构设计
    查看>>
    MYSQL:基础——触发器
    查看>>
    Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
    查看>>
    mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
    查看>>
    mysqldump 参数--lock-tables浅析
    查看>>
    mysqldump 导出中文乱码
    查看>>
    mysqldump 导出数据库中每张表的前n条
    查看>>
    mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
    查看>>
    Mysqldump参数大全(参数来源于mysql5.5.19源码)
    查看>>
    mysqldump备份时忽略某些表
    查看>>