天天看點

spring 學習1

2. spring di= javabeans +interfaces

3.jsr-330=dependency injection for java

4.jsr-303= bean validation api specification 

5.

spring 學習1

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

spring 學習1

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   

}   

spring 學習1

<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 name="height">   

<value>#{injectsimpleconfig.height}</value>   

<property name="isprogrammer">   

<value>#{injectsimpleconfig.programmer}</value>   

<property name="ageinseconds">   

<value>#{injectsimpleconfig.ageinseconds}</value>   

</bean>   

spring 學習1

@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

spring 學習1

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()) {   

// 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) {   

spring 學習1

<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"/>   

</map>   

<property name="props">   

<props>   

<prop key="firstname">clarence</prop>   

<prop key="secondname">ho</prop>   

</props>   

<property name="set">   

<set>   

</set>   

<property name="list">   

<list>   

</list>   

 ////////////////////////////////////////////////////////////////////////////////////////////////////////////

spring 學習1

<util:map id="map" map-class="java.util.hashmap">   

<ref bean="oracle"/>   

</util:map>   

<util:properties id="props">   

</util:properties>   

<util:set id="set">   

</util:set>   

<util:list id="list">   

</util:list>  

spring 學習1

@service("injectcollection")   

@resource(name="map")   

@resource(name="props")   

@resource(name="set")   

@resource(name="list")   

 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).