Guice依赖注入(@Inject)
2017-05-22 14:47
232 查看
package com.ilucky.guice.test3; import com.google.inject.Guice; import com.google.inject.Injector; /** * v1.0:20161114 * 注解:依赖注入@Inject * @author IluckySi */ public class MainTest { public static void main(String[] args) { Injector injector = Guice.createInjector(); Person person = injector.getInstance(Person.class); person.diplayInfo(); } } /** Mobile:[Mobile: 988438434] Laptop:[Laptop: HP 323233232,$545034] */
package com.ilucky.guice.test3; import com.google.inject.Inject; public class Person { private Mobile mobile; private Laptop laptop; @Inject public Person(Mobile mobile, Laptop laptop) { this.mobile = mobile; this.laptop = laptop; } public void diplayInfo() { System.out.println("Mobile:" + mobile); System.out.println("Laptop:" + laptop); } }
package com.ilucky.guice.test3; public class Laptop { private String model; private String price; public Laptop() { this.model = "HP 323233232"; this.price = "$545034"; } public String toString() { return "[Laptop: " + model + "," + price + "]"; } }
package com.ilucky.guice.test3; public class Mobile { private String number; public Mobile() { this.number = "988438434"; } public String toString() { return "[Mobile: " + number + "]"; } }
相关文章推荐
- 依赖注入及Guice简述
- Guice 学习(六)使用Provider注入服务( Provider Inject Service)
- Guice 学习(二)构造器注入(Constructor Inject)
- Guice 学习(三)Setter注入(Setter Inject)
- Guice lightweigh dependency inject
- Guice 学习(四)基本属性注入(Field Inject)
- Guice 学习(六)使用Provider注入服务( Provider Inject Service)
- Guice 学习(七)常量和属性的注入( Constant and Property Inject)
- com.google.inject.CreationException: Guice creation errors
- Spring的核心机制——依赖注入(Dependency Inject)
- 依赖注入与JSR-330的参考实现——Guice
- google guice @inject comments
- 代码注入(Inject your code to a Portable Executable file)
- Google发布Guice: Java IOC 框架. 挑战Spring!
- 依赖注入(Dependency Injection)模式的特点分析与实现
- Spring 2.5 Perfomance Improvements 200% 直逼 Guice
- Guice vs Dependency Injection By Hand
- 通过 Guice 进行依赖项注入
- Guice的scope
- Spring.NET教程(一)——控制反转(依赖注入)(基础篇)(转)