Guice 学习(七)常量和属性的注入( Constant and Property Inject)
1、常量注入方式
package com.guice.ConstantInjectDemo;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Module;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
/*
* 绑定常量
*/
public class ConstantInjectDemo {
@Inject
@Named("a")
private int a;
public static void main(String[] args) {
ConstantInjectDemo instance = Guice.createInjector(new Module() {
@Override
public void configure(Binder binder) {
// 将数值22绑定到常量值a上
binder.bindConstant().annotatedWith(Names.named("a")).to(22);
}
}).getInstance(ConstantInjectDemo.class);
System.out.println(instance.a);// 22
}
}
除了能够绑定int外。在ConstantBindingBuilder类中还能够绑定其他的基本类型。
com.google.inject.binder.ConstantBindingBuilder.to(String)
com.google.inject.binder.ConstantBindingBuilder.to(long)
com.google.inject.binder.ConstantBindingBuilder.to(boolean)
com.google.inject.binder.ConstantBindingBuilder.to(double)
com.google.inject.binder.ConstantBindingBuilder.to(float)
com.google.inject.binder.ConstantBindingBuilder.to(short)
com.google.inject.binder.ConstantBindingBuilder.to(char)
2、属性注入方式
package com.guice.ConstantInjectDemo;
import java.util.Properties;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Module;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
/**
* TODO : 绑定Properties 除了能够绑定基本类型外。还能够绑定一个Properties到Guice中。当然了,因为Properties本质上时一个Map<String,String>, 因此Guice也同意绑定一个Map<String,String>。
*
* @author E468380
*/
public class PropertiesInjectDemo {
@Inject
@Named("csdn")
private String csdn;
public static void main(String[] args) {
PropertiesInjectDemo instance = Guice.createInjector(new Module() {
@Override
public void configure(Binder binder) {
Properties properties = new Properties();
properties.setProperty("csdn", "www.csdn.com");
Names.bindProperties(binder, properties);
}
}).getInstance(PropertiesInjectDemo.class);
System.out.println(instance.csdn);
}
}
Guice 学习(七)常量和属性的注入( Constant and Property Inject)的更多相关文章
- Guice 学习(六)使用Provider注入服务( Provider Inject Service)
1.定义接口 package com.guice.providerInject; import com.google.inject.ProvidedBy; public interface Servi ...
- Spring学习笔记之 Spring IOC容器(一)之 实例化容器,创建JavaBean对象,控制Bean实例化,setter方式注入,依赖属性的注入,自动装配功能实现自动属性注入
本节主要内容: 1.实例化Spring容器示例 2.利用Spring容器创建JavaBean对象 3.如何控制Bean实例化 4.利用Spring实现bean属性sett ...
- Java开发学习(七)----DI依赖注入之自动装配与集合注入
一.自动配置 上一篇博客花了大量的时间把Spring的注入去学习了下,总结起来就两个字麻烦.麻烦在配置文件的编写配置上.那有更简单方式么?有,自动配置 1.1 依赖自动装配 IoC容器根据bean所依 ...
- Spring学习(三)几种集合属性的注入方式
1.前言 众所周知.java中不只有八大简单类型.还有一些集合类型.本文围绕集合类型的注入做一个总结. 2.项目骨架 3.过程 1.创建实体类AllCollectionType package com ...
- Angular4学习笔记(四)- 依赖注入
概念 依赖注入是一种设计思想,并不是某一类语言所特有的,因此可以参考开涛大神关于学习Java语言的Spring框架时对其的解释: DI-Dependency Injection,即"依赖注入 ...
- Quartz定时任务学习(二)web应用/Quartz定时任务学习(三)属性文件和jar
web中使用Quartz 1.首先在web.xml文件中加入 如下内容(根据自己情况设定) 在web.xml中添加QuartzInitializerServlet,Quartz为能够在web应用中使用 ...
- Guice学习(一)
Guice学习(一) Guice是Google开发的一个轻量级依赖注入框架(IOC).Guice非常小而且快,功能类似与Spring,但效率上网上文档显示是它的100倍,而且还提供对Servlet,A ...
- spring 运行时属性值注入
继续spring学习,今天介绍两种外部属性值注入的方式.当你需要读取配置信息时,可以快速读取. 开始之前先创建属性文件site.properties,放在classpath下面 #数据库配置 ### ...
- MyBatis学习七:spring和MyBatis整合
<\mybatis\day02\16mybatis和spring整合-sqlSessionFactory配置.avi;> MyBatis学习七:spring和MyBatis整合.逆向工程 ...
随机推荐
- Xamarin for Visual Studio 3.11.590 稳定版 破解补丁 Version 3
前提概要 全新安装请参考 安装 Xamarin for Visual Studio. Release Log 3.11.590 此版本是紧急修复(HotFix)版,重点改善了 build-tool 及 ...
- (1)oracle安装、卸载、启动、关闭、登陆以及同时遇到的问题
数据库概念 在oracle里数据库是一个静态的概念,数据库的资料保存在硬盘上,一个数据库可以有多个实例 数据库实例 数据库实例是一个动态的概念,它是进程+这个进程的内存块.就把它当成个指针吧,这个指针 ...
- java.sql.SQLException: Access denied for user 'roo'@'localhost' (using password: YES)
初学mysql,安装了mysql8.0.11,激动的用jdbc连接数据库,出现error,折腾了三天依旧无解,最后无奈装了比较稳定的mysql5.5,问题得以解决,很迷,但只要error没了就开心. ...
- GIF工具 | 分享几个Gif相关工具
文章目录 LICEcap GiFResizer LICEcap LICEcap 是一款屏幕录制工具,支持导出GIF动画图片格式,轻量级.使用简单,录制过程中可以随意改变录屏范围. 下载地址: 官方地址 ...
- 随机取若干条记录的SQL语句
原文:随机取若干条记录的SQL语句 MySql中随机提取数据库N条记录 select * from TableName order by rand() limit N SQLServer中随机提取 ...
- SQL Reverse函数
原文:SQL Reverse函数 Sql sever里面有个自带的reverse函数,这个函数的主要功能是把一个字符产反转.比如对于: select REVERSE('hello,world') 将得 ...
- redis--AOF
Redis 分别提供了 RDB 和 AOF 两种持久化机制: RDB 将数据库的快照( snapshot)以二进制的方式保存到磁盘中. 相当于MySQL binlog 的 raw模式 AOF 则以协议 ...
- 【Linux】CentOS7 alien命令 转化deb 与 rpm的相互转化
-d, --to-deb Make debian packages. This is the default. -r, --to-rpm Make rpm packages. -t, --to-tgz ...
- oc的插件
umbra https://umbra3d.com/ 很不错
- 泳池水面fresnel 的近似替代
vs float4 ep = TBMultiply(ModelViewMatrix, FinalPosition); DistFromEye.x = TBSaturate( 10.0 + ep.z / ...