一.spring的核心api
Spring有如下的核心api
BeanFactory :这是一个工厂,用于生成任意bean。采取延迟加载,第一次getBean时才会初始化Bean
ApplicationContext:是BeanFactory的子接口,功能更强大。(国际化处理、事件传递、Bean自动装配、各种不同应用层的Context实现)。当配置文件被加载,就进行对象实例化。
ClassPathXmlApplicationContext 用于加载classpath(类路径、src)下的xml,加载xml运行时位置 --> /WEB-INF/classes/...xml
FileSystemXmlApplicationContext 用于加载指定盘符下的xml,加载xml运行时位置 --> /WEB-INF/...xml,通过java web ServletContext.getRealPath() 获得具体盘符
二.spring的两种实例化方式
1.使用ApplicationContext方式进行加载
2.使用BeanFactory方式进行加载
第一步 建立Spring工程,并向其中导入必须的jar包
第二步 建立BookDao接口,BookDaoImpl实现类,BookService接口,BookServiceImpl实现类
package com.zk.myspring; public interface BookDao { public void addBook(); }
package com.zk.myspring; public class BookDaoImpl implements BookDao{ @Override public void addBook() { // TODO Auto-generated method stub System.out.println("addBook"); } }
package com.zk.myspring; public interface BookService { public abstract void addBook(); }
package com.zk.myspring; public class BookServiceImpl implements BookService{ //方式一:接口等于实现类 //private BookDao bookDao=new BookDaoImpl(); //方式二:接口+set方法 private BookDao bookdao; //BookService依赖注入 BookDao public void setBookdao(BookDao bookdao) {//依赖注入 this.bookdao = bookdao; } @Override public void addBook() { // TODO Auto-generated method stub //this.bookdao.addBook(); System.out.println("add book"); } }
第三步 创建一个ApplicationContext.xml文件,并在文件中配置BookDao和BookService的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" 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"> <!-- 创建Dao --> <!-- 模拟spring执行过程 创建Service实例:BookService bookservice=new BookServiceImpl(); IOC <bean> 创建Dao实例:BookDao bookDao=new BookDaoImpl(); IOC 将dao设置给service:bookservice.setBookDao(bookDao); DI <property> <property> 用于属性注入 name:bean的属性名,通过setter方法获得 ref:另一个bean的id值的引用 --> <!--创建Dao --> <bean id="bookDaoId" class="com.zk.myspring.BookDaoImpl" ></bean> <!-- 创建service 依赖注入bookdaoId--> <bean id="bookServiceId" class="com.zk.myspring.BookServiceImpl"> <property name="bookdao" ref="bookDaoId"></property> </bean> </beans>
最后一步 使用两种方式实例化bean,我们可以使用ApplicationContext和BeanFactory分别实例化Bean
package com.zk.myspring; 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.core.io.ClassPathResource; public class TestBook { public static void main(String[]args) { //从spring容器中获得 String path="ApplicationContext.xml"; ApplicationContext ac=new ClassPathXmlApplicationContext(path); BookService bs=(BookService) ac.getBean("bookServiceId"); bs.addBook(); } @Test public void test1(){ //使用BeanFactory实例化 String path="ApplicationContext.xml"; BeanFactory bf=new XmlBeanFactory(new ClassPathResource(path)); BookService bs=(BookService) bf.getBean("bookServiceId"); bs.addBook(); } }
分别运行两个实例
ApplicationContext运行结果
BeanFactory运行结果
成功
三.自定义工厂
新建一个Spring_003 工程,并加入MyBeanFactory.java、TestBeanFactory.java、UserService.java、UserServiceImpl.java
MyBeanFactory表示自定义工厂
package com.zk.BeanFactory; public class MyBeanFactory { public static UserService createService(){ return new UserServiceImpl(); } }
UserService.java
package com.zk.BeanFactory; public interface UserService { public void addUser(); }
UserServiceImpl.java
package com.zk.BeanFactory; public class UserServiceImpl implements UserService{ @Override public void addUser() { // TODO Auto-generated method stub System.out.println("UserService"); } }
配置ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <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"> <bean id="userservice" class="com.zk.BeanFactory.UserServiceImpl"></bean> <!-- <bean id="user" class="com.zk.BeanFactory.User"></bean>--> </beans>
最后写一个TestBeanFactory.java文件
package com.zk.BeanFactory; import org.junit.Test; public class TestBeanFactory { @Test public void Demo(){ UserService userservice=MyBeanFactory.createService(); userservice.addUser(); } }
执行结果
success