Springのいくつかのインターフェース
4943 ワード
備考:Awareインターフェースspringマークインターフェースはspringのコールバックを表しています.
org.springframe ewark.beans.factory.FactoryBen org.springframe ewark.beans.factory.BenName Aware org.springframe ewark.beans.factory.Initializing Bean org.springframe ebook.beans.factory.Dispsable Bean import org.springframe ework.com.Apple Controntaction ControtextAware Lookup method injection(上の機能の簡単な実現版とすることができます)
/**
* getBean() spring getObject()
*
*/
public class Factory implements FactoryBean<Test1> {
@Override
public Test1 getObject() throws Exception {
return new Test1();
}
@Override
public Class<?> getObjectType() {
return Test1.class;
}
@Override
public boolean isSingleton() {
return false;
}
}
<!-- Factory ,context.getBean("&t3") -->
<bean id="t3" class="i.test.Factory"/>
/**
* ,id
*
*/
public class Test3 implements BeanNameAware {
private String name;
@Override
public void setBeanName(String name) {
this.name = name;
}
}
<!-- Test3 id -->
<bean id="t3" class="i.test.Test3" />
public class Test3 implements InitializingBean {
public void setName(String name) {
System.out.println("-----" + name);
}
/**
*
*/
@Override
public void afterPropertiesSet() throws Exception {
//
System.out.println("-----");
}
}
<bean id="t3" class="i.test.Test3" p:name="name"/>
<!-- , -->
<bean id="t3" class="i.test.Test3" p:name="name" init-method="afterPropertiesSet"/>
public class Test3 implements DisposableBean{
public void setName(String name) {
System.out.println("-----"+name);
}
/**
* ,
* bean spring
*/
@Override
public void destroy() throws Exception {
//
System.out.println("-----");
}
}
<bean id="t3" class="i.test.Test3" p:name="name" />
<!-- , -->
<bean id="t3" class="i.test.Test3" p:name="name" destroy-method="destroy" />
AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
//
context.registerShutdownHook();
public class Test3 implements ApplicationContextAware {
private ApplicationContext context;
/**
*
*/
public Test1 getTest1() {
return context.getBean("t1", Test1.class);
}
/**
* spring
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
}
<bean id="t1" class="i.test.Test1" scope="prototype" />
<bean id="t3" class="i.test.Test3" />
public abstract class Test3 {
/**
* Test1
*/
public abstract Test1 getTest1();
@Override
public String toString() {
System.out.println(getTest1());
return "";
}
}
<bean id="t1" class="i.test.Test1" scope="prototype" />
<bean id="t3" class="i.test.Test3">
<!-- Spring CGLIB , 。 cglib spring3.2 -->
<lookup-method name="getTest1" bean="t1" />
</bean>
cglibはクラスに対して代理を実現するもので、彼の原理は指定されたターゲットクラスに対してサブクラスを生成し、その中をカバーする方法で強化を実現するが、継承を採用しているので、final修飾のクラスを代理できない.javaが持参するプロキシツールproxy(プロキシクラスはインターフェースを実現しなければならない) ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
System.out.println(context.getBean("t3"));
//i.test.Test3$$EnhancerByCGLIB$$86e9bbc0@f1afec5