这里我将用spring来开发一个简单的书店项目
Step1:所用到的开发软件为 idea , 所用到的jar包为
这些jar的下载可在 官网 下载,选择5.2.6版本下载,不会的在下面评论私信
Step2:打开idea,创建java项目,然后项目命名 BookStore
Step3:导入jar包
①项目文件夹下创建文件夹libs,用来存放jar包,并将上面的12个jar包都直接复制到文件夹内
②将jar导入,第一步只是简单的将jar包复制进去了,还不能使用
Step4:在src下建立包 com.tty.spring,然后在该包下继续建5个包,分别是配置文件夹 config, 实体文件夹 entity , 然后是三层架构 (控制层 control , 业务层 service , 持久层 dao)
Step5: 在配置文件夹下建类 BookConfig,并编写代码
Step6: 在实体文件夹下建类 Book,并编写代码
Step7: 在持久层文件夹下建接口BookDao 和类BookDaoImpl,并编写代码
Step8: 在业务层文件夹下建类BookService,并编写代码
Step9: 在控制层下建类BookControl,并编写代码
Step10: 在包com.tty.spring下建立三个类,分别是 BookStoreEntry 入口类,BookStoreProxy 增强类,TestBookStore 测试类,并编写代码
①我们先来编写增强类,这个也叫 “代理”
②我们先来编写测试类,这个类是做测试用的
写完了这里,我们可以简单的测试一下啦
结果如下:
③我们最后来编写书店入口类
点击main方法运行一次,结果如下:
一个简单的书店小项目就完成了,下面附上各类的源码:
// 配置类源码
package com.tty.spring.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan(basePackages = "com.tty.spring")
@EnableAspectJAutoProxy
public class BookConfig {
}
//实体类源码
package com.tty.spring.entity;
import org.springframework.stereotype.Component;
@Component
public class Book {
private String b_name;
private int b_price;
public void setB_name(String b_name) {
this.b_name = b_name;
}
public void setB_price(int b_price) {
this.b_price = b_price;
}
@Override
public String toString() {
return "Book{" +
"b_name='" + b_name + '\'' +
", b_price=" + b_price +
'}';
}
}
//持久层 接口源码
package com.tty.spring.dao;
import com.tty.spring.entity.Book;
public interface BookDao {
public Book queryBook();
}
//持久层接口实现类源码
package com.tty.spring.dao;
import com.tty.spring.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class BookDaoImpl implements BookDao{
@Autowired
private Book book;
@Override
public Book queryBook() {
book.setB_name("spring教程");
book.setB_price(99);
return this.book;
}
}
//服务层源码
package com.tty.spring.service;
import com.tty.spring.dao.BookDao;
import com.tty.spring.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service
public class BookService {
@Autowired
private BookDao bookDao;
public Book queryBook(){
return bookDao.queryBook();
}
}
//控制层源码
package com.tty.spring.control;
import com.tty.spring.entity.Book;
import com.tty.spring.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
@Controller
public class BookControl {
@Autowired
private BookService bookService;
public Book sellBook(){
return bookService.queryBook();
}
}
//增强类源码
package com.tty.spring;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class BookStoreProxy {
@Before(value = "execution(* com.tty.spring.service.BookService.queryBook(..))")
public void before(){
System.out.println("现在开始卖书啦!....");
}
}
//测试类源码
package com.tty.spring;
import com.tty.spring.config.BookConfig;
import com.tty.spring.control.BookControl;
import com.tty.spring.entity.Book;
import com.tty.spring.service.BookService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestBookStore {
@Test
public void test(){
ApplicationContext context = new AnnotationConfigApplicationContext(BookConfig.class);
BookControl bookControl = context.getBean("bookControl", BookControl.class);
Book book = bookControl.sellBook();
System.out.println(book);
}
}
//书店入口类
package com.tty.spring;
import com.tty.spring.config.BookConfig;
import com.tty.spring.control.BookControl;
import com.tty.spring.entity.Book;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.Scanner;
public class BookStoreEntry {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(BookConfig.class);
BookControl bookControl = context.getBean("bookControl", BookControl.class);
System.out.println("您好! 欢迎光临本书店");
System.out.println("请选择 1. 购书 | 2.离开 ");
Scanner sc=new Scanner(System.in);
int val=sc.nextInt();
if(val==1){
Book book = bookControl.sellBook();
System.out.println(book);
}
if (val==2){
System.out.println("欢迎下次再来!");
}
}
}