Spring—注解开发

Spring原始注解

Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文 件可以简化配置,提高开发效率。

  • @Component 使用在类上用于实例化Bean
  • @Controller 使用在web层类上用于实例化Bean
  • @Service 使用在service层类上用于实例化Bean
  • @Repository 使用在dao层类上用于实例化Bean
  • @Autowired 使用在字段上用于根据类型依赖注入
  • @Qualifier 结合@Autowired一起使用用于根据名称进行依赖注入
  • @Resource 相当于@Autowired+@Qualifier,按照名称进行注入
  • @Value 注入普通属性
  • @Scope 标注Bean的作用范围
  • @PostConstruct 使用在方法上标注该方法是Bean的初始化方法
  • @PreDestroy 使用在方法上标注该方法是Bean的销毁方法

注意:
使用注解进行开发时,需要在applicationContext.xml中配置组件扫描,作用是指定哪个包及其子包下的Bean需要 进行扫描以便识别使用注解配置的类、字段和方法。

<?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"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
"><context:component-scan base-package="com"/>
<context:property-placeholder location="jdbc.properties"/>
</beans>

使用@Compont或@Repository标识UserDaoImpl需要Spring进行实例化。

@Service("service")
@Scope("singleton")
public class service {@Autowired@Qualifier("impl")Dao dao;@Value("12")int no;@Value("${jdbc.username}")String name;public service(Dao dao) {this.dao = dao;}public service() {}public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");service i = (service) applicationContext.getBean("service");i.save();}public void save() {System.out.println(name);dao.save();}public void setDao(Dao dao) {this.dao = dao;}
}
@Repository("impl")
@Scope("singleton")
public class ImplDao implements Dao {public void save() {System.out.println("saving");}public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");ImplDao i = (ImplDao) applicationContext.getBean("impl");i.save();}
}

Spring新注解

使用上面的注解还不能全部替代xml配置文件,还需要使用注解替代的配置如下:

  • 非自定义的Bean的配置

  • 加载properties文件的配置:context:property-placeholder

  • 组件扫描的配置:context:component-scan

  • 引入其他文件:

  • @Configuration 用于指定当前类是一个 Spring 配置类,当创建容器时会从该类上加载注解

  • @ComponentScan 用于指定 Spring 在初始化容器时要扫描的包。 作用和在 Spring 的 xml 配置文件中 的 <context:component-scan base-package=“com.itheima”/>一样

  • @Bean 使用在方法上,标注将该方法的返回值存储到 Spring 容器中

  • @PropertySource 用于加载.properties 文件中的配置

  • @Import 用于导入其他配置类

@Configuration
@ComponentScan("com")
@Import(DataSourceConfiguration.class)
public class SpringConfiguration {@Testpublic void test(){ApplicationContext applicationContext=new AnnotationConfigApplicationContext(SpringConfiguration.class);service service=(service) applicationContext.getBean("service");service.save();javax.sql.DataSource dataSource=(javax.sql.DataSource)applicationContext.getBean("datasource");try {System.out.println(dataSource.getConnection());} catch (SQLException throwables) {throwables.printStackTrace();}}
}
@PropertySource("classpath:jdbc.properties")
public class DataSourceConfiguration {@Value("${jdbc.driver}")String clazz;@Value("${jdbc.url}")String url;@Value("${jdbc.username}")String user;@Value("${jdbc.password}")String password;@Bean("datasource")public ComboPooledDataSource testC3P0() throws Exception {ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();comboPooledDataSource.setDriverClass(clazz);comboPooledDataSource.setJdbcUrl(url);comboPooledDataSource.setUser(user);comboPooledDataSource.setPassword(password);return comboPooledDataSource;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391947.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

政府公开数据可视化_公开演讲如何帮助您设计更好的数据可视化

政府公开数据可视化What do good speeches and good data visualisation have in common? More than you may think.好的演讲和好的数据可视化有什么共同点&#xff1f; 超出您的想象。 Aristotle — the founding father of all things public speaking — believed that th…

C++字符串完全指引之一 —— Win32 字符编码 (转载)

C字符串完全指引之一 —— Win32 字符编码原著&#xff1a;Michael Dunn翻译&#xff1a;Chengjie Sun 原文出处&#xff1a;CodeProject&#xff1a;The Complete Guide to C Strings, Part I 引言  毫无疑问&#xff0c;我们都看到过像 TCHAR, std::string, BSTR 等各种各样…

网络计算机无法访问 请检查,局域网电脑无法访问,请检查来宾访问帐号是否开通...

局域网电脑无法访问&#xff0c;有时候并不是由于网络故障引起的&#xff0c;而是因为自身电脑的一些设置问题&#xff0c;例如之前谈过的网络参数设置不对造成局域网电脑无法访问。今天分析另一个电脑设置的因素&#xff0c;它也会导致局域网电脑无法访问&#xff0c;那就是宾…

unity中创建游戏场景_在Unity中创建Beat Em Up游戏

unity中创建游戏场景Learn how to use Unity to create a 3D Beat Em Up game in this full tutorial from Awesome Tuts. 在Awesome Tuts的完整教程中&#xff0c;了解如何使用Unity来创建3D Beat Em Up游戏。 This tutorial covers everything you need to know to make a …

雷军的金山云D轮获3亿美元!投后估值达19亿美金

12月12日&#xff0c;雷军旗下金山云宣布D轮完成3亿美元融资&#xff0c;金额为云行业单轮融资最高。至此金山云投后估值达到19亿美元&#xff0c;成为国内估值最高的独立云服务商。金山集团相关公告显示&#xff0c;金山云在本轮融资中总计发行3.535亿股D系列优先股。骊悦投资…

转:利用深度学习方法进行情感分析以及在海航舆情云平台的实践

http://geek.csdn.net/news/detail/139152 本文主要为大家介绍深度学习算法在自然语言处理任务中的应用——包括算法的原理是什么&#xff0c;相比于其他算法它具有什么优势&#xff0c;以及如何使用深度学习算法进行情感分析。 原理解析 在讲算法之前&#xff0c;我们需要先剖…

消费者行为分析_消费者行为分析-是否点击广告?

消费者行为分析什么是消费者行为&#xff1f; (What is Consumer Behavior?) consumer behavior is the study of individuals, groups, or organizations and all the activities associated with the purchase, use, and disposal of goods and services, and how the consu…

Spring—集成Junit

Spring集成Junit步骤 ①导入spring集成Junit的坐标 ②使用Runwith注解替换原来的运行期 ③使用ContextConfiguration指定配置文件或配置类 ④使用Autowired注入需要测试的对象 ⑤创建测试方法进行测试 ①导入spring集成Junit的坐标 <dependency> <groupId>org.s…

计算机的微程序存放在dram,计算机组成与结构

计算机组成与结构A/B卷填空1. 原码一位乘法中&#xff0c;符号位与数值位(分开计算)&#xff0c;运算结果的符号位等于(相乘两数符号位的异或值)。2. 微程序&#xff0c;微指令只存放在只读存储器中。3. 辅助磁道被分为若干个扇区4. 总线数据传输方式&#xff1a;_串行_,_并行_…

python算法面试_求职面试的Python算法

python算法面试During software job interviews, candidates often have to solve algorithm challenges. In this video from CupOfCode01, you will learn about common algorithm concepts in Python and how to solve algorithm challenges you may encounter in an interv…

vue实用难点讲解

此篇文章是我基于研究vue文档三遍的基础上&#xff0c;觉得还有点难理解或者难记的知识点总结 列表渲染 1.渲染组件必须加key&#xff0c;并且属性是手动传递给组件的<my-componentv-for"(item, index) in items"v-bind:item"item"v-bind:index"in…

leetcode 1208. 尽可能使字符串相等(滑动窗口)

给你两个长度相同的字符串&#xff0c;s 和 t。 将 s 中的第 i 个字符变到 t 中的第 i 个字符需要 |s[i] - t[i]| 的开销&#xff08;开销可能为 0&#xff09;&#xff0c;也就是两个字符的 ASCII 码值的差的绝对值。 用于变更字符串的最大预算是 maxCost。在转化字符串时&a…

魅族mx5游戏模式小熊猫_您不知道的5大熊猫技巧

魅族mx5游戏模式小熊猫重点 (Top highlight)I’ve been using pandas for years and each time I feel I am typing too much, I google it and I usually find a new pandas trick! I learned about these functions recently and I deem them essential because of ease of u…

可行性分析报告

1 引言1.1 编写目的&#xff1a;阐明编写可行性研究报告的目的&#xff0c;提出读者对象。1.2 项目背景&#xff1a;应包括● 所建议开发软件的名称● 项目的任务提出者、开发者、用户及实现软件的单位● 项目与其他软件或其他系统的关系。1.3 定义&#xff1a;列出文档中用到的…

(Python的)__ name__中包含什么?

_名称_变量及其在Python中的用法简介 (An introduction to the _ _name_ _ variable and its usage in Python) You’ve most likely seen the __name__ variable when you’ve gone through Python code. Below you see an example code snippet of how it may look:通过Pytho…

毕业论文计算机附录模板,毕业论文格式是什么,附录又是什么?

毕业论文格式是什么&#xff0c;附录又是什么?附录对论文内用起到一个补充说明的作用&#xff0c;附录应属于论文的正文&#xff0c;有的论文需要写明&#xff0c;有的论文可能不需要写&#xff0c;大多数情况是不需要写的&#xff0c;附录的位置一般放在论文的结尾处&#xf…

文件上传速度查询方法

由于业务迁移&#xff0c;需要将大量文件拷贝到目标机器上的/mnt目录&#xff0c;在拷贝过程中&#xff0c;想要查看上传的速度&#xff0c;做法如下&#xff1a;[rootmail01 ~]# du -sh /mnt5.6G /mnt[rootmail01 ~]# watch -n1 du -sm /mnt/ #会出现下面的一屏现象 …

spring—AOP 的动态代理技术

AOP 的动态代理技术 常用的动态代理技术 JDK 代理 : 基于接口的动态代理技术 cglib 代理&#xff1a;基于父类的动态代理技术 JDK 代理 public class proxy {Testpublic void test() {final ImplDao dao new ImplDao();Dao pro (Dao) Proxy.newProxyInstance(ImplDao.cl…

非常详细的Django使用Token(转)

基于Token的身份验证 在实现登录功能的时候,正常的B/S应用都会使用cookiesession的方式来做身份验证,后台直接向cookie中写数据,但是由于移动端的存在,移动端是没有cookie机制的,所以使用token可以实现移动端和客户端的token通信。 验证流程 整个基于Token的验证流程如下: 客户…

Java中获取完整的url

HttpServletRequest httpRequest(HttpServletRequest)request; String strBackUrl "http://" request.getServerName() //服务器地址 ":" request.getServerPort() //端口号 httpRequest.getContextPath() //项目名称 httpRequ…