1.使用xml的方式进行配置IOC容器,首先引入依赖
在Resource资源下配置,applicationContext.xml ,刷新mevan后可以直接选择配置spring.xml文件
<!-- spring核心用来管理bean --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency>
applicationContext.xml
配置bean:
id:实现标识给bean起名字
class属性标识给这个bean对象定义类型
name:属性是可以给当前bean起别名(使用“ ”,“,”,“;”分隔开来)
singleton:单例(默认)
prototype:非单例
<?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="bookDAO" class="org.itheima.dao.impl.BookDAOImpl"/><bean id="bookService" class="org.itheima.service.impl.BookServiceImpl"/>
</beans>
现在配置DAO层:
public interface BookDAO {public void book();
}
public class BookDAOImpl implements BookDAO {@Overridepublic void book() {System.out.println("BookDAO……");}
}
Service层:
public interface BookService {public void write();
}
public class BookServiceImpl implements BookService {private BookDAO bookDAO = new BookDAOImpl();@Overridepublic void write() {System.out.println("BookService……");}
}
测试文件1:
public class BookDAOTest {@Testpublic void book() {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");BookDAO bookDAO = (BookDAO) ctx.getBean("bookDAO");bookDAO.book();}
}
打印结果:
测试文件2:
public class BookServiceTest {@Testpublic void write() {ClassPathXmlApplicationContext txc = new ClassPathXmlApplicationContext("applicationContext.xml");BookService bookService = (BookService) txc.getBean("bookService");bookService.write();}
}
打印结果:
依赖 new ClassPathXmlApplicationContext("applicationContext.xml")的方式不太合适,同样是创建对象的形式,那不如直接new一个实体DAO,因此此方法需要改进!!
2.使用dao与Service的关系
与以上不同的是BookServiceImpl与测试类不同:
public class BookServiceImpl implements BookService {
private BookDAO bookDAO = new BookDAOImpl();@Overridepublic void write() {bookDAO.book();System.out.println("BookService……");}
}
测试类:
public class BookServiceImplTest {@Testpublic void testWrite() {ClassPathXmlApplicationContext cax = new ClassPathXmlApplicationContext("applicationContext.xml");BookService bookService = (BookService) cax.getBean("bookService");bookService.write();}
}
测试结果:
3.使用DI注入 bean实例,并且使用service 调用dao层方法
首先配置Service的依赖关系properties
properties:配置当前bean的属性
name属性标识配置哪一个具体的属性
ref属性表示参照哪个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="bookDAO" class="org.itheima.dao.impl.BookDAOImpl"/><bean id="bookService" class="org.itheima.service.impl.BookServiceImpl"><property name="bookDAO" ref="bookDAO"/></bean>
</beans>
dao层:
public interface BookDAO {public void book();
}
public class BookDAOImpl implements BookDAO {@Overridepublic void book() {System.out.println("BookDAO……");}
}
service层:
public interface BookService {public void write();
}
通过容器将bookDAO对象传入进去
public class BookServiceImpl implements BookService {private BookDAO bookDAO;@Overridepublic void write() {bookDAO.book();System.out.println("BookService……");}public void setBookDAO(BookDAO bookDAO) {this.bookDAO = bookDAO;}
}
测试类:
public class BookServiceImplTest {
private BookService bookService;@Testpublic void write() {ClassPathXmlApplicationContext cax = new ClassPathXmlApplicationContext("applicationContext.xml");BookService bookService = (BookService) cax.getBean("bookService");bookService.write();}
}
测试结果:
4.动态工厂bean的实例化:
DAO层:
public interface BookDAO {public void book();
}
实例化工厂:
public class FactoryImpl implements FactoryBean<BookDAO> {@Overridepublic BookDAO getObject() throws Exception {System.out.println("工厂正在创造DAO对象......");return new BookDAOImpl();}@Overridepublic Class<?> getObjectType() {return BookDAO.class;}@Overridepublic boolean isSingleton() {return true;}
}
配置文件:
<?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="bookService" name="service" class="org.itheima.service.impl.BookServiceImpl"><property name="bookDAO" ref="bookDAO"/></bean><bean id="bookDAO" class="org.itheima.factory.FactoryImpl"/>
</beans>
测试文件:
public class BookDAOTest {@Testpublic void book() {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");BookDAO bookDAO = (BookDAO) ctx.getBean("bookDAO");bookDAO.book();}
}
运行结果: