spring bean 一

栏目: Java · 发布时间: 5年前

内容简介:spring 的bean 默认使用无参的构造方法。测试先创建工厂对象,然后实例化对象

spring 的bean 默认使用无参的构造方法。

使用静态工厂

package com.ming;

public class BeanFactory {
    public static IMing createMing(){
        return new Ming("ming");
    }
}
package com.ming;

public interface IMing {
    public void hello();
}
package com.ming;

public class Ming implements IMing{
    private String ming = null;

    public Ming(String ming){
        this.ming = ming;
    }

    @Override
    public void hello(){
        System.out.println(this.ming);
    }

    public void setMing(String ming) {
        this.ming = ming;
    }

    public String getMing() {
        return ming;
    }
}
<?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="ming" class="com.ming.BeanFactory" factory-method="createMing"/>
</beans>

测试

package com.ming;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import static org.junit.Assert.*;

public class MingTest {
    ApplicationContext applicationContext = null;
    @Before
    public void setUp() throws Exception{
        applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @Test
    public void hello() {
        IMing ming = (IMing)applicationContext.getBean("ming");
        ming.hello();
    }
}

实例工厂实例化

先创建工厂对象,然后实例化对象

<?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="bean3Factory" class="com.ming.Bean3Factory"/>
	<bean id="bean3" factory-bean="bean3Factory" factory-method="createBean3"/>
</beans>
package com.ming;

/**
 * 实例工厂
 * @author ming
 */
public class Bean3Factory {
    public IMing createBean3(){
        System.out.println("bean3的工厂被执行");
        return new Bean3();
    }
}
package com.ming;

public class Bean3 implements IMing {
    @Override
    public void hello() {
        System.out.println(3333);
    }
}
<?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="bean3Factory" class="com.ming.Bean3Factory"/>
	<bean id="bean3" factory-bean="bean3Factory" factory-method="createBean3"/>
</beans>

Bean配置

id 和 name都为别名

id 在ioc容器需要唯一

name 可以有多个名称,有非字符

Bean作用域

默认Bean为单例模式

同时可以设置为多例模式

<?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="person" class="com.ming.Ming" scope="prototype"/>
</beans>

上方为多例

生命周期

载入容器会调用方法,销毁的时候会调用方法

package com.ming;

public class Ming implements IMing{
    public Ming(){
        System.out.println("实例化");
    }

    public void setUp(){
        System.out.println("实例初始化");
    }

    public void tearDown(){
        System.out.println("被销毁");
    }
}
package com.ming;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class MingTest {
    ApplicationContext applicationContext = null;
    @Before
    public void setUp() throws Exception{
        applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @Test
    public void hello() {
        IMing ming = (IMing) applicationContext.getBean("ming");
    }
}

生命周期完整过程

对象实例化

设置属性

实现BeanFactoryAware 执行setBeanName 等

@Override
    public void setBeanName(String s) {
        System.out.println(s);
        System.out.println("第三步设置bean的名称");
    }

实现BeanNameAware接口

获取工厂

@Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("第四步 获取上下文对象" + applicationContext);
    }

实现 ApplicationContextAware

@Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("第四步 获取上下文对象" + applicationContext);
    }

实现 BeanFactoryAware 接口

后置处理 前置处理

package com.ming;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        System.out.println("初始化前执行------");
        return o;
    }

    @Override
    public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
        System.out.println("初始化之后------");
        return o;
    }

    public void ming(){
        System.out.println("------");
    }
}
<bean class="com.ming.MyBeanPostProcessor"/>

此时会在初始化前和初始化后执行

属性设置后执行

指定初始化 init

执行自身的业务方法

销毁方法

实现DisposableBan 执行destroy

调用在bean标签中定义的方法

注意

spring 会实例化所有的类的对象

spring 会实例化所有的类,不会每次使用一个实例化一个,,那是动态语言。。。

后置处理器

对类进行增强,aop 在不修改源代码的时候,对类进行增强

Spring 中的生命周期

面向接口编程。。

编写接口

package com.ming;

public interface UserDao {
    public void findAll();
    public void save();
    public void update();
    public void delete();
}
package com.ming;

public class UserDaoImpl implements UserDao {
    @Override
    public void findAll() {
        System.out.println("findAll");
    }

    @Override
    public void save() {
        System.out.println("save");
    }

    @Override
    public void update() {
        System.out.println("update");
    }

    @Override
    public void delete() {
        System.out.println("delete");
    }
}
<bean id="userDao" class="com.ming.UserDaoImpl"/>
@Test
    public void hello() {
        UserDao userDao = (UserDao) applicationContext.getBean("userDao");
        userDao.findAll();
    }

对save方法进行增强

前置 后置处理器

package com.ming;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        System.out.println("初始化前执行------");
        return o;
    }

    @Override
    public Object postProcessAfterInitialization(final Object o, String s) throws BeansException {
        System.out.println("初始化之后------");
        System.out.println(s);
        Object object = Proxy.newProxyInstance(o.getClass().getClassLoader(), o.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println(method.getName());
                System.out.println(proxy.getClass().getName());
                // 此处调用
                return method.invoke(o, args);
            }
        });
        return object;
    }

}
package com.ming;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class MingTest {
    ApplicationContext applicationContext = null;
    @Before
    public void setUp() throws Exception{
        applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @Test
    public void hello() {
        UserDao userDao = (UserDao) applicationContext.getBean("userDao");
        userDao.delete();
    }

}
package com.ming;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class MingTest {
    ApplicationContext applicationContext = null;
    @Before
    public void setUp() throws Exception{
        applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @Test
    public void hello() {
        UserDao userDao = (UserDao) applicationContext.getBean("userDao");
        userDao.delete();
    }

}

这里使用动态代理,进行代理调用 其中Proxy.newProxyInstance( 为dao


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Pattern Recognition and Machine Learning

Pattern Recognition and Machine Learning

Christopher Bishop / Springer / 2007-10-1 / USD 94.95

The dramatic growth in practical applications for machine learning over the last ten years has been accompanied by many important developments in the underlying algorithms and techniques. For example,......一起来看看 《Pattern Recognition and Machine Learning》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具