TODAYContext是一个轻量级依赖注入框架。
使用说明标识一个Bean使用@Component任意注解只要注解上有@Component注解就会标识为一个Bean不论多少层@Target({ElementType.TYPE,ElementType.METHOD})public@interfaceComponent{/**@returnbeanname*/String[]value()default{};/**@returnbean'sscope*/Scopescope()defaultScope.SINGLETON;String[]initMethods()default{};String[]destroyMethods()default{};}@Singleton
@Component(scope=Scope.SINGLETON)@Target({ElementType.TYPE,ElementType.METHOD})public@interfaceSingleton{//beannameString[]value()default{};String[]initMethods()default{};String[]destroyMethods()default{};}@Prototype
@Retention(RetentionPolicy.RUNTIME)@Component(scope=Scope.PROTOTYPE)@Target({ElementType.TYPE,ElementType.METHOD})public@interfacePrototype{//beannameString[]value()default{};String[]initMethods()default{};String[]destroyMethods()default{};}@Configuration
@Target(ElementType.TYPE)@Component(scope=Scope.SINGLETON)public@interfaceConfiguration{}@Service
@Component(scope=Scope.SINGLETON)@Target({ElementType.TYPE,ElementType.METHOD})public@interfaceService{String[]value()default{};//beannames}注入Bean使用@Autowired注入使用@Resource注入使用@Inject注入可自定义注解和实现PropertyValueResolver:@FunctionalInterfacepublicinterfacePropertyValueResolver{defaultbooleansupports(Fieldfield){returnfalse;}PropertyValueresolveProperty(Fieldfield)throwsContextException;}注入示例:@Controller@SuppressWarnings("serial")publicclassLoginControllerimplementsConstant,ServletContextAware{privateStringcontextPath;@AutowiredprivateUserServiceuserService;//@Inject@ResourceprivateBloggerServicebloggerService;@GET("/login")publicStringlogin(@CookieStringemail,Stringforward,Modelmodel){model.attribute(KEY_EMAIL,email);model.attribute("forward",forward);return"/login/index";}@POST("/login")@Logger(value="登录",//content="email:[${email}]"//+"passwd:[${passwd}]"//+"inputcode:[${randCode}]"//+"insession:[${randCodeInSession}]"//+"forwardto:[${forward}]"//+"msg:[${redirectModel.attribute('msg')}]"//)publicStringlogin(HttpSessionsession,@Cookie(KEY_EMAIL)StringemailInCookie,@RequestParam(required=true)Stringemail,@RequestParam(required=true)Stringpasswd,@RequestParam(required=true)StringrandCode,@RequestParam(required=false)Stringforward,@Session(RAND_CODE)StringrandCodeInSession,RedirectModelredirectModel)//{session.removeAttribute(RAND_CODE);if(!randCode.equalsIgnoreCase(randCodeInSession)){redirectModel.attribute(KEY_MSG,"验证码错误!");redirectModel.attribute(KEY_EMAIL,email);redirectModel.attribute(KEY_FORWARD,forward);returnredirectLogin(forward);}UserloginUser=userService.login(newUser().setEmail(email));if(loginUser==null){redirectModel.attribute(KEY_EMAIL,email);redirectModel.attribute(KEY_FORWARD,forward);redirectModel.attribute(KEY_MSG,email+"账号不存在!");returnredirectLogin(forward);}//
评论