提交 31e89f54 编写于 作者: unravel space's avatar unravel space 😆

注解spring开发测试

上级 c6e1b373
<?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">
<parent>
<artifactId>spring-framework-test</artifactId>
<groupId>com.test</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-annocation</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
</project>
\ No newline at end of file
package com.test.annocation.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
*
*<p>des</p>
*
* @project spring-framework-test
* @description
* @author Phoenix
* @Date 2024/4/9 10:43:17
* @version 1.0
**/
@Configuration
@ComponentScan({"com.test.annocation"})
public class SpringConfig
{
}
package com.test.annocation.dao;
/**
*
*<p>des</p>
*
* @project spring-framework-test
* @description
* @author Phoenix
* @Date 2024/4/9 10:45:11
* @version 1.0
**/
public interface BookDao
{
public void save();
}
package com.test.annocation.dao.impl;
import com.test.annocation.dao.BookDao;
import org.springframework.stereotype.Component;
/**
*
*<p>des</p>
*
* @project spring-framework-test
* @description
* @author Phoenix
* @Date 2024/4/9 10:45:49
* @version 1.0
**/
@Component("bookDao")
public class BookDaoImpl implements BookDao
{
@Override
public void save()
{
System.out.println("dao save ...");
}
}
package com.test.annocation.service;
/**
*
*<p>des</p>
*
* @project spring-framework-test
* @description
* @author Phoenix
* @Date 2024/4/9 10:45:27
* @version 1.0
**/
public interface BookService
{
public void save();
}
package com.test.annocation.service.impl;
import com.test.annocation.service.BookService;
import org.springframework.stereotype.Component;
/**
*
*<p>des</p>
*
* @project spring-framework-test
* @description
* @author Phoenix
* @Date 2024/4/9 10:46:07
* @version 1.0
**/
@Component("bookService")
public class BookServiceImpl implements BookService
{
@Override
public void save()
{
System.out.println("bookService save ...");
}
}
package com.test.annocation.controller;
import com.test.annocation.config.SpringConfig;
import com.test.annocation.dao.BookDao;
import com.test.annocation.service.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
*
*<p>des</p>
*
* @project spring-framework-test
* @description
* @author Phoenix
* @Date 2024/4/9 12:19:54
* @version 1.0
**/
public class AnnocationController
{
@Test
public void test01(){
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
BookService bookService = ctx.getBean("bookService", BookService.class);
bookService.save();
BookDao bookDao = ctx.getBean("bookDao", BookDao.class);
bookDao.save();
}
@Test
public void test02(){
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
BookService bookService = ctx.getBean("bookService", BookService.class);
bookService.save();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册