首页 > 代码库 > spring随手笔记2:初始化方法

spring随手笔记2:初始化方法

1.init-method="init"

public class HelloWorldServiceImpl implements HelloWorldService {    private String msg;        public void init(){        this.msg="hellword";    }    public void setMsg(String msg) {        this.msg = msg;    }    public String getMsg() {        return msg;    }}
<bean id="HelloWorld" class="com.ltf.captha.serviceImpl.HelloWorldServiceImpl" init-method="init">        <property name="msg">            <value>helloworld</value>        </property>    </bean>

2.实现org.springframework.beans.factory.InitializingBean中的afterPropertiesSet

public class HelloWord implements InitializingBean{    private String msg;    public void setMsg(String msg) {        this.msg = msg;    }    public String getMsg() {        return msg;    }    public void afterPropertiesSet() throws Exception {        this.msg="hello";    }}

 

spring随手笔记2:初始化方法