Spring使用注解开发

Spring使用注解开发

在Spring4之后,要使用注解开发,必须要保证AOP的包导入了

bean如何注解

约束配置

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
		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:component-scan base-package="com.cike6"/>
    <context:annotation-config/>


</beans>

要注解的类

java 复制代码
// 等价于  <bean id="user" class="com.cike6.dao.User"/>
// @Component 组件

@Component
public class User {
    public String name="cike_y";

}

测试方法

java 复制代码
public class spring_6_Test {
    @Test
    public  void  test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user.name);
    }
}

属性如何注入

在对应属性上面加一个@Value 注解即可

java 复制代码
@Component
public class User {
    // 相当于 <property name="name" value="cike_y"/>
    @Value("cike_y")
    public String name;

}

衍生的注解

@Component 有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!都是组件的意思,只是名字不一样为了划分,功能一样

dao【@Repository】

复制代码
- 一般dao层都会用这个注解

service【@Service】

复制代码
- service层用这个注解

controller【@Controller】

复制代码
- 控制层用这个注解
controller的案例
  • 如果找不到controller的容器id,我们还可以指定注解显示定义一个容器id名

使用bean注解的类,并且属性注入的注解方式

java 复制代码
@Controller("usercontroller")
public class UserController {
    @Value("aa")
    public String name;

}

注解约束的xml配置

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
		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:component-scan base-package="com.cike6"/>
    <context:annotation-config/>


</beans>

测试方法并且实例化容器id

java 复制代码
@Test
public  void  test2(){
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserController usercontroller = context.getBean("usercontroller", UserController.class);
    System.out.println(usercontroller.name);
}

可以看见成功输出 UserController类下的属性值name

这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配Bean

自动装配

plain 复制代码
@Autowired:自动装配通过类型。名字
  如果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value="xx")
@Nullable 字段标记了这个注解,说明这个字段可以null
@Resource  自动装配通过名字、类型

作用域

java 复制代码
@Component
@Scope("singleton") // 单例模式
 // 原型模式 @Scope("prototype")
public class User {
    // 相当于 <property name="name" value="cike_y"/>
    @Value("cike_y")
    public String name;

}

小结

xml与注解:

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

xml与注解的最佳实践:

  • xml用来管理bean;
  • 注解之复杂完成属性的注入
  • 我们在使用的过程中,只需要注意一个问题:必须要注解生效就需要开启注解的支持、扫描包
xml 复制代码
<context:component-scan base-package="com.cike6"/>
<context:annotation-config/>
相关推荐
希望永不加班1 分钟前
SpringBoot 主启动类解释:@SpringBootApplication 到底做了什么
java·spring boot·后端·spring
一只叫煤球的猫4 分钟前
为什么不用 RAG 做记忆系统 ——压缩上下文与 memory.md 的架构选择
人工智能·后端·ai编程
智能工业品检测-奇妙智能10 分钟前
国产化系统的性价比对比
人工智能·spring boot·后端·openclaw·奇妙智能
编码忘我21 分钟前
java强引用、软引用、弱引用、虚引用
后端
蝎子莱莱爱打怪29 分钟前
别再裸用 Claude Code 了!32 个亲测Skills + 8 个 MCP,开发效率直接拉满!
java·后端·claude
犯困的饭团32 分钟前
4_【自动化引擎Ansible Runner】将 Runner 嵌入灵魂 - Python API 编程
后端
AI茶水间管理员35 分钟前
爆火的OpenClaw到底强在哪?一文了解核心架构(附一条消息的全链路流程)
人工智能·后端
Java水解35 分钟前
Rust异步缓存系统的设计与实现
后端·rust
野犬寒鸦43 分钟前
JVM垃圾回收机制面试常问问题及详解
java·服务器·开发语言·jvm·后端·算法·面试
用户908324602731 小时前
Spring AI + RAG + SSE 实现带搜索来源的智能问答完整方案
前端·后端