Spring的核心是___控制反转__(IOC)和__面向切面__(AOP)
Beans:管理bean
Core:核心
Context:上下文(配置文件)
Expression:spel表达式
AOP:切面编程
Aspects:AOP框架
JDBC:JdbcTemplete,数据库开发
ORM:整合hibernate
Transactions:事务管理
Web:web开发
Struts:整合struts
Test:整合Junit
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>4.3.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>4.3.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.3.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>4.3.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>4.3.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>4.3.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.3.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>4.3.7.RELEASEversion>
dependency>
public class HelloWord {
public void hello(){
System.out.println("Hello Spring");
}
}
<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="helloWord" class="test.HelloWord"/>
beans>
public class TestIOC {
@Test
public void demo01() {
// 之前
HelloWord helloWord=new HelloWord();
helloWord.hello();
// 现在从spring容器获得
// 1:获得容器
// 2:获得内容--不需要自己new,都是从spring容器获得
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContent.xml");
HelloWord helloWord1= (HelloWord) ctx.getBean("helloWord");
helloWord1.hello();
}
都可以输出Hello Spring
is a :是一个,继承
has a:有一个,成员变量,依赖
class B{
private A a; //B类依赖A类
}
依赖:一个对象需要使用另一个对象
注入:通过setter方法进行另一个对象实例设置
class BookServiceImpl{
// 之前开发:接口=实现类(service和dao耦合)
pirvate BookDao bookDao=new BookDaoImpl();
//spring之后(解耦,service实现类使用dao的接口,不知道具体的实现类)
private BookDao bookDao();
setter();
}
模拟spring执行过程:
创建service实例:BookService bookService=new BookServiceImpl();–>IOC
创建dao实例:BookDao bookDao=new BookDaoImpl(); -->IOC
将dao设置给service:bookService.setBookDao(bookDao); -->DI
public interface BookDao {
public void addBook();
}
public class BookDaoImpl implements BookDao {
@Override
public void addBook() {
System.out.println("添加书籍");
}
}
public interface BookService {
public void addBook();
}
public class BookServiceImpl implements BookService {
// 方式一:
// BookDao bookDao = new BookDaoImpl();
// 方式二:
private BookDao bookDao;
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
@Override
public void addBook() {
this.bookDao.addBook();
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="bookService" class="service.BookServiceImpl">
<property name="bookDao" ref="bookDao"/>
bean>
<bean id="bookDao" class="dao.BookDaoImpl"/>
beans>
public class test {
@Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContent.xml");
BookService bookService = (BookService) applicationContext.getBean("bookService");
bookService.addBook();
}
}
<bean id="bookService" class="service.BookServiceImpl">//必须提供默认工厂
<bean id="" class="工厂全限定类名" factory-method="静态工厂方法">
工厂
public class MyBeanFactory {
public static UserService createService() {
return new UserServiceImpl();
}
}
spring配置
<bean id="myBeanFactory" class="test.MyBeanFactory" factory-method="createService"/>
静态工厂测试
@Test
public void test02() {
//自定义静态工厂
UserService userService = MyBeanFactory.createService();
userService.addUser();
//spring工厂
// class 确定静态工厂全限定类名
// factory-method 确定静态方法名
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContent.xml");
UserService userService1 = (UserService) applicationContext.getBean("myBeanFactory",UserService.class);
userService1.addUser();
}
结果:控制台输出add user
public class MyBeanFactory {
public UserService createService() {
return new UserServiceImpl();
}
}
spring配置
<bean id="myBeanFactory" class="test.MyBeanFactory"/>
<bean id="userService" factory-bean="myBeanFactory" factory-method="createService"/>
工厂测试
@Test
public void test03() {
//自定义实例工厂
MyBeanFactory myBeanFactory = new MyBeanFactory();
UserService userService = myBeanFactory.createService();
userService.addUser();
// spring实例工厂
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContent.xml");
UserService userService1 = applicationContext.getBean("userService", UserService.class);
userService1.addUser();
}
用于确定spring创建实例的个数
<bean id="" class="" scope="">
测试
<bean id="userService" class="service.UserServiceImpl" scope="singleton"/>
<bean id="userService" class="service.UserServiceImpl" scope="prototype"/>
目标方法执行前和执行后,将进行初始化或者销毁
<bean id="" class="" init-method="初始化方法名称" destory-method="销毁的方法名称">
初始化
<bean id="userService" class="service.UserServiceImpl" init-method="myInit"/>
销毁(容器必须close,销毁方法才能执行,并且必须是单例的)
applicationContext.close();
spring提供一种机制,只要实现此接口BeanPostProcessor,并将实现类提供给spring容器,spring容器将自动执行,在初始化方法前执行before(),在初始化方法后执行after()。
Spring提供的工厂钩子,用于修改实例对象,可以生成代理对象,是AOP底层。
模拟:
A a=new A();
a=B.before(); -->将a的实例对象传递后处理bean,可以生成代理对象并返回
a.init();
a=B.after();
a.addUser(); -->生成代理对象,目的在目标方法前后执行(例如:开启事务,提交事务)
a.destory();
后处理bean
public class myBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
// return null;//之后使用会出现空指针异常NullPointerException
return o;
}
@Override
public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
//生成代理
return Proxy.newProxyInstance(myBeanPostProcessor.class.getClassLoader(), o.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("开启事务");
Object obj = method.invoke(o