浅谈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容器不存在时,也不会报错
阅读全文
1 0
- Spring @Resource注解浅谈
- 浅谈Spring注解
- 浅谈spring 注解之@Autowired 与@Resource
- 浅谈Spring框架注解的用法分析
- 浅谈 Spring 框架注解的用法分析
- 浅谈spring中@Conditional(条件注解)
- 浅谈 Spring 框架注解的用法分析
- 浅谈spring——注解配置(九)
- Spring框架注入注解与拦截器浅谈
- 【浅谈JavaEE框架】Spring常用注解,自动扫描装配Bean
- 干货:浅谈Spring框架注解的用法分析
- Spring AOP浅谈及使用注解方法测试
- spring注解--组合注解
- Spring注解
- Spring注解
- Spring注解
- Spring注解
- Spring注解
- anaconda3.6 + opencv3.3 + windows10 配置
- MySql安装与使用图文教程【推荐】
- sql常用语句
- MyBatis之自查询使用递归实现 N级联动效果(两种实现方式)
- Python学习_我该怎么使用字典
- 浅谈Spring注解
- LeetCode.55 Jump Game
- 机器学习入门--MNIST(一)
- IBM服务器引导盘serverguide 下载
- Somethings about the coding in Python
- UBoot:ENTRY等宏的展开,CPSR寄存器的设置(stat.S)
- sql排序问题
- Android音频子系统,音频流的回放(四)
- 【Java】【线程同步】sleep,join,yield,synchronized,wait,notify