查看一个类或者接口的继承关系的快捷键:Cirl + T
查看一个类或者接口的结构:Cirl + O
抽取变量快捷键:shift + Alt + L
控制反转(IOC,Inversion of Control),是一个概念,是一种思想。指将传统上由程序代
码直接操控的对象调用权交给容器,通过容器来实现对象的装配和管理。控制反转就是对对
象控制权的转移,从程序代码本身反转到了外部容器。
2.1 Spring程序开发
容器就是一个xml配置文件
说明在调用无参数构造函数时,成员变量已经初始化。
ApplicationContext与BeanFactory容器的区别:
// 这两上容器对于其中Bean的创建时机不同:
// 1)ApplicationContext容器在进行初始化时,会将其中的所有Bean(对象)进行创建
// 缺点:占用系统资源(内存、CPU等)
// 优点:响应速度快
// 2)BeanFactory容器中的对象,在容器初始化时并不会被创建,而是在真正获取该对象时才被创建
// 缺点:相对来说,响应速度慢
// 优点:不多占用系统资源
Appliacation 容器用的较多
@Test
public void test05() {
// 创建BeanFactory容器,,加载Spring配置文件
BeanFactory bf = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
ISomeService service = (ISomeService) bf.getBean("myService");
service.doSome();
}
@Test
public void test02() {
// 创建ApplicationContext容器对象,加载Spring配置文件
// 会从类路径下查找配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
ISomeService service = (ISomeService) ac.getBean("myService");
service.doSome();
}
2.2 Bean 的装配
2.2.1 默认装配方式
<?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">
<!--注册Service
SomeServiceImpl myService = new SomeServiceImpl();
-->
<bean id="myService" class="com.bjpowernode.ba01.SomeServiceImpl"/>
</beans>
2.2.2 工厂 Bean
(1)动态工厂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="factory" class="com.bjpowernode.ba02.ServiceFactory"/>
<!--注册Service 动态工厂Bean -->
<bean id="myService" factory-bean="factory" factory-method="getSomeService"/>
</beans>
第一个<bean>创建了factory对象,第二个<bean>创建SomeServiceImpl对象
package com.bjpowernode.ba02;
import java.security.Provider.Service;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
//View层
public class MyTest {
@Test
public void test01(){
ISomeService service = new SomeServiceImpl();
service.doSome();
}
@Test
public void test02(){
ISomeService service = ServiceFactory.getSomeService();
service.doSome();
}
//解耦合,已经看不见实现类
public void test03(){
// 创建容器对象,加载Spring配置文件
String resource = "com/bjpowernode/ba02/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(resource);
ServiceFactory factory = (ServiceFactory) applicationContext.getBean("factory");
ISomeService service = factory.getSomeService();
service.doSome();
}
//动态加载Bean
public void test04(){
// 创建容器对象,加载Spring配置文件
String resource = "com/bjpowernode/ba02/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(resource);
ISomeService Service = (ISomeService) applicationContext.getBean("myService");
Service.doSome();
}
}
package com.bjpowernode.ba02;
public class ServiceFactory {
public ISomeService getSomeService(){
return new SomeServiceImpl();
}
}
(2)静态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">
<!-- 注册Service:静态工厂Bean -->
<bean id="myService" class="com.bjpowernode.ba03.ServiceFactory" factory-method="getSomeService"/>
</beans>
package com.bjpowernode.ba03;
public class ServiceFactory {
public static ISomeService getSomeService(){
return new SomeServiceImpl();
}
}
最后一句解析:class类的factory-method方法创建了ISomeService对象。
2.2.3 容器中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">
<!-- 注册Service,
scope="prototype" 原型模式,其对象的创建时机不是在Spring容器初始化时创建,而是在代码中真正访问时才创建
scope="singleton" 单例模式,其对象的创建时机是在Spring容器初始化时创建,是默认值
-->
<bean id="myService" class="com.bjpowernode.ba04.SomeServiceImpl" scope="prototype"/>
</beans>
<?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">
<!-- 注册Service,
scope="prototype" 原型模式,其对象的创建时机不是在Spring容器初始化时创建,而是在代码中真正访问时才创建
scope="singleton" 单例模式,其对象的创建时机是在Spring容器初始化时创建,是默认值
-->
<bean id="myService" class="com.bjpowernode.ba04.SomeServiceImpl" scope="singleton"/>
</beans>
@Test
public void test01() {
// 创建容器对象,加载Spring配置文件
String resource = "com/bjpowernode/ba04/applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
ISomeService service = (ISomeService) ac.getBean("myService");
ISomeService service2 = (ISomeService) ac.getBean("myService");
System.out.println("service == service2 " + (service == service2));
String resource2 = "com/bjpowernode/ba04/applicationContext2.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(resource2);
ISomeService service3 = (ISomeService) applicationContext.getBean("myService");
ISomeService service4 = (ISomeService) applicationContext.getBean("myService");
System.out.println("service3 == service4 " + (service3 == service4));
}
执行无参构造器
执行无参构造器
service == service2 false
执行无参构造器
service3 == service4 true
2.2.5 Bean后处理器
Bean后处理器:即当Spring容器实例化Bean实例之后进行的增强处理
xml配置:
注意没有id
<!-- 注册Service -->
<bean id="myService" class="com.bjpowernode.ba05.SomeServiceImpl"/>
<bean id="myService2" class="com.bjpowernode.ba05.SomeServiceImpl"/>
<!-- 注册Bean后处理器 -->
<bean class="com.bjpowernode.ba05.MyBeanPostProcessor"/>
Bean后处理器类:
package com.bjpowernode.ba05;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
//bean:表示当前正在初始化的Bean对象
//
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("执行----before---()方法");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("执行----after---()方法");
if ("myService".equals(beanName)) {
Object object = Proxy.newProxyInstance(bean.getClass().getClassLoader(), bean.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO Auto-generated method stub
Object invoke = method.invoke(bean, args);
if("doSome".equals(method.getName()))
return ((String) invoke).toUpperCase();
return invoke;
}
});
return object;
}
return bean;
}
}
package com.bjpowernode.ba05;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test01() {
// 创建容器对象,加载Spring配置文件
String resource = "com/bjpowernode/ba05/applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
ISomeService service = (ISomeService) ac.getBean("myService");
System.out.println(service.doSome());
System.out.println(service.doOther());
System.out.println("=======================");
ISomeService service2 = (ISomeService) ac.getBean("myService2");
System.out.println(service2.doSome());
System.out.println(service2.doOther());
}
}
选择性执行增强效果:
2.2.6 定制Bean的生命始末
对于销毁方法的执行有两个条件:
①:当前Bean为Singleton
②:需要手工关闭容器
import org.apache.log4j.net.SyslogAppender;
public class SomeServiceImpl implements ISomeService {
//Service层
public SomeServiceImpl(){
System.out.println("执行无参构造器");
}
@Override
public void doSome() {
System.out.println("执行doSome()方法");
}
public void setUp(){
System.out.println("生命起始");
}
public void tearDown(){
System.out.println("销毁之前");
}
}
<bean id="myService"
init-method="setUp"
destroy-method="tearDown"
class="com.bjpowernode.ba06.SomeServiceImpl"/>
@Test
public void test02(){
//解耦合,已经看不见实现类
// 创建容器对象,加载Spring配置文件
String resource = "com/bjpowernode/ba06/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(resource);
ISomeService service = (ISomeService) applicationContext.getBean("myService");
service.doSome();
//需要手工关闭容器
((ClassPathXmlApplicationContext)applicationContext).close();
}