- <beans>
- <bean id="singletonBean" class="com.itxxz.HelloItxxz">
- <constructor-arg>
- <value>Hello! 这是singletonBeanvalue>
- constructor-arg>
- bean>
- <bean id="itxxzBean" class="com.itxxz.HelloItxxz" singleton="false">
- <constructor-arg>
- <value>Hello! 这是itxxzBean! value>
- constructor-arg>
- bean>
- beans>
就以工厂方法中的静态方法为例讲解一下:
- import java.util.Random;
- public class StaticFactoryBean {
- public static Integer createRandom() {
- return new Integer(new Random().nextInt());
- }
- }
建一个config.xm配置文件,将其纳入Spring容器来管理,需要通过factory-method指定静态方法名称:
"random"class="example.chapter3.StaticFactoryBean" factory-method="createRandom" scope="prototype"/> "random"class="example.chapter3.StaticFactoryBean" factory-method="createRandom" scope="prototype"/>
测试:
- public static void main(String[] args) {
- //调用getBean()时,返回随机数.如果没有指定factory-method,会返回StaticFactoryBean的实例,即返回工厂Bean的实例
- XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("config.xml"));
- System.out.println("我是IT学习者创建的实例:"+factory.getBean("random").toString());
- }
- public static void main(String[] args) {
- //调用getBean()时,返回随机数.如果没有指定factory-method,会返回StaticFactoryBean的实例,即返回工厂Bean的实例
- XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("config.xml"));
- System.out.println("我是IT学习者创建的实例:"+factory.getBean("random").toString());
- }
Adapter类接口:Target
- public interface AdvisorAdapter {
- boolean supportsAdvice(Advice advice);
- MethodInterceptor getInterceptor(Advisor advisor);
- }
- MethodBeforeAdviceAdapter类
- Adapterclass MethodBeforeAdviceAdapter implements AdvisorAdapter, Serializable {
- public boolean supportsAdvice(Advice advice) {
- return (advice instanceof MethodBeforeAdvice);
- }
- public MethodInterceptor getInterceptor(Advisor advisor) {
- MethodBeforeAdvice advice = (MethodBeforeAdvice) advisor.getAdvice();
- return new MethodBeforeAdviceInterceptor(advice); }
- }
- public interface AdvisorAdapter {
- boolean supportsAdvice(Advice advice);
- MethodInterceptor getInterceptor(Advisor advisor);
- }
- MethodBeforeAdviceAdapter类
- Adapterclass MethodBeforeAdviceAdapter implements AdvisorAdapter, Serializable {
- public boolean supportsAdvice(Advice advice) {
- return (advice instanceof MethodBeforeAdvice);
- }
- public MethodInterceptor getInterceptor(Advisor advisor) {
- MethodBeforeAdvice advice = (MethodBeforeAdvice) advisor.getAdvice();
- return new MethodBeforeAdviceInterceptor(advice);
- }
- }