Spring注解开发配置
applicationContext.xml配置文件
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:component-scan base-package="hjy"/> <context:annotation-config/></beans>
<context:component-scan base-package="hjy"/> //指定要扫描的包,这个包下的注解就会生效
pom.xml配置文件
<?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"><modelVersion>4.0.0</modelVersion><groupId>com.hjy</groupId><artifactId>hellospring</artifactId><version>1.0-SNAPSHOT</version><dependencies><!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.0.RELEASE</version></dependency></dependencies></project>
注解说明:
@Autowired:通过类型、名字自动装配
如果@Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value="实现类名称")指定
@Resource:自动装配通过类型、名字
@Component:组件,放在类上,说明这个类被spring管理了,就是bean
//等价于<bean id="user" class="com.hjy.pojo.User"/>
@Value:给属性赋值 也可以写在set方法上
//等价于 <property name="name" value="酸奶"/>
@Component有几个衍生注解,我们在web开发中会按照MVC三层架构分层。
dao层:@Repository
service层: @Service
controller层: @Controller
这四个注解功能一样,都是将类注册到spring中,装配Bean
@Scope("singleton") //作用域
Spring注解开发配置相关推荐
- spring注解开发配置spring父子容器
spring注解开发配置spring父子容器 官网 https://docs.spring.io/spring-framework/docs/current/spring-framework-refe ...
- spring注解开发:容器中注册组件方式
1.包扫描+组件标注注解 使用到的注解如下,主要针对自己写的类 @Controller @Service @Repository @Component @ComponentScan 参考 spring ...
- 关于Spring注解开发教程,打包全送你
摘要:spring是我们web开发中必不可少的一个框架,基于传统的xml方式配置bean总觉得太过繁琐,从spring2.5之后注解的出现可以大大简化我们的配置. 本文分享自华为云社区<如何高效 ...
- Spring注解开发学习笔记
1 IOC 1.1 工厂模式 使用工厂中方法代替new形式创建对象的一种设计模式 1.2 Inversion of Control控制翻转 一种思想,用于消减代码间的耦合. 实现思想:利用工厂设计模式 ...
- 纯注解开发配置spring
1.纯注解开发[定义配置类的注解] ==@Confituration == 表示该类是一个配置类 ==@ComponentScan("com.itheima") == 配置包扫描 ...
- Spring注解开发以及基于java的容器配置
Spring4后的注解开发 使用bean.xml实现注解开发 需要导入aop包(如果没有导入该包,注解无效) beans.xml配置 <?xml version="1.0" ...
- Spring注解开发-Bean注册
1.使用注解开发我们需要在applicationContext.xml文件中添加context标签. 在配置文件中开启注解扫描. <?xml version="1.0" en ...
- Spring—注解开发
Spring原始注解 Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文 件可以简化配置,提高开发效率. @Component 使用在类上用于 ...
- Spring注解开发入门教程
注解开发: 什么是驱动注解? 注解启动时使用注解的形式替代xml配置,将繁杂的spring配置文件从工程中彻底消除掉,简化书写 注解驱动的弊端 为了达成注解驱动的目的,可能会将原先很简单的书写,变的更 ...
最新文章
- iMeta | 中科院生态中心邓晔组发布微生物组网络分析平台iNAP(图文/视频教程)
- C++网络游戏程序员笔试题
- 零基础python必背代码-30个Python常用极简代码,拿走就用
- 《C和指针》读书笔记第一章快速上手
- boost::hana::capture用法的测试程序
- Java script第二课
- linux的安全性能,技术|Linux 系统安全性能检查小记
- 淘宝成全球电商第一人气网站
- 几个例子弄懂JS 的setInterval的运行方式
- Selenium WebDriver控制操作(Python)
- 设置eclipse中的编辑区的背景颜色、注释文字的颜色、修改注释内作者名和时间...
- linux 远程shell,linux 远程shell 实现
- java 百亿计算器,百万换算成亿(百万换算成亿计算器)
- 使用Enum.Prase及Enum.TryPrase时的注意事项
- 【AI语音】魔百盒M301H-JL代工-3798MV300芯片-支持蓝牙_免费固件包
- 织梦网站后台-织梦网站后台登陆方式-织梦CMS模板
- 300最不常用的springboot计算机毕业设计题目汇总,总有你需要用到的
- PC端网页特效 | 常见网页特效案例
- DirectX游戏编程入门——第一部分(Windows和DirectX游戏编程引言) —— 初识DirectX
- intellij idea、notepad++、Google浏览器以及Windows 10系统修改背景为豆沙绿护眼色