Spring 注解
来源:互联网 发布:yy笑声软件下载 编辑:程序博客网 时间:2024/06/03 20:45
Spring注解@Component、@Repository、@Service、@Controller区别
所以,如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层(dao层)、业务层(seriver层)和控制层(controller层)分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释,而用 @Component 对那些比较中立的类进行注释。
在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找以及维护起来也不太方便。 Spring2.5为我们引入了组件自动扫描机制,他可以在类路径底下寻找标注了@Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。它的作用和在xml文件中使用bean节点配置组件时一样的。要使用自动扫描机制,我们需要打开以下配置信息:
<?xml version="1.0" encoding="UTF-8" ?> <beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/X
MLSchema-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-2.5.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd" > <context:component-scan base-package=”com.eric.spring”> </beans> 其中base-package为需要扫描的包(含所有子包) @Service用于标注业务层组件,@Controller用于标注控制层组件(如struts中的action),
@Repository用于标注数据访问组件,即DAO组件,而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Service
public class VentorServiceImpl implements iVentorService {
}
@Repository
public class VentorDaoImpl implements iVentorDao {
}
使用的时候只需要用Autowired标注就可以了
@Controller("dtforeDdosController")@RequestMapping("/dtforce/defense/ddos")public class DdosController extends BaseController{ private static final String CLASSNAME = "DdosController"; private static final String DEPARTMENT = "department"; private static final String RESULT = " result: "; @Autowired private LogService logService; @Autowired private UserInfo userinfo; @Autowired private DdosService ddosService; @Autowired private RegionService regionService;
Spring中@Autowired注解、@Resource注解的区别
@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。1、 @Autowired与@Resource都可以用来装配bean.都可以写在字段上,或写在setter方法上。
2、 @Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设置它的required属性为false,如:@Autowired(required=false),如果我们想使用名称装配可以结合@Qualifier注解进行使用,如下:
@Autowired()
@Qualifier("baseDao")
2
private
BaseDao baseDao;
3、@Resource(这个注解属于J2EE的),默认安装名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名进行安装名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。
@Resource(name="baseDao")
2
private
BaseDao baseDao;
- spring注解--组合注解
- Spring注解
- Spring注解
- Spring注解
- Spring注解
- Spring注解
- Spring 注解
- Spring注解
- Spring注解
- Spring注解
- spring 注解
- spring 注解
- spring注解
- Spring 注解
- Spring 注解
- spring 注解
- spring 注解
- spring 注解
- 钛极OS(TiJOS)物联网操作系统之小试牛刀(2)——超声波测距
- 漂亮的UI控件
- zookeeper 安装 windows环境
- 解决-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable
- JEECMS——安全框架Shiro
- Spring 注解
- Spring(2)—IOC
- java-StringBuffer(字符串缓冲类)
- SafetyNet简要梳理
- iOS 加载Gif图片
- css实现div高度根据自适应宽度(百分比)调整
- eclipse或者sts安装svn插件()
- 安装 suds 出现 问题
- 【Java】【JVM】内存分配机制