天天看點

spring 學習2-Spring Configuration in detail

1.bean life-cycle management

spring 學習2-Spring Configuration in detail

<bean id="simplebean1"   

class="com.apress.prospring3.ch5.lifecycle.simplebean"   

init-method="init" />   

spring 學習2-Spring Configuration in detail

<bean id="destructivebean"   

class="com.apress.prospring3.ch5.lifecycle.destructivebean"   

destroy-method="destroy"/>  

spring 學習2-Spring Configuration in detail

public class simplebeanwithinterface implements initializingbean{   

private static final string default_name = "luke skywalker";   

private string name = null;   

private int age = integer.min_value;   

public void setname(string name) {   

this.name = name;   

}   

public void setage(int age) {   

this.age = age;   

public void myinit() {   

system.out.println("my init");   

public void afterpropertiesset() throws exception {   

system.out.println("initializing bean");   

if (name == null) {   

system.out.println("using default name");   

name = default_name;   

spring 學習2-Spring Configuration in detail

public class destructivebeanwithinterface implements initializingbean,   

disposablebean{   

private inputstream is = null;   

public string filepath = null;   

if (filepath == null) {   

throw new illegalargumentexception(   

"you must specify the filepath property of " + destructivebean.class);   

is = new fileinputstream(filepath);   

public void destroy() {   

system.out.println("destroying bean");   

if (is != null) {   

try {    

is.close();   

is = null;   

} catch (ioexception ex) {   

system.err.println("warn: an ioexception occured"   

+ " trying to close the inputstream");   

public void setfilepath(string filepath) {   

this.filepath = filepath;   

}  

spring 學習2-Spring Configuration in detail

public class simplebeanwithjsr250 {   

// codes omitted   

@postconstruct   

public void init() throws exception {   

// rest of codes omitted   

spring 學習2-Spring Configuration in detail

public class destructivebeanwithjsr250 {   

@predestroy   

try {   

spring 學習2-Spring Configuration in detail

public class shutdownhookbean implements applicationcontextaware {   

private applicationcontext ctx;   

public void setapplicationcontext(applicationcontext ctx)   

throws beansexception {   

if (ctx instanceof genericapplicationcontext) {   

((genericapplicationcontext) ctx).registershutdownhook();   

spring 學習2-Spring Configuration in detail

class="com.apress.prospring3.ch5.lifecycle.destructivebeanwithinterface">   

<property name="filepath">   

<value>d:/temp/test.txt</value>   

</property>   

</bean>   

<bean id="shutdownhook"   

class="com.apress.prospring3.ch5.interaction.shutdownhookbean"/>   

 3.using applicationcontext and messagesource 

spring 學習2-Spring Configuration in detail

public static void main(string[] args) {   

genericxmlapplicationcontext ctx = new genericxmlapplicationcontext();   

ctx.load("classpath:appcontext/messagesource.xml");   

ctx.refresh();   

locale english = locale.english;   

locale czech = new locale("cs", "cz");   

system.out.println(ctx.getmessage("msg", null, english));   

system.out.println(ctx.getmessage("msg", null, czech));   

system.out.println(ctx.getmessage("namemsg", new object[] { "clarence",   

"ho" }, english));   

spring 學習2-Spring Configuration in detail

<bean id="messagesource"   

class="org.springframework.context.support.resourcebundlemessagesource">   

<property name="basenames">   

<list>   

<value>buttons</value>   

<value>labels</value>   

</list>   

 4.using application events 

spring 學習2-Spring Configuration in detail

public class messageevent extends applicationevent {   

private string msg;   

public messageevent(object source, string msg) {   

super(source);   

this.msg = msg;   

public string getmessage() {   

return msg;   

spring 學習2-Spring Configuration in detail

public class messageeventlistener implements applicationlistener<messageevent> {   

public void onapplicationevent(messageevent event) {   

messageevent msgevt = (messageevent) event;   

system.out.println("received: " + msgevt.getmessage());   

spring 學習2-Spring Configuration in detail

public class publisher implements applicationcontextaware {   

applicationcontext ctx = new classpathxmlapplicationcontext(   

"classpath:events/events.xml");   

publisher pub = (publisher) ctx.getbean("publisher");   

pub.publish("hello world!");   

pub.publish("the quick brown fox jumped over the lazy dog");   

public void setapplicationcontext(applicationcontext applicationcontext)   

this.ctx = applicationcontext;   

public void publish(string message) {   

ctx.publishevent(new messageevent(this, message));   

spring 學習2-Spring Configuration in detail

<bean id="publisher" class="com.apress.prospring3.ch5.event.publisher"/>   

<bean id="messageeventlistener"   

class="com.apress.prospring3.ch5.event.messageeventlistener"/>  

 5.accessing resources

spring 學習2-Spring Configuration in detail

public static void main(string[] args) throws exception{   

resource res1 = ctx.getresource("file:///d:/temp/test.txt");   

displayinfo(res1);   

resource res2 = ctx.getresource("classpath:test.txt");   

displayinfo(res2);   

resource res3 = ctx.getresource("http://www.google.co.uk");   

displayinfo(res3);   

private static void displayinfo(resource res) throws exception{   

system.out.println(res.getclass());   

system.out.println(res.geturl().getcontent());   

system.out.println("");   

//getfile(), getinputstream(), or geturl()  

<strong>6. java configuration  

</strong>  

spring 學習2-Spring Configuration in detail

@configuration   

public class appconfig {   

// xml:   

// <bean id="messageprovider"   

class="com.apress.prospring3.ch5.javaconfig.configurablemessageprovider"/>   

@bean   

public messageprovider messageprovider() {   

return new configurablemessageprovider();   

// <bean id="messagerenderer"   

class="com.apress.prospring3.ch5.javaconfig.standardoutmessagerenderer"   

// p:messageprovider-ref="messageprovider"/>   

public messagerenderer messagerenderer() {   

messagerenderer renderer = new standardoutmessagerenderer();   

// setter injection   

renderer.setmessageprovider(messageprovider());   

return renderer;   

spring 學習2-Spring Configuration in detail

<strong>   

spring 學習2-Spring Configuration in detail

@import(otherconfig.class)   

// xml: <import resource="classpath:events/events.xml")   

@importresource(value="classpath:events/events.xml")   

// xml: <context:property-placeholder location="classpath:message.properties"/>   

@propertysource(value="classpath:message.properties")   

// xml: <context:component-scan base-package="com.apress.prospring3.ch5.context"/>   

@componentscan(basepackages={"com.apress.prospring3.ch5.context"})   

@enabletransactionmanagement public class appconfig {   

@autowired   

environment env;   

@lazy(value=true) //xml <bean .... lazy-init="true"/>   

// constructor injection   

return new configurablemessageprovider(env.getproperty("message"));   

@bean(name="messagerenderer")   

@scope(value="prototype") // xml: <bean ... scope="prototype"/>   

@dependson(value="messageprovider") // xml: <bean ... depends-on="messageprovider"/>   

 7.configurableenvironment env = ctx.getenvironment(); 

mutablepropertysources propertysources = env.getpropertysources(); 

8.jsr-330

spring 學習2-Spring Configuration in detail

@named("messagerenderer")   

@singleton   

public class standardoutmessagerenderer implements messagerenderer {   

@inject   

@named("messageprovider")   

private messageprovider messageprovider = null;   

public void render() {   

if (messageprovider == null) {   

throw new runtimeexception(   

"you must set the property messageprovider of class:"   

+ standardoutmessagerenderer.class.getname());   

system.out.println(messageprovider.getmessage());   

public void setmessageprovider(messageprovider provider) {   

this.messageprovider = provider;   

public messageprovider getmessageprovider() {   

return this.messageprovider;   

}