注解 @Resource与@Autowired与@Component的使用

当自动注入的时候如果注入的类是当前类的静态变量则只能通过在构造器上面应用@Autowired进行自动注入,实例代码如下:

@Component
public class FileNameGetUtils {
    //文件服务
    public static FileServerHelper fileServerHelper;
    @Autowired
    public FileNameGetUtils(FileServerHelper fileServerHelper) {
        FileNameGetUtils.fileServerHelper = fileServerHelper;
    }
    /**
     * 通过文件唯一号获取文件名称
     * @param fileUniqNo 文件唯一号
     * @return 文件名称
     */
    public static String getFileName(String fileUniqNo){
        //获取文件信息
        FileServerResponseDTO fileServerResponseDTO =fileServerHelper.getFileInfo(fileUniqNo);
        //解析文件信息以获取导入文件名
        FileServerObject fileServerObject =null;
        if(Objects.nonNull(fileServerResponseDTO)){
            fileServerObject= JSON.parseObject(fileServerResponseDTO.getData(),FileServerObject.class);
        }
        if(Objects.isNull(fileServerObject)){
            return null;
        }
        return fileServerObject.getFileName();
    }
}

 在java代码中使用@Autowired或@Resource注解方式进行装配,这两个注解的区别是:@Autowired 默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。    

@Autowired    private PersonDao  personDao;//用于字段上    

@Autowired    public void setOrderDao(OrderDao orderDao) {//用于属性的setter方法上       

 this.orderDao = orderDao;    }

​@Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false。如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:    @Autowired  @Qualifier("personDaoBean")    private PersonDao  personDao;@Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上,但它默认按名称装配。名称可以通过@Resource的name属性指定,如果没有指定name属性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象。    @Resource(name=“personDaoBean”)    private PersonDao  personDao;//用于字段上注意:如果没有指定name属性,并且按照默认的名称仍然找不到依赖对象时, @Resource注解会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。  步骤:(1).修改beans.xmlJava代码 在java代码中使用@Autowired或@Resource注解方式进行装配。但我们需要在xml配置文件中配置以下信息:         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-2.5.xsd             http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context-2.5.xsd">                这个配置隐式注册了多个对注释进行解析处理的处理器:AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor      注: @Resource注解在spring安装目录的lib\j2ee\common-annotations.jar      必不可少

​Spring注解@Component和@resource的使用

  @Component(“”)和@resource(name=””)的使用:

spring的配置文件配置:

配置解析:

使用ClassPathXmlApplicationContext("bean.xml")初始化bean.xml的时候,或者说初始化spring配置文件的时候,会自动扫描com.test下的所有包,若发现有@component,则将该类初始化为一个对象,对象的key为@component("name")中指明的name,若不指明,则默认为类的名字首字母小写;当调用ctx.getBean("userService"),时,则查看容器是否有一个名字为userService;如有则在初始化这个类得过程中,如果发现@Resource(name=”u”);则查看容器是否有名字叫u的bean,若有则将u注入到方法参数中,而后参数就会传人方法内,当然也就注入了成员变量里:

UserService.java文件中使用:

@Component("userService")

public class UserService {

    private UserDAO userDAO = new UserDAOImpl();

    private UserLogDAO userLogDAO;

    public UserDAO getUserDAO() {

       return userDAO;

    }

    public UserLogDAO getUserLogDAO() {

       return userLogDAO;

    }

    @Resource(name="ulog")

    public void setUserLogDAO(UserLogDAO userLogDAO) {

       this.userLogDAO = userLogDAO;

    }

    @Resource(name="u")

    public void setUserDAO(UserDAO userDAO) {

       this.userDAO = userDAO;

    }

 

UserDAOImpl.java中使用:

  @Component("u")

public class UserDAOImpl implements UserDAO {

    private SessionFactory  sessionFactory;

   

    public SessionFactory getSessionFactory() {

       return sessionFactory;

    }

    @Resource

    public void setSessionFactory(SessionFactory sessionFactory) {

       this.sessionFactory = sessionFactory;

    }

    @Override

    public void save(User u) {

           Session s=sessionFactory.openSession();//.getCurrentSession();

           //s.beginTransaction();

           s.save(u);

       System.out.println("a user save,XXXX!");

    }

}

 
使用上述的@Component和@resource注解,相当于spring使用了如下的配置:

  这样就可以省去之前在bean中所写的:

猜你喜欢

转载自blog.csdn.net/qq_39621802/article/details/79500108