注解扫描:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--注解扫描
context:上下文
context:component-scan 上下文组件(bean)扫描
-->
<context:component-scan base-package="com.cxd"/>
</beans>
三层架构调用
controller -> service -> dao
组件相关注解
@Component
//@Component 写在类上,表示该类的对象由spring容器创建
//相当于在xml中配置了<bean>标签
//对象在容器中的名字 默认是类名手写字母小写
//可以在注解中添加value值(对象在容器中的名字)
@Component(value = "student")
public class Student {
public Student() {
System.out.println("实列化Student对象");
}
}
public class Test01 {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Object bean=context.getBean("student");
bean.getClass();
}
}
@Service
UserService接口
public interface UserService {
void addUser();
}
UserServiceImpl实现UserService接口
//@Service 注解是 @CComponent注解的别名 功能与 @Component一样
// 一般写在业务层
@Service
public class UserServiceImpl implements UserService {
@Override
public void addUser() {
System.out.println("添加用户的业务");
userDao.addUser();
}
}
public class Test01 {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Object bean1=context.getBean("userServiceImpl");
System.out.println(bean1);
}
}
@Controller
//@Controller 是@Component注解别名
// 一般写在控制层
@Controller
public class UserController {
//@Autowired 自动装配,是按类型进行注入
@Autowired
private UserService userService;
public void addUser() {
userService.addUser();
}
}
@Repository
//是 @Component是注解的别名
// 写在持久层
@Repository
public class UserDao {
public void addUser(){
System.out.println("添加用户到数据库");
}
}
注入相关注解
@Autowird
controller -> service -> dao
controller层
//@Controller 是@Component注解别名
// 一般写在控制层
@Controller
public class UserController {
//@Autowired 自动装配,是按类型进行注入
@Autowired
private UserService userService;
public void addUser() {
userService.addUser();
}
}
service层
//@Service 注解是 @CComponent注解的别名 功能与 @Component一样
// 一般写在业务层
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public void addUser() {
System.out.println("添加用户到数据库");
userDao.addUser();
}
}
dao层
//是 @Component是注解的别名
// 写在持久层
@Repository
public class UserDao {
public void addUser(){
System.out.println("添加用户到数据库");
}
}
Test测试
public class Test01 {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
// Object bean=context.getBean("student");
// bean.getClass();
//
// Object bean1=context.getBean("userServiceImpl");
// System.out.println(bean1);
//
UserController bean = context.getBean(UserController.class);
bean.addUser();
}
}
@Qulifier
//@Controller 是@Component注解别名
// 一般写在控制层
@Controller
public class UserController {
//@Autowired 是spring下的注解 自动装配,是按类型进行注入
//@Qualifier 一般是和@Autowired样子使用,指定注入对象的名字
//@Autowired
//@Resource java下的注解,自动按属性名字进行注入
//@Resource
@Autowired
@Qualifier("userServiceImpl")
private UserService userServiceImpl;
public void addUser() {
userServiceImpl.addUser();
}
}
@Resource
//@Controller 是@Component注解别名
// 一般写在控制层
@Controller
public class UserController {
//@Autowired 是spring下的注解 自动装配,是按类型进行注入
//@Autowired
//@Resource java下的注解,自动按属性名字进行注入
@Resource
private UserService userServiceImpl;
public void addUser() {
userServiceImpl.addUser();
}
}
@Bean
/**
* @Bean 写在配置类的方法上
* 改方法的发回对象会存入Spring容器中
* <bean></bean>标签方法一致
* 在容器中对象的名字就是方法名
*/
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource =
new DriverManagerDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
public JdbcTemplate getJdbcTemplate() {
return new JdbcTemplate(dataSource());
}
配置类相关注解
@Configuration
//@Configuration 写在类上,表示该类是一个配置类,相当于一个xml文件
@Configuration
//@ComponentScan 组件扫描,<context:component-scan basePackages="">
@ComponentScan(basePackages = "com.cxd")
public class SpringConfig {
}
@ComponentScan
- 基本用法:在Spring配置类上添加@ComponentScan注解,指定需要被扫描的包路径。
- 例如:如果有一个包
com.example.myapp
包含了许多使用@Component等注解的类,可以通过在配置类上添加@ComponentScan(basePackages = "com.example.myapp")来指定扫描这个包。
@Import
//@Import 导入配置类
@Import(Test.class)
单元测试相关注解
@ContextConfiguration
//@ContextConfiguration Spring容器配置文件(配置类,xml)
//@ContextConfiguration(classes = SpringConfig.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
//@RunWith 以SpringJunit测试方式运行
@RunWith(SpringJUnit4ClassRunner.class)
//@RequiredArgsConstructor
public class Text02 {
@Autowired
private UserController userController;
@Test
public void test01(){
userController.addUser();
System.out.println("---");
}
}
@RunWith
//@ContextConfiguration Spring容器配置文件(配置类,xml)
@ContextConfiguration(classes = SpringConfig.class)
//@RunWith 以SpringJunit测试方式运行
@RunWith(SpringJUnit4ClassRunner.class)
//@RequiredArgsConstructor
public class Text02 {
@Autowired
private UserController userController;
@Test
public void test01(){
userController.addUser();
System.out.println("---");
}
}
@RequiredArgsConstructor
dao层
//是 @Component是注解的别名
// 写在持久层
@Repository
public class UserDao {
public void addUser(){
System.out.println("添加用户到数据库");
}
}
service层中impl
//@Service 注解是 @CComponent注解的别名 功能与 @Component一样
// 一般写在业务层
@Service
@RequiredArgsConstructor
//@AllArgsConstructor //生成有参构造,所有的属性都会添加到构造器中
public class UserServiceImpl implements UserService {
private final UserDao userDao;//会生成在有参构造中
@Override
public void addUser() {
System.out.s的业务");
userDao.addUser();
}
}
service层
public interface UserService {
void addUser();
}
controller层
//@Controller 是@Component注解别名
// 一般写在控制层
@Controller
public class UserController {
@Resource(name = "userServiceImpl")
private UserService userService;
public void addUser() {
userService.addUser();
}
}
测试类
//@ContextConfiguration Spring容器配置文件(配置类,xml)
//@ContextConfiguration(classes = SpringConfig.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
//@RunWith 以SpringJunit测试方式运行
@RunWith(SpringJUnit4ClassRunner.class)
//@RequiredArgsConstructor
public class Text02 {
@Autowired
private UserController userController;
@Test
public void test01(){
userController.addUser();
System.out.println("---");
}
}
AOP(面向切面编程)的注解
AspectJ 切面注解中五种通知注解:@Before、@After、@AfterReturning、@AfterThrowing、@Around
- @Before: 前置通知, 在方法执行之前执行
- @After: 后置通知, 在方法执行之后执行 。
- @AfterReturning: 返回通知, 在方法返回结果之后执行
- @AfterThrowing: 异常通知, 在方法抛出异常之后
- @Around: 环绕通知, 围绕着方法执行
@Aspect
//配置切入点,提供方法
@Pointcut(value = "execution(* com.cxd.service.*.*())")
public void pointcut() {
}
@Before(value = "pointcut()")
public void before(JoinPoint joinPoint) {
System.out.println("前置" + joinPoint.getSignature().getName());
}
@After(value = "execution(* com.cxd.service.*.*())")
public void after() {
System.out.println("后置");
}
public void afterReturning() {
System.out.println("返回通知");
}
public void afterThrowing() {
System.out.println("异常通知");
}
public void around(ProceedingJoinPoint joinPont) {
try {
//前置
System.out.println("前置通知");
before(joinPont);
//调用方法
Object proceed = joinPont.proceed();
//返回通知
System.out.println("返回通知");
} catch (Throwable e) {
//异常通知
System.out.println("异常通知");
} finally {
System.out.println("后置通知");
}
}
注解jar包依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.1.11</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.11</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>6.1.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>6.1.11</version>
</dependency>