注解开发

要使用注解开发,必须保证aop的包导入了。
使用注解需要导入context约束,增加约束的支持。

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>

1、bean
2、属性如何注入

@Component
public class User {
    public String name;

    //相当于写了一个<property>标签
    @Value("liuyf")
    public void setName(String name) {
        this.name = name;
    }
}

3、衍生的注解
@Component有几个衍生注解,我们在web开发中,会按照MVC三层架构分层

  • dao 【@Repository】
  • service 【@Service】
  • controller 【@Controller】

这四个注解的功能都是一样的,都是代表将某个类注册到spring容器中,装配bean
4、自动装配置

- @Autowired :自动装配通过类型,名字
@Qualifier(value = "xxx")
- @Nullable :字段标记了这个注解,说明这个字段可以为null
- @Resource :自动装配通过名字,类型

5、作用域

@Component
@Scope("prototype")
public class User {
    public String name;

    //相当于写了一个<property>标签
    @Value("liuyf")
    public void setName(String name) {
        this.name = name;
    }
}

6、小结
xml与注解:

  • xml更加万能,适用于任何场合,维护简单方便
  • 注解,不是自己类使用不了,维护相对复杂

xml与注解最佳实践:

  • xml用来管理bean
  • 注解只负责完成属性的注入
  • 在使用的过程中,只需要注意一个问题,必须让注解生效,就需要开启注解的支持
<!--指定要扫描的包,这个包下的注解就会生效-->
    <context:component-scan base-package="com.god.pojo"/>
    <context:annotation-config/>

使用Java的方式配置spring

完全不使用spring的XML配置,全权交给Java来做
实体类

package com.god.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class User {
    private String name;

    public String getName() {
        return name;
    }

    @Value("刘亦菲")
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}

配置文件

package com.god.config;

import com.god.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

//这个也会spring容器托管,注册到容器中,因为它本身就是一个@Component
//@Configuration代表这是一个配置类,就像我们之前看到的beans.xml
@Configuration
@ComponentScan("com.god.pojo")
@Import(MyConfig2.class)
public class MyConfig {
    //注册一个bean,就相当于我们之前写的一个注册一个bean标签
    //这个方法的名字,就相当于bean标签中的ID属性
    // 这个方法的返回值,就相当于bean标签中的class属性
    @Bean
    public User getUser(){
        return new User();    //就是返回要注入到bean的对象
    }
}

测试文件

import com.god.config.MyConfig;
import com.god.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        //如果完全使用了配置类方式去做,就只能通过AnnotationConfig上下文来获取容器,通过配置类的class对象加载
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        User getUser = (User) context.getBean("getUser");
        System.out.println(getUser.getName());
    }
}
posted @ 2026-02-21 18:42  Dominus  阅读(2)  评论(0)    收藏  举报