1. 程式人生 > >spring annotation 注入static 屬性

spring annotation 注入static 屬性

第一種方法:

@Component
public class UserUtils
{
  private static UserAccessor userAccessor;
 
  /**
   * Sets the user DAO. This method should never be called except by Spring
   * @param userAccessor The user accessor to set
   */
  @Autowired(required = true)
  public void setUserAccessor(userAccessor UserAccessor) {
    UserUtils.userAccessor = userAccessor;
  }
}

第二種方法:

@Component
public class UserUtils
{
    private static UserAccessor userAccessor;

    @Autowired(required = true)
    public UserUtils(@Qualifier(“userAccessorImpl”) userAccessor UserAccessor) {
         UserUtils.userAccessor = userAccessor;
    }
}