Spring 学习2--Spring特殊语义注释类定义bean

来源:互联网 发布:sql注入防范 编辑:程序博客网 时间:2024/06/04 19:43

        在使用Spring框架2.5以后的版本开发Web项目时,经常会遇到@repository、@service、@controller等注释类。从注释类的命名上,很容易看出这几个注释类分别与持久层、业务层、控制层(web)相对应,分别标记对应层的一个组件。这几个注解是当你需要定义某个类为一个bean,则在这个类的类名前一行使用@service("名称"),就相当于将该类定义为一个bean,bean命名为"名称";当定义的类为基类时,可以定义名称,也可以不定义,不定义会默认以类名为bean的命名(类首字母小写)

         而Spring还提供了一个@component注释类用来标记一个组件,目前看来@repository、@service、@controller这三个注解与@component注解对比并没有什么太大的不同,相当于等效的,但Spring将在以后的版本中为它们添加特殊的功能。所以,如果Web应用程序采用了三层架构的话,最好在各层分布采用对应的注解进行注释。

        @Service用于标注业务层组件(我们通常定义的service层就用这个)

        @Controller用于标注控制层组件(如struts的action、springmvc的controller)

        @Repository用于标注数据访问组件(dao)

        @Component泛指组件,当组件不好归类的时候,我们可以使用这个进行标注

        在一个稍大的项目中,如果组件采用XML的bean定义来配置,显然会增加配置文件的体积,查找以及维护起来相当不方便。Spring2.5为我们引入了组件自动扫描机制,它在类路径下寻找上述注解的类,并把这些类纳入Spring容器中管理。它的作用和在XML文件中使用bean节点配置组件时一样的。

        要使用自动扫描机制,我们需要打开以下配置信息: 

 

<?xmlversion="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 加载配置文件-->
   <context:property-placeholderlocation="classpath:config.properties"/>
    <!-- 扫描service自动注入为bean-->
    <context:component-scan
           base-package="org.fsdcic.visua.service.impl,
            org.fsdcic.visua.dao.impl,
            org.fsdcic.visua.utils,
           org.fsdcic.visua.annotation"/>

</beans>

   component-scan标签默认情况下自动扫描指定路径下的包(含所有子包),将带有@Component、@Repository、@Service、@Controller标签的类自动注册到Spring容器。对标记了Spring的@Required、@Autowired,JSR250的@PostConstruct、@PreDestroy、@Resource,JAX-WS的@WebServiceRef,EJB的@EJB,JPA的@PersistenceContext、@PersistenceUnit等注解的类进行对应的操作使注解生效(包含了annotation-config标签的作用)。

    getBean的默认名称是类名(首字母小写),如果想自定义,可以@Service("名称")这样来指定。这中bean默认的"Singleton(单例)",如果想改变,可以使用@Scope("prototype")来改变。

注入方式:

三层架构:

Web:

package com.ulewo.ioc;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

@Controller

public class IocAction {

    @Autowired

    private IocService service;

    public void add(){

        service.add();

    }

}

service层:(service就直接定义类了,没有定义接口,定义接口也是一样的)

package com.ulewo.ioc;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

@Service

public class IocService {

    @Resource

    private IIocDao iocDao;

    public void add(){

        iocDao.add();

    }

}

Dao层

定义一个接口

package com.ulewo.ioc;

public interface IIocDao {

    public void add();

}

实现类:

package com.ulewo.ioc;

import org.springframework.stereotype.Repository;

@Repository

public class IocDao implements IIocDao{

    public void add(){

        System.out.println("调用了dao");

    }

}

当接口存在两个实现类的时候必须使用@Qualifier指定注入哪个实现类,否则可以省略,只写@Autowired。  

0 0
原创粉丝点击