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>
id 和 name都为别名
id 在ioc容器需要唯一
name 可以有多个名称,有非字符
默认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"); } }
@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"/>
此时会在初始化前和初始化后执行
实现DisposableBan 执行destroy
spring 会实例化所有的类的对象
spring 会实例化所有的类,不会每次使用一个实例化一个,,那是动态语言。。。
对类进行增强,aop 在不修改源代码的时候,对类进行增强
面向接口编程。。
编写接口
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