Spring工厂类
==============applicationContext.xml======================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!-- UserService的创建权交给了Spring -->
<bean id="userService" class="com.imooc.ioc.demo1.UserServiceImpl">
<property name="name" value="李四"/>
</bean>
</beans>
=====================springDemo1.java======================
/**
* Spring的方式实现
*/
@Test
public void demo2(){
//创建spring的工厂
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//通过工厂获得
UserService userService=(UserService) applicationContext.getBean("userService");
userService.sayHello();
}
/**
* 读取磁盘系统中的配置文件
*/
@Test
public void demo3(){
//创建spring的工厂
ApplicationContext applicationContext=new FileSystemXmlApplicationContext("c:\\applicationContext.xml");
//通过工厂获得
UserService userService=(UserService) applicationContext.getBean("userService");
userService.sayHello();
}
/**
* 传统方式的工厂类BeanFactory
*/
@Test
public void demo4(){
//创建工厂类
BeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
//通过工厂获得
UserService userService=(UserService) beanFactory.getBean("userService");
userService.sayHello();
}
Spring的Bean管理(XML方式)
- 使用类构造器实例化(默认无参数)
- 使用静态工厂方法实例化(简单工厂模式)
- 使用实例工厂方法实例化(工厂方法模式) 无参构造
无参构造
<!--============== Bean的实例化的三种方式,默认单例模式============== -->
<!-- 第一种无参构造器的方式 -->
<bean id="bean1" class="com.imooc.ioc.demo2.Bean1" />
/**
* Bean的实例化的三种方式:采用无参数构造方法
*/
public class Bean1 {
public int i=0;
public Bean1(){
i++;
System.out.println("Bean1被实例化了..."+i);
}
}
/**
* Bean实例化的三种方式
*/
public class SpringDemo2 {
/**
* 无参构造的方式
*/
@Test
public void demo1(){
//创建工厂
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//通过工厂类实例化Bean
Bean1 bean1=(Bean1) applicationContext.getBean("bean1");
}
}
简单工厂模式
/**
* 静态工厂的方式
*/
@Test
public void demo2(){
//创建工厂
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//通过工厂类实例化Bean
Bean2 bean1=(Bean2) applicationContext.getBean("bean2");
}
public class Bean2 {
}
public class Bean2Factory {
public static Bean2 createBean2(){
System.out.println("实例化Bean2....");
return new Bean2();
}
}
<!--第二种静态工厂的方式-->
<bean id="bean2" class="com.imooc.ioc.demo2.Bean2Factory" factory-method="createBean2"/>
实例工厂
/**
* 实例工厂的方式
*/
@Test
public void demo3(){
//创建工厂
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//通过工厂类实例化Bean
Bean3 bean1=(Bean3) applicationContext.getBean("bean3");
}
public class Bean3Factory {
public Bean3 createBean3(){
System.out.println("实例化Bean3....");
return new Bean3();
}
}
public class Bean3 {
}
<!--第三种实例工厂的方式-->
<bean id="beanFactory" class="com.imooc.ioc.demo2.Bean3Factory"/>
<bean id="bean3" factory-bean="beanFactory" factory-method="createBean3"/>
<!--============Bean的作用范围=============-->
<bean id="person" class="com.imooc.ioc.demo3.Person" scope="prototype"/>
public class Person {
}
/**
* Bean的作用范围测试
*/
public class SpringDemo3 {
/**
* 测试sigleton或property属性的区别
* property时,每次会实例化一个对象
*/
@Test
public void demo1(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
Person person1=(Person) applicationContext.getBean("person");
Person person2=(Person) applicationContext.getBean("person");
System.out.println(person1);
System.out.println(person2);
}
}
===============================Man==============================
public class Man implements BeanNameAware,ApplicationContextAware,InitializingBean,DisposableBean{
public Man(){
out.println("第一步MAN被实例化了...");
}
public String name ;
public void setName(String name) {
out.println("第二步,设置属性");
this.name = name;
}
public void setup(){
out.println("第七步:MAN被初始化了...");
}
public void shutdown(){
out.println("第十一步,MAN被销毁了...");
}
@Override
public void setBeanName(String s) {
out.println("第三步,设置Bean的名称:"+name);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
out.println("第四步,了解工厂信息");
}
@Override
public void afterPropertiesSet() throws Exception {
out.println("第六步,属性设置后");
}
public void run(){
out.println("第九步,执行业务方法");
}
@Override
public void destroy() throws Exception {
System.out.println("第十步:执行spring的销毁方法");
}
}
=======================MyBeanPostProcessor =========================
public class MyBeanPostProcessor implements BeanPostProcessor{
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// System.out.println("第五步:初始化前方法...");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// System.out.println("第八步:初始化后方法...");
if ("userDao".equals(beanName)) {
Object proxy = Proxy.newProxyInstance(bean.getClass().getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if ("save".equals(method.getName())) {
System.out.println("权限校验=============");
return method.invoke(bean, args);
}
return method.invoke(bean, args);
}
});
return proxy;
} else {
return bean;
}
}
}
========================配置====================================
<bean id="man" class="com.imooc.ioc.demo3.Man" init-method="setup" destroy-method="shutdown">
<property name="name" value="张三"/>
</bean>
<bean class="com.imooc.ioc.demo3.MyBeanPostProcessor"/>
=========================测试类==================================
@Test
public void demo2(){
ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
Man man=(Man) applicationContext.getBean("man");
//System.out.println(man);
man.run();
applicationContext.close();
}