目录
- init-method:指定类中的初始化方法名称
- destory-method:指定类中销毁方法名称
建立空项目
建立一个模块:maven项目
建立数据层dao和业务层service的接口和实现类文件
目录如下:
BookDao.class
package com.itheima.dao;
public interface BookDao {
public void save();
}
BookDaoimpl.class
package com.itheima.dao.impl;
import com.itheima.dao.BookDao;
public class BookDaoimpl implements BookDao {
public void save() {
System.out.println("BookDao运行了");
}
//测试生命周偶器
public void init(){
System.out.println("初始化成功");
}
public void destroy(){
System.out.println("程序关闭");
}
}
BookService.class
package com.itheima.service;
public interface BookService {
public void save();
}
BookServiceimpl.class
package com.itheima.service.impl;
import com.itheima.dao.BookDao;
import com.itheima.service.BookService;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class BookServiceimpl implements BookService, InitializingBean, DisposableBean {
private BookDao book;
public void save() {
System.out.println("BookService运行了");
book.save();
}
//alt+insert快速生成set方法,再去配置文件中让数据层和业务层有关联
public void setBook(BookDao book) {
this.book = book;
}
@Override
public void destroy() throws Exception {
System.out.println("service结束");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("service初始化了");
}
}
建立测试类app.class
package com.itheima;
import com.itheima.dao.BookDao;
import com.itheima.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App3 {
public static void main(String[] args) {
//获取ioc容器
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
//测试bean的作用范围
BookDao aa = (BookDao) ctx.getBean("aa");
aa.save();
//关闭程序
//利用关闭钩子
ctx.registerShutdownHook();
ctx.close();//暴力关闭
}
}
在pom.xml导入spring的两个坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springioc</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
建立配置文件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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置bean、-->
<!-- 起别名name,作用范围prototype
-->
<bean id="bookdao1" name="aa bb cc" class="com.itheima.dao.impl.BookDaoimpl" init-method="init" destroy-method="destroy" />
<bean id="bookservice1" name="kk" class="com.itheima.service.impl.BookServiceimpl">
<!-- 配置service和dao发生关系-->
<!-- <property name="book" ref="aa"></property>-->
<property name="book" ref="aa"></property>
<!-- <property name="book" ref=bb"></property>-->
</bean>
</beans>