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;

相关推荐
皮皮林5514 小时前
拒绝写重复代码,试试这套开源的 SpringBoot 组件,效率翻倍~
java·spring boot
IT_陈寒7 小时前
Python开发者必知的5大性能陷阱:90%的人都踩过的坑!
前端·人工智能·后端
顺风尿一寸8 小时前
从 Java NIO poll 到 Linux 内核 poll:一次系统调用的完整旅程
java
流浪克拉玛依8 小时前
Go Web 服务限流器实战:从原理到压测验证 --使用 Gin 框架 + Uber Ratelimit / 官方限流器,并通过 Vegeta 进行性能剖析
后端
程途知微8 小时前
JVM运行时数据区各区域作用与溢出原理
java
孟沐8 小时前
保姆级教程:手写三层架构 vs MyBatis-Plus
后端
星浩AI8 小时前
让模型自己写 Skills——从素材到自动生成工作流
人工智能·后端·agent
华仔啊10 小时前
为啥不用 MP 的 saveOrUpdateBatch?MySQL 一条 SQL 批量增改才是最优解
java·后端
武子康11 小时前
大数据-242 离线数仓 - DataX 实战:MySQL 全量/增量导入 HDFS + Hive 分区(离线数仓 ODS
大数据·后端·apache hive
砍材农夫11 小时前
TCP和UDP区别
后端