spring 学习1

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.
Java代码  spring 学习1
  1. props = new Properties();  
  2. props.load(new FileInputStream(“ch2/src/conf/msf.properties”));  
  3. 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
Java代码  spring 学习1
  1. public class InjectSimpleConfig {   
  2. private String name = "John Smith";   
  3. private int age = 35;   
  4. private float height = 1.78f;   
  5. private boolean programmer = true;   
  6. private Long ageInSeconds = 1103760000L;   
  7. // Getter/setter method omitted   
  8. }   
 
Xml代码  spring 学习1
  1. <bean id="injectSimpleConfig" class="com.apress.prospring3.ch4.xml.InjectSimpleConfig"/>   
  2. <bean id="injectSimpleSpel" class="com.apress.prospring3.ch4.xml.InjectSimpleSpel">   
  3. <property name="name">   
  4. <value>#{injectSimpleConfig.name}</value>   
  5. </property>   
  6. <property name="age">   
  7. <value>#{injectSimpleConfig.age + 1}</value>   
  8. </property>   
  9. <property name="height">   
  10. <value>#{injectSimpleConfig.height}</value>   
  11. </property>   
  12. <property name="isProgrammer">   
  13. <value>#{injectSimpleConfig.programmer}</value>   
  14. </property>   
  15. <property name="ageInSeconds">   
  16. <value>#{injectSimpleConfig.ageInSeconds}</value>   
  17. </property>   
  18. </bean>   
 
Java代码  spring 学习1
  1. @Service("injectSimpleSpel")   
  2. public class InjectSimpleSpel {   
  3. @Value("#{injectSimpleConfig.name}")   
  4. private String name;   
  5. @Value("#{injectSimpleConfig.age + 1}")   
  6. private int age;   
  7. @Value("#{injectSimpleConfig.height}")   
  8. private float height;   
  9. @Value("#{injectSimpleConfig.programmer}")   
  10. private boolean programmer;   
  11. @Value("#{injectSimpleConfig.ageInSeconds}")   
  12. private Long ageInSeconds;   
  13. // Other codes omitted   
  14. }   
 10.Using Collections for Injection
Java代码  spring 学习1
  1. public class CollectionInjection {   
  2. private Map<String, Object> map;   
  3. private Properties props;   
  4. private Set set;   
  5. private List list;   
  6. public static void main(String[] args) {   
  7. GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();   
  8. ctx.load("classpath:app-context-xml.xml");   
  9. ctx.refresh();   
  10. CollectionInjection instance = (CollectionInjection) ctx.getBean("injectCollection");   
  11. instance.displayInfo();   
  12. }   
  13. public void setList(List list) {   
  14. this.list = list;   
  15. }   
  16. public void setSet(Set set) {   
  17. this.set = set;   
  18. }   
  19. public void setMap(Map <String, Object> map) {   
  20. this.map = map;   
  21. }   
  22. public void setProps(Properties props) {   
  23. this.props = props;   
  24. }   
  25. public void displayInfo() {   
  26. // display the Map   
  27. System.out.println("Map contents:\n");   
  28. for (Map.Entry<String, Object> entry: map.entrySet()) {   
  29. System.out.println("Key: " + entry.getKey() + " - Value: " + entry.getValue());   
  30. }   
  31. // display the properties   
  32. System.out.println("\nProperties contents:\n");   
  33. for (Map.Entry<Object, Object> entry: props.entrySet()) {   
  34. System.out.println("Key: " + entry.getKey() + " - Value: " + entry.getValue());   
  35. }   
  36. // display the set   
  37. System.out.println("\nSet contents:\n");   
  38. for (Object obj: set) {   
  39. System.out.println("Value: " + obj);   
  40. }   
  41. // display the list   
  42. System.out.println("\nList contents:\n");   
  43. for (Object obj: list) {   
  44. System.out.println("Value: " + obj);   
  45. }   
  46. }   
  47. }   
 
Java代码  spring 学习1
  1. <bean id="oracle" name="wiseworm" class="com.apress.prospring3.ch4.BookwormOracle"/>   
  2. <bean id="injectCollection" class="com.apress.prospring3.ch4.xml.CollectionInjection">   
  3. <property name="map">   
  4. <map>   
  5. <entry key="someValue">   
  6. <value>Hello World!</value>   
  7. </entry>   
  8. <entry key="someBean">   
  9. <ref local="oracle"/>   
  10. </entry>   
  11. </map>   
  12. </property>   
  13. <property name="props">   
  14. <props>   
  15. <prop key="firstName">Clarence</prop>   
  16. <prop key="secondName">Ho</prop>   
  17. </props>   
  18. </property>   
  19. <property name="set">   
  20. <set>   
  21. <value>Hello World!</value>   
  22. <ref local="oracle"/>   
  23. </set>   
  24. </property>   
  25. <property name="list">   
  26. <list>   
  27. <value>Hello World!</value>   
  28. <ref local="oracle"/>   
  29. </list>   
  30. </property>   
  31. </bean>   
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
Xml代码  spring 学习1
  1. <util:map id="map" map-class="java.util.HashMap">   
  2. <entry key="someValue">   
  3. <value>Hello World!</value>   
  4. </entry>   
  5. <entry key="someBean">   
  6. <ref bean="oracle"/>   
  7. </entry>   
  8. </util:map>   
  9. <util:properties id="props">   
  10. <prop key="firstName">Clarence</prop>   
  11. <prop key="secondName">Ho</prop>   
  12. </util:properties>   
  13. <util:set id="set">   
  14. <value>Hello World!</value>   
  15. <ref bean="oracle"/>   
  16. </util:set>   
  17. <util:list id="list">   
  18. <value>Hello World!</value>   
  19. <ref bean="oracle"/>   
  20. </util:list>  
 
Java代码  spring 学习1
  1. @Service("injectCollection")   
  2. public class CollectionInjection {   
  3. @Resource(name="map")   
  4. private Map<String, Object> map;   
  5. @Resource(name="props")   
  6. private Properties props;   
  7. @Resource(name="set")   
  8. private Set set;   
  9. @Resource(name="list")   
  10. private List list;   
  11. // Other codes omitted   
  12. }   
 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). 
上一篇:子集中的回溯问题


下一篇:142. 环形链表 II