1. Spring is lightweight= minimal impact
2. Spring DI= JavaBeans +interfaces
3.JSR-330=Dependency Injection for Java
4.JSR-303= Bean Validation API specification
5.
props = new Properties(); props.load(new FileInputStream(“ch2/src/conf/msf.properties”)); String rendererClass = props.getProperty(“renderer.class”);6.Injection vs. Lookup
7.<context:annotation-config>tag tells Spring to scan the codebase for dependency requirements.because of <context:annotation-config>tag,during the initialization of Spring’s ApplicationContext, Spring will discover those @Autowired annotations and inject the dependency (discovered via the <context:component-scan>tag) as required.
8.
@Value("John Smith")
private String name;
9.SpringEL
public class InjectSimpleConfig { private String name = "John Smith"; private int age = 35; private float height = 1.78f; private boolean programmer = true; private Long ageInSeconds = 1103760000L; // Getter/setter method omitted }
<bean id="injectSimpleConfig" class="com.apress.prospring3.ch4.xml.InjectSimpleConfig"/> <bean id="injectSimpleSpel" class="com.apress.prospring3.ch4.xml.InjectSimpleSpel"> <property name="name"> <value>#{injectSimpleConfig.name}</value> </property> <property name="age"> <value>#{injectSimpleConfig.age + 1}</value> </property> <property name="height"> <value>#{injectSimpleConfig.height}</value> </property> <property name="isProgrammer"> <value>#{injectSimpleConfig.programmer}</value> </property> <property name="ageInSeconds"> <value>#{injectSimpleConfig.ageInSeconds}</value> </property> </bean>
@Service("injectSimpleSpel") public class InjectSimpleSpel { @Value("#{injectSimpleConfig.name}") private String name; @Value("#{injectSimpleConfig.age + 1}") private int age; @Value("#{injectSimpleConfig.height}") private float height; @Value("#{injectSimpleConfig.programmer}") private boolean programmer; @Value("#{injectSimpleConfig.ageInSeconds}") private Long ageInSeconds; // Other codes omitted }10.Using Collections for Injection
public class CollectionInjection { private Map<String, Object> map; private Properties props; private Set set; private List list; public static void main(String[] args) { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); ctx.load("classpath:app-context-xml.xml"); ctx.refresh(); CollectionInjection instance = (CollectionInjection) ctx.getBean("injectCollection"); instance.displayInfo(); } public void setList(List list) { this.list = list; } public void setSet(Set set) { this.set = set; } public void setMap(Map <String, Object> map) { this.map = map; } public void setProps(Properties props) { this.props = props; } public void displayInfo() { // display the Map System.out.println("Map contents:\n"); for (Map.Entry<String, Object> entry: map.entrySet()) { System.out.println("Key: " + entry.getKey() + " - Value: " + entry.getValue()); } // display the properties System.out.println("\nProperties contents:\n"); for (Map.Entry<Object, Object> entry: props.entrySet()) { System.out.println("Key: " + entry.getKey() + " - Value: " + entry.getValue()); } // display the set System.out.println("\nSet contents:\n"); for (Object obj: set) { System.out.println("Value: " + obj); } // display the list System.out.println("\nList contents:\n"); for (Object obj: list) { System.out.println("Value: " + obj); } } }
<bean id="oracle" name="wiseworm" class="com.apress.prospring3.ch4.BookwormOracle"/> <bean id="injectCollection" class="com.apress.prospring3.ch4.xml.CollectionInjection"> <property name="map"> <map> <entry key="someValue"> <value>Hello World!</value> </entry> <entry key="someBean"> <ref local="oracle"/> </entry> </map> </property> <property name="props"> <props> <prop key="firstName">Clarence</prop> <prop key="secondName">Ho</prop> </props> </property> <property name="set"> <set> <value>Hello World!</value> <ref local="oracle"/> </set> </property> <property name="list"> <list> <value>Hello World!</value> <ref local="oracle"/> </list> </property> </bean>////////////////////////////////////////////////////////////////////////////////////////////////////////////
<util:map id="map" map-class="java.util.HashMap"> <entry key="someValue"> <value>Hello World!</value> </entry> <entry key="someBean"> <ref bean="oracle"/> </entry> </util:map> <util:properties id="props"> <prop key="firstName">Clarence</prop> <prop key="secondName">Ho</prop> </util:properties> <util:set id="set"> <value>Hello World!</value> <ref bean="oracle"/> </util:set> <util:list id="list"> <value>Hello World!</value> <ref bean="oracle"/> </util:list>
@Service("injectCollection") public class CollectionInjection { @Resource(name="map") private Map<String, Object> map; @Resource(name="props") private Properties props; @Resource(name="set") private Set set; @Resource(name="list") private List list; // Other codes omitted }11.Bean Scopes
Singleton:The default singleton scope.
Prototype:A new instance will be created by Spring when requested by application.
Request:For web application use. When using Spring MVC for web application,
beans with request scope will be instantiated for every HTTP request and then
destroyed when the request is completed.
Session:For web application use. When using Spring MVC for web applications,
beans with session scope will be instantiated for every HTTP session and then
destroyed when the session is over.
Global session:For portlet-based web applications. The global session scope beans
can be shared among all portlets withinthe same Spring MVC–powered portal
application.
Thread: A new bean instance will be created by Spring when requested by a new
thread, while for the same thread, the same bean instance will be returned. Note
that this scope is not registered by default.
Custom:Custom bean scope that can be created by implementing the interface
org.springframework.beans.factory.config.Scopeand registering the custom
scope in Spring’s configuration (for XML, use the class org.springframework.beans
.factory.config.CustomScopeConfigurer).
新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。
京东地址:http://item.jd.com/11894632.html
当当地址:http://product.dangdang.com/23926195.html
亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6
淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail
或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。
相关推荐
spring学习1-29集无拖拽
spring学习资料大全spring学习资料大全spring学习资料大全spring学习资料大全spring学习资料大全spring学习资料大全spring学习资料大全spring学习资料大全
spring学习笔记spring学习笔记spring学习笔记spring学习笔记spring学习笔记spring学习笔记spring学习笔记spring学习笔记
springcloud视频学习,学习springcloud推荐的视频,讲的真的很好.
spring学习笔记
spring 学习指南 提供了很多有用的技术资料供大家学习使用
Spring 学习手册Spring 学习手册Spring 学习手册
本人spring学习笔记资源文档,感兴趣可下载
spring系统学习,解读spring,源码解读 spring系统学习,解读spring,源码解读 spring系统学习,解读spring,源码解读
网易云课堂黑马Spring四天精通学习笔记&源码(数据库要另建)
Spring学习思维导图Spring学习思维导图Spring学习思维导图Spring学习思维导图Spring学习思维导图Spring学习思维导图Spring学习思维导图Spring学习思维导图Spring学习思维导图Spring学习思维导图Spring学习思维导图...
Spring学习资料文档合集,包含 spring2.0-reference_RC2.1_zh_cn spring_reference_inchinese_m2 SpringGuide Spring基础教程 spring框架,技术详解及使用指导
spring学习事务源码spring学习事务源码spring学习事务源码spring学习事务源码spring学习事务源码spring学习事务源码spring学习事务源码spring学习事务源码
学习spring资源书籍,第三版讲解的很全面,包括springmvc的整合
spring 学习 springcore 例子
Spring学习总结文档,帮助快速入门。
spring学习,通过搭建基本的工程,从中学习spring的原理
Spring学习笔记( spring视频笔记)
Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Spring...
spring学习笔记