Spring常用注解!!!

pom.xml:

XML 复制代码
<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.12</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.19</version>
        </dependency>
    </dependencies>

applicationContext.xml:

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!--1、注意:要导入schema约束-->
<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 https://www.springframework.org/schema/context/spring-context.xsd">
    <!--扫描哪个包下的文件-->
    <context:component-scan base-package="com.by"></context:component-scan>
    <!--加载config.properties-->
    <context:property-placeholder location="config.properties"></context:property-placeholder>
</beans>

log4j.properties:

XML 复制代码
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

config.porperties:

XML 复制代码
name=小王
age=18

UserDaoImpl:

java 复制代码
package com.by.dao;


import org.springframework.stereotype.Repository;

/**
 * 持久层实现类
 */
//@Repository("userDao")
@Repository //不配置默认为类名首字母小写
public class UserDaoImpl implements UserDao {

    @Override
    public void addUser() {
        System.out.println("insert into tb_user......");
    }
}

UserServiceImpl:

java 复制代码
package com.by.service;

import com.by.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * 业务层实现类
 */
//@Service("userService")
@Service //不配置默认为类名首字母小写
//@Component
@Scope("prototype")
public class UserServiceImpl implements UserService {

    @Autowired //注入属性
    private UserDao userDao;
    @Value("${name}")
    private String name;
    @Value("${age}")
    private Integer age;

    @Override
    public void addUser() {
        System.out.println(userDao + "--" + name + "__" + age);
        userDao.addUser();
    }
}

总结:

IOC:

1.@Service:用于service层,@Service("userService")指定配置的id,@Service //不配置默认为类名首字母小写。
2.@Repository:用于dao层,@Repository("userDao")指定配置的id,@Repository //不配置默认为类名首字母小写。
3.@Component:用于配置与三层架构之外的类,也可用于其它几层,使用方法同上。
4.@Scope:用于配置bean的作用域范围,使用方法:@Scope("prototype")

DI:

1.@Autowired:用于注入属性,
@Autowired //注入属性

private UserDao userDao;

2.@Resource(name="userDao"),主要用于自定义类型

@Autowired //注入属性
private UserDao userDao;

3.@value("${}"),主要用于基本数据类型和String类型,需要在config.properties文件中声明属性,在applicationContext.xml文件中加载config.properties文件

@Value("${name}")
private String name;

相关推荐
Chase_______2 分钟前
【Java基础 | 13】IO 流(下):缓冲流、转换流、序列化与综合案例
java·开发语言
itfallrain15 分钟前
Spring 构造器循环依赖排查:@RequiredArgsConstructor + @Lazy 到底有没有生效
数据库·python·spring
bush420 分钟前
嵌入式linux学习记录十二,mmap
java·linux·学习
源码宝23 分钟前
基于SpringCloud+UniApp的智慧工地云平台整体架构设计与实现
java·人工智能·spring cloud·源码·智慧工地·云平台
天文家1 小时前
深入理解装饰器与适配器:从设计模式到 Spring AOP 的工程实践
java·设计模式
贺国亚1 小时前
Spring-AI与LangChain4j
java·人工智能·spring
野生技术架构师1 小时前
2026 Java面试宝典(春招/社招/秋招通用):没有前言,只有答案,直接开背
java·开发语言·面试
mN9B2uk171 小时前
数据库的约束简介
java·数据库·sql
AI人工智能+电脑小能手1 小时前
【大白话说Java面试题 第99题】【Mysql篇】第29题:如何选择合适的分布式主键方案?
java·数据库·分布式·mysql·面试
极光代码工作室1 小时前
基于SpringBoot的任务管理系统
java·springboot·web开发·后端开发