spring零配置Annotation笔记
程序员文章站
2022-07-08 14:40:07
...
spring提供相关的几个Annotation来标注bean先列出来
@Component:标注一个普通的spring bean
@Controller:标注一个控制器组建类如action
@Service:标注一个逻辑控制类 service层
@Repository:标识一个持久化层Dao组件类
再列几个
@Scope:表示bean作用域使用方式:Scope("prototype")
@Resource:
@Autowired:自动装配默认按照type装配,如果需要按照名称装配则需要和下面相结合
@Qualifier 针对自动装配下面展示两种写法分别表示属性修饰和set方式修饰:
@Autowired
@Qualifier("namexxx")
private XXXX xxxx;
-------------------------------------------------
@Autowired
public void setXxx(@Qualifier("namexxx") Xxx xxx)
用一个项目工程的小例子
包结构:
cn.life.routine
----action
----dao
------service
引入ContextSchema,spring配置
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd
http://www.springframework.org/schema/context <a href="http://www.springframework.org/schema/context/spring-context-3.0.xsd">http://www.springframework.org/schema/context/spring-context-3.0.xsd</a>"
>
<bean id="NoticeAction" class="cn.life.routine.action.NoticeAction" scope="prototype" />
<!--注解测试,routine-->
<context:component-scan base-package="cn.life.routine" />
</beans>
一次贴出service , dao
service接口
/**
* 注解
* @author Francis.Hu
* @createDate Oct 21, 2012
*/
public interface TestService {
/**
* 注解测试
* @return
*/
public String getTestAnnotation();
}
service 实现类
package cn.life.routine.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import cn.life.routine.dao.TestDao;
/**
* 注解测试
* @author Francis.Hu
* @createDate Oct 21, 2012
*/
@Service("testService")
public class TestServiceImp implements TestService{
/**
* 自动装配
*/
@Autowired
@Qualifier("testDao")
//@Resource(name="testDao"), 等价于<property ………… ref="testDao" />
private TestDao testDao;
public String getTestAnnotation() {
return testDao.getTestDaoAnnotation();
}
public TestDao getTestDao() {
return testDao;
}
public void setTestDao(TestDao testDao) {
this.testDao = testDao;
}
}
dao层接口
/**
* 测试注解
* @author Francis.Hu
* @createDate Oct 21, 2012
*/
public interface TestDao {
/**
* 得到dao层注解
* @return
*/
public String getTestDaoAnnotation();
}
dao层实现类
/**
* 测试注解
* @author Francis.Hu
* @createDate Oct 21, 2012
*/
@Repository("testDao")
public class TestDaoImpl implements TestDao {
public String getTestDaoAnnotation() {
return "This is testDao Annotation";
}
}
action中的调用
/**
* 测试注解
* @return
*/
@Resource(name="testService")
private TestService testService;
public String testAnnotation(){
String result = testService.getTestAnnotation();
System.out.println(result);
return SUCCESS;
}
public TestService getTestService() {
return testService;
}
public void setTestService(TestService testService) {
this.testService = testService;
}
/*********************测试结束******************************/
开发中没有绝对的xml配置或者绝对的零配置 ,只有合理的相互结合才能保证项目的可读性,高效性
上一篇: SpringMVC 跨域请求
下一篇: Spring的注解积累
推荐阅读
-
SpringCloud学习笔记(7):使用Spring Cloud Config配置中心
-
【Spring笔记】11、拆分Spring配置文件
-
spring 攻略 - 笔记- 第三章 Spring中Bean的配置(1)
-
Spring Boot 学习笔记_自动配置原理
-
【java高级进阶笔记4】之约定优于配置设计范式及Spring Boot源码剖析
-
Spring入门学习笔记(2)——基于Java的配置
-
spring零配置Annotation笔记
-
体验Spring boot 零配置开发微服务
-
Spring Boot配置 : YAML (简介+语法+配置文件值注入)+报错提示Spring Boot Configuration Annotation Processor not
-
spring boot 学习笔记(二):项目属性配置