博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring 学习1
阅读量:6551 次
发布时间:2019-06-24

本文共 5945 字,大约阅读时间需要 19 分钟。

1.  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代码  
  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代码  
  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代码  
  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代码  
  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代码  
  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代码  
  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代码  
  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代码  
  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). 

转载地址:http://tafco.baihongyu.com/

你可能感兴趣的文章
吉炬消费系统软件输入密码后无法打开软件界面故障处理
查看>>
Hibernate学习系列————注解一对多双向实例
查看>>
Cannot load from mysql.proc
查看>>
汇编字符串拷贝
查看>>
Lambda的前世今生
查看>>
TCP/IP模型简介和/etc/hosts文件说明
查看>>
UIButton常用属性
查看>>
主键自增归0
查看>>
mysql之 [ERROR] InnoDB: Unable to lock ./ibdata1, error: 11
查看>>
如何批量修改文件后缀的方法
查看>>
Effective STL 笔记
查看>>
[LeetCode] 1. Two Sum
查看>>
POJ2538 ZOJ1884 UVA10082 WERTYU【输入输出】
查看>>
HDU5620 KK's Steel(C++语言版)
查看>>
旋转卡壳
查看>>
2016/10/09
查看>>
自定义HorizontalScrollView的scrollBar
查看>>
c++学习笔记和思考
查看>>
27.Docker集群部署
查看>>
DNS保存
查看>>