目录
建立空项目
建立一个模块: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;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
//@Component
@Repository
@Scope
public class BookDaoimpl implements BookDao {
public void save() {
System.out.println("BookDao运行了");
}
@PostConstruct
public void init() {
System.out.println("初始化成功");
}
@PreDestroy
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.dao.impl.BookDaoimpl;
import com.itheima.service.BookService;
import org.springframework.stereotype.Component;
@Component
public class BookServiceimpl implements BookService {
private BookDao bookDao;
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
public void save() {
System.out.println("BookService运行了");
bookDao.save();
}
}
建立测试类app.class
package com.itheima;
import com.itheima.Config.SpringConfig;
import com.itheima.dao.BookDao;
import com.itheima.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class APPText {
public static void main(String[] args) {
//新的加载配置类的方式
AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
BookDao bookdao1 = (BookDao) ctx.getBean(BookDao.class);
BookDao bookdao2 = (BookDao) ctx.getBean(BookDao.class);
System.out.println(bookdao1);
System.out.println(bookdao2);
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>
SpringConfig.class
package com.itheima.Config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.itheima")
public class SpringConfig {
}
测试结果
小结