首页 > 其他分享 >【Spring第六篇】注解:Annotation

【Spring第六篇】注解:Annotation

时间:2022-10-20 10:32:29浏览次数:64  
标签:www spring springframework context Spring org Annotation 注解 第六篇


注解:Annotation


首先不惜在spring容器配置中加上以下字段:

<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 指定要扫描的包-->
<context:component-scan base-package="com.kk"/>

<!-- 开启注解的支持-->
<context:annotation-config/>

</beans>

​<context:component-scan base-package="com.kk"/> 扫面com.kk目录下所有的注解​


​实体类:User​

@Component注解的作用

​在实体类中加上@Component注解,相当于在applicationContext.xml中添加 <bean id="user" class="com.kk.pojo.User"/>​

​其中在实体类中的字段加上值可以使用 @Value(""),相当于 <property name="name" value="赵六"/>​

<bean id="user" class="com.kk.pojo.User"/>
<property name="name" value="赵六"/>
</bean>
//@Component 等价 于 <bean id="user" class="com.kk.pojo.User"/>
//其中id为@Component里边的参数user @Component("user")
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
// @Value("赵六") 等价 于 <property name="name" value="赵六"/>
@Value("赵六")
public String name;

}

​dao层​

@Repository

​在dao层的接口类中,如果我们想要把其中的类交给spring容器托管,我们可以使用@Repository注解​

import org.springframework.stereotype.Repository;

@Repository
public interface UserDao {
}

​service层​

@Service

​在service层的接口类中,如果我们想要把其中的类交给spring容器托管,我们可以使用@Servicea注解​

@Service
public interface UserService {
}

​controller层​

@Controller

​在controller层中,如果我们想要把其中的类交给spring容器托管,我们可以使用@Controller注解​

@Controller
public class UserControl {
}

测试:

public class Test {
@org.junit.Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = context.getBean("user", User.class);
System.out.println(user.getName());
}
}

​tip:​

​ClassPathXmlApplicationContext是spring读取xml最常用的类。而我们一般操作的是ta的接口ApplicationContext。BeanFactory和ApplicationContext区别不大,BeanFactory不在自动BeanPostProcessor和自动 BeanFactoryPostProcessor 上注册。​

【Spring第六篇】注解:Annotation_实体类



标签:www,spring,springframework,context,Spring,org,Annotation,注解,第六篇
From: https://blog.51cto.com/u_15837794/5778759

相关文章

  • 【Spring第九篇】AOP
    文章目录​​AOP核心概念​​​​AOP:切点表达式​​​​AOP:使用切点表达式@annotation​​​​通知分类​​​​获取被增强方法相关信息​​​​【不使用自动注入】AOP方......
  • SpringBoot项目部署
    我们要想在linux系统上运行这个项目,就要保证他运行所用的端口没有被占用,不然运行就会报错查看端口使用情况netstat-anp|grep9999可以看到这个端口被占用了(没被占用的......
  • 手写自定义springboot-starter,感受框架的魅力和原理
    一、前言Springboot的自动配置原理,面试中经常问到,一直看也记不住,不如手写一个starter,加深一下记忆。看了之后发现大部分的starter都是这个原理,实践才会记忆深刻。核心思......
  • 配置Spring报错:class path resource [applicationContext.xml] cannot be opened beca
    无法打开applicationContext.xml文件问题:classpathresource[applicationContext.xml]cannotbeopenedbecauseitdoesnotexistExceptioninthread"main"org.s......
  • AOP和spring事务
    AOP面向切面编程,在不惊动原始设计的基础上增强功能,叫做无侵入式/无入侵式连接点:所有的方法(所有英雄)切入点:要追加功能的方法(已选择的英雄)通知:共性功能要追加......
  • Spring
    Bean创建的声明周期无参构造方法创建对象依赖注入使用反射判断属性是否有@AutoWried注解,如果有则给属性赋值初始化前使用反射判断方法上是否有@PostConstruct注......
  • SpringBoot+MybatisPlus--使用
    1、在entity包下面创建数据实体类,添加注解@Data,如果和数据库名字不一样的话,还需要+@TableField注解。字段名字不一样也需要添加此注解@TableName(value="user")publi......
  • SpringCloud FeignClient的坑(httpClient连接池的使用)
    SpringCloudFeignClient的坑(httpClient连接池的使用)前言在头条上已经发布过不少的文章了,根据文章的浏览量来看,go语言的市场需求明显是小于java的需求量的,最近也开始发布......
  • SpringBoot对接口请求参数(@RequestBody 和 @ Request Param)进行解密过滤
      /***@Description:拦截所有请求过滤器,并将请求类型是HttpServletRequest类型的请求替换为自定义*/@javax.servlet.annotation.WebFilter(filterName="Web......
  • SpringBoot+MybatisPlus--文件上传
    文件上传时,对页面的form表单有如下要求: 采用post方式提交数据   method="post"采用multipart格式上传文件  enctype="multipart/form-data"使用inp......