How to use the spring framework to develop a simple small project - bookstore project

Here I will use spring to develop a simple bookstore project

Step1: The development software used is idea, and the jar package used is

The download of these jars can be downloaded on the official website , choose the 5.2.6 version to download, if you don’t, please comment on the private message below

Step2: Open the idea, create a java project, and then name the project BookStore

Step3: Import the jar package

          ① Create a folder libs under the project folder to store jar packages, and copy the above 12 jar packages directly into the folder

                

 

 

       ②Import the jar, the first step is simply to copy the jar package into it, and it cannot be used yet         

   

 

 

 

 

Step4: Create the package com.tty.spring under src , and then continue to build 5 packages under the package, which are the configuration folder config, the entity folder entity, and then the three-tier architecture (control layer control, business layer service, persistence layer dao)

Step5:  Create a class BookConfig under the configuration folder and write code

 

 

Step6:  Create a class Book under the entity folder and write code

Step7:  Create interface BookDao and class BookDaoImpl under the persistence layer folder, and write code

 

 

 

Step8:  Create a class BookService under the business layer folder and write code

 

 

 Step9:  Create a class BookControl under the control layer and write code

 

 

Step10:  Create three classes under the package com.tty.spring, namely BookStoreEntry entry class, BookStoreProxy enhancement class, TestBookStore test class, and write code

 

         ① Let's write the enhanced class first, which is also called "agent"

         ② Let's write the test class first, this class is for testing

 

 

After writing here, we can simply test it

 The result is as follows:

③ Finally, we will write the bookstore entrance class

 Click the main method to run once, and the result is as follows:

 A simple small bookstore project is completed, and various source codes are attached below:

// 配置类源码
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("欢迎下次再来!");
        }



    }
}

             

Guess you like

Origin blog.csdn.net/qq_49174867/article/details/124514360