Java:JSR330:修订间差异
imported>Soleverlee |
imported>Soleverlee |
||
(未显示同一用户的1个中间版本) | |||
第27行: | 第27行: | ||
=@Named= | =@Named= | ||
<source lang="java"> | |||
@Named("cash") | |||
public class CashPaymentImpl implements Payment { | |||
private static final Logger LOGGER = Logger.getLogger(CashPaymentImpl.class.toString()); | |||
@Override | |||
public void pay(BigDecimal amount) { | |||
LOGGER.log(Level.INFO, "payed {0} cash", amount.toString()); | |||
} | |||
} | |||
@Named("visa") | |||
public class VisaPaymentImpl implements Payment { | |||
private static final Logger LOGGER = Logger.getLogger(VisaPaymentImpl.class.toString()); | |||
@Override | |||
public void pay(BigDecimal amount) { | |||
LOGGER.log(Level.INFO, "payed {0} with visa", amount.toString()); | |||
} | |||
} | |||
@Inject private @Named("visa") Payment payment; | |||
</source> | |||
=@Singleton= | =@Singleton= | ||
=@Qualifier= | =@Qualifier= | ||
<source lang="java"> | |||
@java.lang.annotation.Documented | |||
@java.lang.annotation.Retention(RetentionPolicy.RUNTIME) | |||
@javax.inject.Qualifier | |||
public @interface CashPayment {} | |||
@java.lang.annotation.Documented | |||
@java.lang.annotation.Retention(RetentionPolicy.RUNTIME) | |||
@javax.inject.Qualifier | |||
public @interface VisaPayment {} | |||
@CashPayment | |||
public class CashPaymentImpl implements Payment { | |||
... | |||
} | |||
@VisaPayment | |||
public class VisaPaymentImpl implements Payment { | |||
... | |||
} | |||
@Inject private @VisaPayment Payment payment; | |||
</source> | |||
=@Scope= | =@Scope= | ||
=Provider<T>= | =Provider<T>= | ||
[[Category:Programe]] | [[Category:Programe]] |
2016年11月28日 (一) 01:56的最新版本
JSR330定义了Java依赖注入的标准。主要提供了一系列的定义(javax.inject.*):
- Provider<T> Provides instances of T.
- Inject Identifies injectable constructors, methods, and fields.
- Named String-based qualifier.
- Qualifier Identifies qualifier annotations.
- Scope Identifies scope annotations.
- Singleton Identifies a type that the injector only instantiates once.
@Inject
see this post
public interface Payment {
void pay(BigDecimal amount);
}
public class CashPaymentImpl implements Payment {
private static final Logger LOGGER = Logger.getLogger(CashPaymentImpl.class.toString());
@Override
public void pay(BigDecimal amount) {
LOGGER.log(Level.INFO, "payed {0} cash", amount.toString());
}
}
@Inject private Payment payment;
@Named
@Named("cash")
public class CashPaymentImpl implements Payment {
private static final Logger LOGGER = Logger.getLogger(CashPaymentImpl.class.toString());
@Override
public void pay(BigDecimal amount) {
LOGGER.log(Level.INFO, "payed {0} cash", amount.toString());
}
}
@Named("visa")
public class VisaPaymentImpl implements Payment {
private static final Logger LOGGER = Logger.getLogger(VisaPaymentImpl.class.toString());
@Override
public void pay(BigDecimal amount) {
LOGGER.log(Level.INFO, "payed {0} with visa", amount.toString());
}
}
@Inject private @Named("visa") Payment payment;
@Singleton
@Qualifier
@java.lang.annotation.Documented
@java.lang.annotation.Retention(RetentionPolicy.RUNTIME)
@javax.inject.Qualifier
public @interface CashPayment {}
@java.lang.annotation.Documented
@java.lang.annotation.Retention(RetentionPolicy.RUNTIME)
@javax.inject.Qualifier
public @interface VisaPayment {}
@CashPayment
public class CashPaymentImpl implements Payment {
...
}
@VisaPayment
public class VisaPaymentImpl implements Payment {
...
}
@Inject private @VisaPayment Payment payment;