给容器中注入组件
@Component:交给ioc容器处理
@Controller:控制层
@Service:业务层
@Repository:数据层
@Controller、@Service、@Repository都可以称为@Component。
注入bean的注解
@Autowired:默认按照类型匹配的
@Resource:是按照名称匹配的
@Autowired
private UserDao userDao;
public class Text {
public static void main(String[] args) {
//创建ioc容器皿
//xml
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
//java配置
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
//从ioc容器中获取User对象
Student student= (Student) context.getBean("student");
System.out.println("注解:"+student);
}
}
Java配置类相关注解
@Configuration:声明当前类为配置类;
@ComponentScan:用于对有Component注解的类进行扫描;
@ComponentScan({"com"})
public class Config {}
切面(AOP)相关注解
@Aspect:定义切面类
@EnableAspectJAutoProxy:动态处理,在java配置类中使用开启Spring的支持
@After:最终通知
@Before:前置通知
@AfterReturning:后置通知
@AfterThrowing:异常通知
@Around:环绕通知
@Around("pott()")
public Object aourd(ProceedingJoinPoint point){
Object result=null;
//前置
long begin=new Date().getTime();
Object[] args=point.getArgs();
try {
result=point.proceed(args);
} catch (Throwable e) {
throw new RuntimeException(e);
}
//后置
long end=new Date().getTime();
System.out.println("时间:"+(end-begin)+"毫秒");
return result;
}
SpringMVC常用注解
@Controller:交给ioc容器处理
@RequestBody:允许request的参数在request体中,而不是在直接连接的地址后面。(放在参数前)
@ResponseBody:支持将返回值放到response内,而不是一个页面,返回json数据。
@PostMapping ("test2")
public String test2(@RequestBody User user){
log.info("姓名:"+user.getUserName()+",密码:"+user.getPassword());
return "test2";
}
@RestController:组合注解,相当于@Controller和@ResponseBody的组合,注解在类上,意味着,该Controller的所有方法都默认加上了@ResponseBody。
@RequestMapping:处理前端请求
@RequestMapping("/test")
@GetMapping:处理前端请求
@PosttMapping:处理前端请求
@RequestBody:允许request的参数在request体中,而不是在直接连接的地址后面。(放在参数前)
ps:lombook的一些注解
Lombook常用注解:
@Data:get、set,方法
@NoArgsConstructor:无参构造方法
@AllArgsConstructor:有参构造方法
@Slf4j:日志管理