浅谈Spring注解

本文介绍了Spring框架中常用注解的功能及使用方法,包括@Controller、@Service、@Repository、@Autowired等,并通过示例代码展示了这些注解如何在实际项目中发挥作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介:
xml和注解都是表达bean定义的载体,Spring 从2.0开始就引进了注解,个人觉得其相对xml是比较简洁。在实际开发中,往往还是混合使用,释放Spring的洪荒之力!

1首先简单介绍一下几个常用的注解

 1. @Controller:用于对Controller实现的类进行标注  
 2.  @Service:用于对Service实现的类进行标注 
 3. @Repository:用于对Repository实现的类进行标注
 4. @Autowired:@Autowired进行自动注入
 5. @Order:注解来决定Bean加载的顺序
 6...

2来来来,按惯例先P上工程再来解释

这里写图片描述

模拟一项目,首先控制层接受到前台页面的某一请求(超链接,提交表单等),从页面传到了后台代码,紧接着就是业务逻辑层处理请求,再通过dao层,将数据持久化

Dao层代码(PeopleDao.java)

package com.wby.dao;
import org.springframework.stereotype.Repository;
@Repository("peopleDao")
/*
   @Repository:用于对Repository实现的类进行标注
*/
public class PeopleDao {
    public void save(){
        System.out.println("Dao 层save方法");
    }
}

业务层代码(Peopleservice.java)

package com.wby.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import com.wby.dao.PeopleDao;
@Service("peopleService")
/*
 @Service:用于对Service实现的类进行标注
*/
public class PeopleService {
    @Autowired
    PeopleDao peopleDao;

    public void addPeople(){
        System.out.println("添加");
        peopleDao.save();
    }

}

控制层(PeopleAction.java)

package com.wby.action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.wby.service.PeopleService;
@Controller("peopleAction")
/*
   @Controller:用于对Controller实现的类进行标注
*/
public class PeopleAction {
   @Autowired
   PeopleService peopleService;
   public void excute() {
       System.out.println("接受某一请求");
       peopleService.addPeople();

}
}

Test.java

package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wby.action.PeopleAction;
public class Test {
    public static void main(String[] args) {
           //创建ioc容器
           ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
           PeopleAction peopleAction = (PeopleAction)app.getBean("peopleAction");
           peopleAction.excute();      
    }

}

applicationContext.xml

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

    <!-- 配置自动扫描的包  注解起作用-->
    <context:component-scan base-package="com.wby"></context:component-scan>
</beans>

运行结果:

   接受某一请求
   添加
   Dao 层save方法

上述过程纯属模拟,过段时间我有空再p上整个完整的,嘻嘻。啊?说好浅谈注解的呢。hahaha,来啦。。。

 1 @Controller   @Service标注   @Repository这三个默认的value值默认为类名第一个字母小写,也可指定
  @Repository("peopleDao1")//指定名字为 该bean 在ioc 容器的标志为peopleDao1 等价于Xml中
 <bean id="peopleDao1" class="类的全路径"/>
 2
  @Autowired(类中的变量或者方法入参)
  @Autowired(required=false)
  PeopleDao peopleDao;时,当PeopleDao在ioc容器不存在时,也不会报错
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值