欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring 学习1

程序员文章站 2023-12-30 21:16:04
...

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
            
    
    博客分类: spring  

上一篇:

下一篇: