Spring5.X的注解配置项目

pom.xml

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>net.cybcclass</groupId>
    <artifactId>cyb_spring</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.5</version>
        </dependency>
    </dependencies>
</project>
复制代码

VideoService.java

package net.cybclass.sp.servicce;

import net.cybclass.sp.domain.Video;

public interface VideoService {
    int save(Video video);
    Video findById(int id);
}

VideoServiceImpl.java

复制代码
package net.cybclass.sp.servicce;

import net.cybclass.sp.domain.Video;
import org.springframework.stereotype.Component;

//@Component("videoService") //相当于配置bean的id
@Component() //相当于配置bean的id
public class VideoServiceImpl implements VideoService{
    public int save(Video video) {
        System.out.println("保存Video");
        return 0;
    }

    public Video findById(int id) {
        System.out.println("根据id找视频");
        return new Video();
    }
}
复制代码

在实现类上加@Component,若没有取名字的话,默认是,类名首字母小写

演示

 

posted @   陈彦斌  阅读(343)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示
主题色彩