整体代码结构:
axe.java
package com.kinsey.woo.service;
public interface Axe {
public String chop();
}
Person.java
package com.kinsey.woo.service;
public interface Person {
public void useAxe();
}
StoneAxe.java
package com.kinsey.woo.dto;
import com.kinsey.woo.service.Axe;
public class StoneAxe implements Axe {
@Override
public String chop() {
return "使用斧头砍柴好慢";
}
}
SteelAxe.java
package com.kinsey.woo.dto;
import com.kinsey.woo.service.Axe;
public class SteelAxe implements Axe {
@Override
public String chop() {
return "使用钢斧砍柴好快.";
}
}
Chinese.java
package com.kinsey.woo.dto;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.kinsey.woo.service.Axe;
import com.kinsey.woo.service.Person;
public class Chinese implements Person,InitializingBean,DisposableBean, ApplicationContextAware {
private Axe axe;
public Axe getAxe() {
return axe;
}
public void setAxe(Axe axe) {
System.out.println("setAxe方法被调用...");
this.axe = axe;
}
@Override
public void useAxe(){
System.out.println("我打算去看点柴.");
System.out.println(axe.chop());
}
public void initMethod(){
System.out.println("init-method方法被调用...");
}
public void destoryMethod(){
System.out.println("destory-method方法被调用...");
}
@Override
public void destroy() throws Exception {
System.out.println("实现DisposableBean接口的detory()方法被调用....");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("实现InitializingBean接口的afterPropertiesSet()方法被调用....");
}
@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
System.out.println("this.ctx = " + ctx + "\n");
}
}
RunMain.java
package com.kinsey.woo.main;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.kinsey.woo.dto.Chinese;
public class RunMain {
public static void main(String[] args) {
@SuppressWarnings("resource")
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");
Chinese chinese = ctx.getBean("chinese", Chinese.class);
chinese.useAxe();
//会自动调用destroy-method 对应的方法和destroy方法
/*
* 销毁容器
* 在基于Web的ApplicationContext实现中,系统已经提供了相应的代码保证关闭Web应用时恰当地关闭Spring容器.
* 如果处于非Web应用的环境下,调用AbstractApplicationContext提供的registerShutdownHook即可.
*/
ctx.registerShutdownHook();
}
}
application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="stoneAxe" class="com.kinsey.woo.dto.StoneAxe">
</bean>
<bean id="steelAxe" class="com.kinsey.woo.dto.SteelAxe">
</bean>
<bean id="chinese" class="com.kinsey.woo.dto.Chinese" init-method="initMethod" destroy-method="destoryMethod" depends-on="stoneAxe,steelAxe">
<property name="axe" ref="steelAxe"></property>
</bean>
</beans>

被折叠的 条评论
为什么被折叠?



