浅谈Spring注解

来源:互联网 发布:java try的用法 编辑:程序博客网 时间:2024/06/06 00:51

简介:
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容器不存在时,也不会报错
原创粉丝点击